Conditional Processing

In most web applications, you produce different output based on runtime conditions, such as the state of a bean or the value of a request header such as UserAgent (containing information about the type of client that is accessing the page).

If the differences are not too great, you can use JSP scripting elements to control which parts of the JSP page are sent to the browser, generating alternative outputs from the same JSP page. However, if the outputs are completely different, I recommend using a separate JSP page for each alternative and passing control from one page to another. This chapter contains a number of examples in which one page is used. In the remainder of this book you’ll see plenty of examples where multiple pages are used instead.

Using JavaBeans Properties

In Chapter 5, you saw how to use the <jsp:getProperty> and the <jsp:setProperty> actions to access a bean’s properties. However, a bean is just a Java class that follows certain coding conventions, so you can also call its methods directly.

Briefly, a bean is a class with a constructor that doesn’t take an argument. This makes it possible for a tool, such as the JSP container, to create an instance of the bean class simply by knowing the class name. The other condition of a bean that we are concerned with is the naming of the methods used to access its properties. The method names for reading and writing a property value, collectively known as the bean’s accessor methods, must be composed ...

Get Java Server Pages now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.