Java Reference
In-Depth Information
 
page scope — The bean is accessible within the JSP page with the <jsp:useBean> element or
any of the page's static include files until the page sends a response to the client or forwards a
request to another file.
 
request scope — The bean is accessible from any JSP page processing the same request until a
JSP page sends a response to the client or forwards the request to another file.
 
session scope — The bean is accessible from any JSP page in the same session as the JSP
page that creates the bean. The bean exists across the entire session, and any page that
participates in the session can use it. The page in which you create the bean must have a <%@
page %> directive with session=true.
 
application — The bean is accessible from any JSP page in the same application as the JSP
page that creates the bean. The bean exists across an entire JSP application, and any page in the
application can use the bean.
Caution
When using the <jsp:useBean> tag, the closing "/" at the end of the tag is very
important. If you forget the "/", the tag will work, but it will work unpredictably. For
example, I have spent hours trying to find what I thought was a scope problem
before noticing that I had lost the closing slash in one of a number of JSP pages
using the bean.
Properties: the <jsp:getProperty> and <jsp:setProperty> tags
JavaBean properties are private data fields that can be accessed through predefined "accessor"
methods or getter and setter methods. Property getter and setter method names follow specific rules
called design patterns . By using these design pattern-based method names, JSP pages can access a
JavaBean's properties through these directives:
<jsp:setProperty name="TestBean" property="service" value="login"/>
<jsp:setProperty name="TestBean" property="username"
value='<%=request.getParameter("username")%>'/>
<jsp:setProperty name="TestBean" property="password"/>
<jsp:getProperty name="TestBean" property="message"/>
Notice the different ways in which you can set the value of a JavaBean parameter. The first example
shows how you can set the parameter using a static value. The second shows the use of an evaluation
expression to set the value. In the third case, the value is set implicitly to the value of the same name in
the CGI query parameters. A fourth variant is discussed later in this chapter, under the heading
" Introspection ."
Using JavaBeans, you can get away from using in-line Java code completely. Simply code the logic as
a JavaBean, and load the JavaBean by name, using the jsp:getProperty and jsp:setProperty
tags to access its properties.
Using <jsp:setProperty> for initialization
Recall that the JSP engine only loads a new instance of the bean if it can't find an existing instance in
scope. This means you can use a bean as a storage container that keeps track of the application's data
anywhere within its scope. This raises the question of how the bean is initialized. The answer is to nest
the initialization inside the <jsp:useBean> element itself, as shown here:
<jsp:useBean id="TestBean" class="JavaDatabaseBible.TestBean"/>
<jsp:setProperty name="TestBean" property="message" value="goodbye"/>
</jsp:useBean>
Any <jsp:setProperty> elements nested within the <jsp:useBean> element are executed only when the
bean is first loaded and run. These nested elements are not executed if the bean is found within the
current scope. The idea here is that the first time the bean is used, it is initialized; in subsequent
references; however, it is assumed that you actually want the data stored in the bean by earlier
references.
Search WWH ::




Custom Search