For the convenience of the web application programmer, a servlet container provides separate scopes, each of which manages a table of name/value bindings. These scopes typically hold beans and other objects that need to be available in different components of a web application. Request Scope The request scope is short-lived. It starts when an HTTP request is submitted and ends when the response is sent back to the client. The f:loadBundle tag places the bundle variable in request scope. You would place an object into request scope only if you wanted to forward it to another processing phase inside the current request. NOTE | If a request is forwarded to another request, all name/value pairs stored in the request scope are carried over to the new request. On the other hand, if a request is redirected, the request data are lost. |
Session Scope Recall that the HTTP protocol is stateless. The browser sends a request to the server, the server returns a response, and then neither the browser nor the server has any obligation to keep any memory of the transaction. This simple arrangement works well for retrieving basic information, but it is unsatisfactory for server-side applications. For example, in a shopping application, you want the server to remember the contents of the shopping cart. For that reason, servlet containers augment the HTTP protocol to keep track of a session, that is, repeated connections by the same client. There are various methods for session tracking. The simplest method uses cookies: name/value pairs that a server sends to a client, hoping to have them returned in subsequent requests (see Figure 2-6). Figure 2-6. The Cookie Sent by a JSF Application
As long as the client doesn't deactivate cookies, the server receives a session identifier with each subsequent request. Application servers use fallback strategies, such as URL rewriting, for dealing with those clients that don't return cookies. URL rewriting adds a session identifier to an URL, which looks somewhat like this: http://corejsf.com/login/index.jsp;jsessionid=64C28D1FC...D28 Session tracking with cookies is completely transparent to the web developer, and the standard JSF tags automatically perform URL rewriting if a client does not use cookies. The session scope persists from the time that a session is established until session termination. A session terminates if the web application invokes the invalidate method on the HttpSession object or if it times out. Web applications typically place most of their beans into session scope. For example, a UserBean can contain information about users that is accessible throughout the entire session. A ShoppingCartBean can be filled up gradually during the requests that make up a session. Application Scope Finally, the application scope persists for the entire duration of the web application. That scope is shared among all requests and all sessions. You can see in Chapter 10 how to use the application scope for global beans such as LDAP directories. |