2.3 Struts and Scope

The Struts framework uses various shared resource areas to store objects. The shared resource areas all have a lifetime and visibility rule that defines the scope of the resource. This section discusses these resources, their scopes, and how the framework uses them.

2.3.1 Request Scope

Each time a client issues an HTTP request, the server creates an object that implements the javax.servlet.http.HttpServletRequest interface. Among other things, this object contains a collection of key/value attribute pairs that can be used to store objects for the lifetime of the request. The key of each pair is a String, and the value can be any type of Object. The methods to store objects in and retrieve them from the request scope are:

public void setAttribute( String name, Object obj );
public Object getAttribute( String name );

Request-scope attributes can be removed using the removeAttribute( ) method; however, because the scope of the attribute is only for the lifetime of the request, it is not as important to remove them as it is for other scoped attributes. Once the server fulfills a request and a response is returned to the client, the request and its attributes are no longer available to the client and may be garbage-collected by the JVM.

The Struts framework provides the ability to store JavaBeans in a request, so that they can be used by presentation components such as JSP pages. This makes it much easier to access JavaBeans data, without having to do a manual cleanup of the objects later. There's seldom a need to remove objects from request scope; the web container takes care of it for you. Objects stored at the request level are visible only to resources that have access to that request. Once the response has been returned to the client, the visibility is gone. Objects that are stored in one request are not visible to any other client request.

2.3.2 Session Scope

The next-higher level of visibility is session scope. The web container creates an object that implements the javax.servlet.http.HttpSession interface to identify a user across multiple page requests. When the session is created depends on the application and the container implementation. The user's session will persist for a period of time that is based on how often the user makes requests. This allowed inactivity time is configurable through the application deployment descriptor. It also can be destroyed prematurely by calling the invalidate( ) method on the session object.

The session also allows for a collection of objects to be stored based on a key/value pair schema, as with the request object. The only difference between this one and the one provided by the request is the duration of the objects. Because sessions exist across multiple requests, objects stored in the session scope live longer than those at the request level. The Struts framework uses session attributes extensively. An example of an object that may be stored as a session attribute is the Locale object for the user. This allows the entire framework access to the user's locale to perform localized behavior. Objects stored in one user's session are not visible to users with a different session.

The web container provides no synchronization for objects stored in the session. If multiple threads attempt to access an object stored in the session and modify the value, it's up to the developer to provide synchronization. Although the need to synchronize access to the session is quite low, the developer is responsible for protecting the resources. For example, if your application uses frames, or if you have a process that takes a long time to complete, multiple threads might be accessing the session at the same time.

2.3.3 Application Scope

An even higher level of visibility and duration comes with objects stored at the application-scope level. Application-scoped objects are visible to all clients and threads of the current web application. They live until they are programmatically removed or until the application is terminated. The servlet container creates an object that implements the javax.servlet.ServletContext interface for each and every web application that is installed within the container. This is done when the container is first started.

Beyond the scope for the request and session objects, the ServletContext allows application objects to be stored and retrieved by the entire application and to persist for the lifetime of the application. The Struts framework uses application scope to store JavaBeans that need to be visible to all users. Normally, objects are stored in this scope during application startup and remain there until the application exits.

2.3.4 Page Scope

The last scope we will discuss, page scope, has to do exclusively with JSP pages. Objects with page scope are stored in the javax.servlet.jsp.PageContext for each page and are accessible only within the JSP page in which they were created. Once the response is sent to the client or the page forwards to another resource, the objects are no longer available.

Every JSP page has an implicit object reference named pageContext that can be used to store and retrieve page-level objects. It includes the same getAttribute( ) and setAttribute( ) methods that the other scopes offer, and they function in the same manner.