Java Reference
In-Depth Information
L
ISTING
16.2
Continued
<%
// write the current value of the property count
out.println(“Count from scriptlet code : “
+ counter.getCount() + “<BR>”);
%>
<!-- Get the bean's count property, -->
<!-- using the jsp:getProperty action. -->
Count from jsp:getProperty :
<jsp:getProperty name=”counter” property=”count” /><BR>
</BODY>
</HTML>
In the
BeanCounter.jsp
page, you perform five actions that give examples of using beans in a
JSP. You first tell the JSP engine that the scripting language you are using is Java, with the fol-
lowing snippet:
<%@ page language=”java” %>
You then create an instance of the class
Counter
, with a
scope
of
session
, and assign it an
id
of
counter
. You do this using the standard action
<jsp:useBean>
. Now you can reference this
bean, by using the name
counter
, throughout the rest of your JSP. The code snippet that cre-
ates the bean is as follows:
<jsp:useBean id=”counter” scope=”session” class=”Counter” />
The third action sets the bean's
count
property to the value of the
count
parameter, if it exists
in the request. The code snippet that performs this action is as follows:
<!-- Set the bean's count property to the value of -->
<!-- the request parameter “count”, using the -->
<!-- jsp:setProperty action. -->
<jsp:setProperty name=”counter” property=”count” param=”count” />
In this snippet, you use the
<jsp:setProperty>
standard action to set the value of the
count
property. You do this by setting the
name
attribute to the name of the bean you want to refer-
ence, the
property
attribute to the name of the property to be set, and the
param
attribute to the
name of the request parameter you want to set the property to.