JavaServer Pages: Generating Dynamic Content
Pages: 1, 2, 3, 4, 5, 6, 7, 8
Using JavaBeans
There is also some dynamic content in this example. Step back a moment and think about the type of dynamic content you see on the Web every day. Common examples might be a list of web sites matching a search criteria on a search engine site, the content of a shopping cart on an e-commerce site, a personalized news page, or messages on a bulletin board. Dynamic content is content generated by some server process, for instance the result of a database query. Before it is sent to the browser, the dynamic content needs to be combined with regular HTML elements into a page with the right layout, navigation bars, the company logo, and so forth. In a JSP page, the regular HTML is the template text described earlier. The result of the server processing--the dynamic content--is commonly represented by a JavaBeans component.
A JavaBeans component, or just a bean for short, is a Java class that follows certain coding conventions, so it can be used by tools as a component in a larger application. In this chapter, we discuss only how to use a bean, not how to develop one. (If you're a programmer and not already familiar with JavaBeans, you may want to skip ahead to Chapter 15, Developing JavaBeans for JSP, to learn about these coding conventions.) A bean is often used in JSP as the container for the dynamic content to be displayed by a web page. Typically, a bean represents something specific, such as a person, a product, or a shopping order. A bean is always created by a server process and given to the JSP page. The page then uses JSP elements to insert the bean's data into the HTML template text.
The type of element used to access a bean in a page is called a JSP action element. JSP action elements are executed when a JSP page is requested (this is called the request processing phase, as you may recall from Chapter 3). In other words, JSP actions represent dynamic actions that take place at runtime, as opposed to JSP directives, which are used only during the translation phase (when the JSP page is turned into Java servlet code). JSP defines a number of standard actions and also specifies how you can develop custom actions. For both standard and custom action elements, use the following notation:
<action_name attr1="value1" attr2="value2">action_body</action_name>
Action elements, or tags as they are sometimes called,[2] are grouped into libraries (known as tag libraries). The action name is composed of two parts:
a library prefix and the name of the action within the library, separated by a
colon (i.e., jsp:useBean). All actions in the JSP
standard library use the prefix jsp, while custom
actions can use any prefix except jsp, jspx, java, javax, servlet, sun, or sunw. You specify
input to the action through attribute/value pairs in the opening tag. The
attribute names are case-sensitive, and the values must be enclosed in single
or double quotes. For some actions, you can also enter data that the action
should process in the action's body. It can be any text value, such as a SQL
statement, or even other nested JSP action elements. You will see examples of
action elements with a body later.
|
Related Reading
|
Before you use a bean in a page, you must tell the JSP container
which type of bean it is and associate it with a name. The first JSP action in
Example 5-1, <jsp:useBean>, is used for this purpose:
<jsp:useBean id="clock" class="java.util.Date" />
The id attribute is used to give the
bean a unique name. It must be a name that is a valid Java variable name: it
must start with a letter and cannot contain special characters such as dots,
plus signs, etc. The class attribute contains the
fully qualified name of the bean's Java class. Here, the name clock is associated with an instance of the class java.util.Date. Note that we don't specify a body for
this action. When you omit the body, you must end the opening tag with />, as in this example. In this case, when the JSP
container encounters this directive, there is no bean currently available with
the name clock, so the <jsp:useBean> action creates a bean as an instance
of the specified class and makes it available to other actions in the same
page. In Chapter 8, Sharing Data Between JSP Pages,
Requests, and Users, you will see how <jsp:useBean> can also be used to locate a bean
that has already been created.
Incidentally, the <jsp:useBean>
action supports three additional attributes: scope,
type, and beanName. The
scope attribute is described in detail in Chapter
8, and the other two attributes are covered in Appendix A, JSP Elements Syntax Reference. We don't need to worry
about those attributes here.
Accessing JavaBean Properties
The bean's data is represented by its properties. If you're a page author charged with
developing a JSP page to display the content represented by a bean, you first
need to know the names of all the bean's properties. This information should
be available from the Java programmers on the team or from a third-party
source. In this example, we use a standard Java class named java.util.Date as a bean with properties representing
date and time information. Table 5-1 describes the properties used in this example. (If you're not a
programmer, don't worry about the Java Type and Access columns at this
point.)
Table 5-1: Properties for java.util.Date
|
Property Name |
Java Type |
Access |
Description |
|---|---|---|---|
|
date |
|
read |
The day of the month as a number between 1 and 31 |
|
hours |
|
read |
The hour as a number between 0 (midnight) and 23 |
|
minutes |
|
read |
The number of minutes past the hour as a number between 0 and 59 |
|
month |
|
read |
The month as a number from 0 to 11 |
|
year |
|
read |
The current year minus 1900 |
Once you have created a bean and given it a name, you can
retrieve the values of the bean's properties in the response page with another
JSP standard action, <jsp:getProperty>. This
action obtains the current value of a bean property and inserts it directly
into the response body.
To include the current date property
value in the page, use the following tag:
<jsp:getProperty name="clock" property="date" />
The name attribute, set to clock, refers to the specific bean instance we defined
with the <jsp:useBean> action previously.
This action locates the bean and asks it for the value of the property
specified by the property attribute. As documented
in Table 5-1, the date property contains the day of the
month as a number between 1 and 31. In Example 5-1, multiple <jsp:getProperty> actions
are used to generate a list of all the clock bean's
property values. The result is the page shown in Figure 5-2.
