Up to this point, we have treated the objects that were created with jsp:useBean as though they were simply bound to local variables in the _jspService method (which is called by the service method of the servlet that is generated from the page). Although the beans are indeed bound to local variables, that is not the only behavior. They are also stored in one of four different locations, depending on the value of the optional scope attribute of jsp:useBean . When you use scope , the system first looks for an existing bean of the specified name in the designated location. Only when the system fails to find a preexisting bean does it create a new one. This behavior lets a servlet handle complex user requests by setting up beans, storing them in one of the three standard shared locations (the request, the session, or the servlet context), then forwarding the request to one of several possible JSP pages to present results appropriate to the request data. For details on this approach, see Chapter 15 (Integrating Servlets and JSP: The Model View Controller (MVC) Architecture). As described below, the scope attribute has four possible values : page (the default), request , session , and application .
Creating Beans ConditionallyTo make bean sharing more convenient , you can conditionally evaluate bean- related elements in two situations. First, a jsp:useBean element results in a new bean being instantiated only if no bean with the same id and scope can be found. If a bean with the same id and scope is found, the preexisting bean is simply bound to the variable referenced by id . Second, instead of <jsp:useBean ... /> you can use <jsp:useBean ...> statements </jsp:useBean> The point of using the second form is that the statements between the jsp:useBean start and end tags are executed only if a new bean is created, not if an existing bean is used. Because jsp:useBean invokes the default (zero-argument) constructor, you frequently need to modify the properties after the bean is created. To mimic a constructor, however, you should make these modifications only when the bean is first created, not when an existing (and presumably updated) bean is accessed. No problem: multiple pages can contain jsp:setProperty statements between the start and end tags of jsp:useBean ; only the page first accessed executes the statements. For example, Listing 14.8 shows a simple bean that defines two properties: accessCount and firstPage . The accessCount property records cumulative access counts to any of a set of related pages and thus should be executed for all requests. The firstPage property stores the name of the first page that was accessed and thus should be executed only by the page that is first accessed. To enforce the distinction, we place the jsp:setProperty statement that updates the accessCount property in unconditional code and place the jsp:setProperty statement for firstPage between the start and end tags of jsp:useBean . Listing 14.9 shows the first of three pages that use this approach. The source code archive at http://www.coreservlets.com/ contains the other two nearly identical pages. Figure 14-5 shows a typical result. Listing 14.8 AccessCountBean.javapackage coreservlets; /** Simple bean to illustrate sharing beans through * use of the scope attribute of jsp:useBean. */ public class AccessCountBean { private String firstPage; private int accessCount = 1; public String getFirstPage() { return(firstPage); } public void setFirstPage(String firstPage) { this.firstPage = firstPage; } public int getAccessCount() { return(accessCount); } public void setAccessCountIncrement(int increment) { accessCount = accessCount + increment; } } Listing 14.9 SharedCounts1.jsp<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Shared Access Counts: Page 1</TITLE> <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER"> <TR><TH CLASS="TITLE"> Shared Access Counts: Page 1</TABLE> <P> <jsp:useBean id="counter" class="coreservlets.AccessCountBean" scope="application"> <jsp:setProperty name="counter" property="firstPage" value="SharedCounts1.jsp" /> </jsp:useBean> Of SharedCounts1.jsp (this page), <A HREF="SharedCounts2.jsp">SharedCounts2.jsp</A>, and <A HREF="SharedCounts3.jsp">SharedCounts3.jsp</A>, <jsp:getProperty name="counter" property="firstPage" /> was the first page accessed. <P> Collectively, the three pages have been accessed <jsp:getProperty name="counter" property="accessCount" /> times. <jsp:setProperty name="counter" property="accessCountIncrement" value="1" /> </BODY></HTML> Figure 14-5. Result of a user visiting SharedCounts3.jsp . The first page visited by any user was SharedCounts2.jsp . SharedCounts1.jsp , SharedCounts2.jsp , and SharedCounts3.jsp were collectively visited a total of twelve times after the server was last started but before the visit shown in this figure.
|