Java Reference
In-Depth Information
L
ISTING
17.3
Continued
<html>
<head>
<title>Request Bean Example</title>
</head>
<body>
<!-- call the counter's setCount() method -->
<!-- so that the current value of the property -->
<!-- count is changed. -->
17
<%
counter.setCount(10);
%>
<jsp:forward page=”RequestBean2.jsp” />
</body>
</html>
The only thing you really need to notice about this JSP is the fact that the scope of the bean is
set to
request
, and the
counter.setCount()
method is called with the value of 10. This call is
to prove that the JSP to which the request is forwarded is receiving the same instance of the
counter
bean. Listing 17.4 contains the source for the second JSP.
L
ISTING
17.4
RequestBean2.jsp
<%@ page errorPage=”errorpage.jsp” %>
<!-- Instantiate the Counter bean with an id of “counter” -->
<jsp:useBean id=”counter” scope=”request” class=”Counter” />
<html>
<head>
<title>Request Bean Example 2</title>
</head>
<body>
<H3>Request Bean Example 2</H3>
<center><b>The current count for the counter bean is: </b>
<%=counter.getCount() %></center>
</body>
</html>
As you examine the source of the second JSP, you will see that it gets a reference to the
counter
bean from the request, and then prints the current value of the bean's
count
property.