Java Reference
In-Depth Information
16
The final two actions you perform show how you can get the current value of a bean's prop-
erty. The first of these two examples uses a scriptlet. It simply accesses the bean by its refer-
enced name counter and calls the getCount() method, just as any other Java code would. The
scriptlet snippet is listed here:
<%
// write the current value of the property count
out.println(“Count from scriptlet code : “
+ counter.getCount() + “<BR>”);
%>
The second example uses the <jsp:getProperty> standard action, which requires the name of
the bean and the property to be accessed. The action takes the attribute, calls the appropriate
accessor, and embeds the results directly into the resulting HTML document, as shown in the
following:
<!-- Get the bean's count property, -->
<!-- using the jsp:getProperty action. -->
Count from jsp:getProperty :
<jsp:getProperty name=”counter” property=”count” /><BR>
Notice that the second reference to the count property results in a value that is one greater that
the first reference. This is because both methods of accessing the count property result in a
call to the getCount() method, which increments the value of count . To see this in action
compile the Counter class, move it to the <SERVER_ROOT>/djs/WEB-INF/classes directory,
and copy the BeanCounter.jsp file to the <SERVER_ROOT>/djs/ directory. Now you should
open your browser to the following URL:
http://localhost/djs/BeanCounter.jsp
Another thing you might want to try is changing the value of the <jsp:useBean> action's
scope attribute. This will affect the life of the bean and the value of the count property in the
previous example. We will examine bean scope further in the Chapter 17, “Using JavaBeans
and JSP Scopes.” The available options for the scope attribute are described in Table 16.4.
T ABLE 16.4
The scope Values for the <jsp:useBean> Action
Value
Definition
Objects with page scope are accessible only within the page where they
were created. References to an object with page scope will be released
when the response is sent back to the client or the request is forwarded to
another resource. Objects with page scope are stored in the pagecontext .
page
 
Search WWH ::




Custom Search