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
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
, and the value can be any type of
public void setAttribute( String name, Object obj ); public Object getAttribute( String name );
Request-scope attributes can be removed using the
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 can be
The Struts framework provides the ability to store JavaBeans in a request, so that they can be used by presentation
2.3.2 Session Scope
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.
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
Beyond the scope for the request and session objects, the
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
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.