| [ directory ] |
|
3.4 Making Data Available Throughout an ApplicationSo far, all the beans used have been fairly transient entities: They are summoned into existence through a jsp:useBean tag at the top of a page and disappear when the page ends. This is usually a good thing; in fact, this is the very property that allows each user to get his or her own version of the bean. However, in many situations, it is useful to have a bean last longer than a single page. A simple mechanism allows JSP authors to specify any of several lifetimes for their beans. These lifetimes are called scopes, and they indicate the period of activity during which the bean is available. If a bean is created and placed in a scope, any changes to that bean made via jsp:setProperty tags will be visible to other pages using the same bean. A scope may be given to a bean by adding scope= to the jsp:useBean tag: <jsp:useBean id="bean name" class="bean class" scope="scope"/> Legal values for the scope property are "page", "request", "session", and "application". Each may be useful in different situations. 3.4.1 The Page ScopeBeans created in the page scope, the smallest scope, will be available only within the JSP from which they were created. It is reasonable to think of the page scope as spanning a single JSP file. This means that if one page includes another by using a jsp:include tag and that if they each use a jsp:useBean tag to obtain the same bean in the page scope, they will each in fact get a different instance of the bean. This is illustrated in Listing 3.6. Listing 3.6 A page that uses page scope<jsp:useBean id="bean1" class="com.awl.jspbook.ch03.AnimalBean" scope="page"/> At first, our animal is: <jsp:getProperty name="bean1" property="animal"/> <br> <jsp:setProperty name="bean1" property="animal" value="octopus"/> After setting the property, the animal is: <jsp:getProperty name="bean1" property="animal"/> <br> <jsp:include page="page_scope2.jsp"/> This example creates a bean, places it in the page scope, and then shows the value of the bean's animal property. This bean comes with a default value for this property, "ferret", which will be displayed by the first jsp:getProperty. This property is then changed to "octopus" and redisplayed, to prove that it changed. Then the page includes another page, which is shown in Listing 3.7. Listing 3.7 An included page that reuses the bean<jsp:useBean id="bean1" class="com.awl.jspbook.ch03.AnimalBean" scope="page"/> In the include, the animal is: <jsp:getProperty name="bean1" property="animal"/> This page obtains the same bean, and once again displays the animal property. Because the bean is in the page scope, a completely new instance of the bean will be created when the included page reaches the jsp:useBean statement; this instance will still have the original value, "ferret", and so the page will display the following: At first, our animal is: ferret After setting the property, the animal is: octopus In the include, the animal is: ferret 3.4.2 The Request ScopeThe request scope is larger than the page scope; beans created in the request scope will last from the time they are first created until the last of the data is sent to the user. Most significantly, this means that the same instance of the bean will be available to all included pages, which can be demonstrated by changing scope="page" to scope="request" in Listings 3.6 and 3.7. When this is done, the resulting page will look like this: At first, our animal is: ferret After setting the property, the animal is: octopus In the include, the animal is: octopus The request scope is useful when multiple components of a page are scattered among several files that include one another, and all need access to the same data. As this is the most common situation, the request scope is the default. In all the previous examples, in which no scope was specified, we were in fact using the request scope without knowing it. 3.4.3 The Session ScopeBoth of the scopes encountered so far associate data with pages or pieces of pages. But because users, not pages, should be the focus of a site, a way is needed to tie data to a particular user so it will be available and adjustable from whichever pages the user visits. An obvious example of this is a shopping cart. Multiple users can all see the same "checkout" page of a shopping site, but each user will see his or her own selections. Likewise, many users may view a particular item, but when one user elects to buy it, the item goes into that person's shopping cart and no one else's. In JSP terms, data associated with a user is in the session scope. A session does not correspond directly to a user but rather to the period of time the user spends at a site. Typically, this period is defined as extending from the first visit to the site, through the point at which the user has not accessed any pages on the site for more than half an hour. Thereafter, the session will have expired, and any beans in the session scope will disappear. Listing 3.8 demonstrates a simple use of the session scope to keep track of how many times a user has accessed the site. Listing 3.8 A counter bean in the session scope<jsp:useBean id="counter" class="com.awl.jspbook.ch03.CounterBean" scope="session"/> <jsp:setProperty name="counter" property="incrementCount" value="1"/> You have visited this page <jsp:getProperty name="counter" property="count"/> time(s). This example doesn't look like anything special, but a user who accesses the page and repeatedly clicks the browser's reload button will see the counter steadily climb. Two things make this page work. The first is the way the incrementCount property works. When this property is set, it adds its value to the count property, much as Listing 3.5 added the value1 and value2 properties to set the sum property. More important, the secret to this page's functionality is the session scope, which keeps the bean around even after the page completes. If the scope were changed to page or request, the counter would stay stuck at 1 perpetually. The session scope extends across multiple pages, as well as across individual pages multiple times. If the jsp:useBean and jsp:setProperty tags were copied into another page, the counter would then register the total number of times the user had visited both pages during the current session. 3.4.4 The Application ScopeThe application scope is the largest of the available scopes. Beans placed in the application scope will be available to all pages, for all users, until the Web application is shut down or restarted. This scope is useful for displaying global information, such as a "quote of the day" that might be displayed at the bottom of every page across a site. The application scope can also be used to create a counter that displays the total number of times a page has been accessed. A simple modification to Listing 3.8 counts how many times each user has accessed a page: To make this global, change scope="session" to scope="application". |
| [ directory ] |
|