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.

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 ...

Get Programming Jakarta Struts now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.