JSP Scopes

JSPs provide a number of scopes for the sharing of data. Scopes equate to a predetermined lifetime for an object that is enforced by the container. There are four JSP scopes available: page, request, session, and application. All of the scopes contain various objects that can hold application data.

The shortest-lived of the scopes is the page scope, which is accessed through the pageContext implicit variable. The page scope is limited to a single JSP and a single request. Once the page has completed rendering the objects contained inside of it, the page scope is no longer available. From the pageContext object, you can also access all of the available scopes. Every object stored in the various scopes is always an object and not a primitive data type. The getAttribute method of the javax.servlet.jsp.PageContext can be used to retrieve data from the pageContext by name . The page context provides a place to store key/value pairs that only need to be available during the rendering of the page. There is also a setAttribute method that allows you to set objects into the pageContext. Table 14-2 lists some of the commonly used pageContext methods .

Table 14-2: pageContext Methods

Methods

Description

Object getAttribute(String)

Retrieves an object from the page scope

Void setAttribute(String, Object)

Sets an object into the page scope

HttpServletRequest getRequest()

Retrieves the request object

Void removeAttribute(String)

Removes an attribute from the page scope

The next shortest-lived scope is the request scope. This scope is available from the time the user submits the request until the page has completed rendering. The request scope is where the HTML form parameters are stored. Request parameters are always of type String and are populated from the submitted form. For example, if a user is completing a web form in their browser and clicks submit, all of the form fields are placed into the request scope. The parameters that are passed to the server are accessible from the request scope via the getParameter(String) method. The following code example checks to see if the username parameter is null or contains an empty string ( ) and, if so, sets an ErrorMsg object into the request using the setAttribute(String, Object) method:

 <%       if(request.getParameter("username") == null                       "".equals(request.getParameter("username")) {         request.setAttribute("errorMsg",                                new ErrorMsg("Username is required."); } %> 

The page can then print the message using the getAttribute(String) method. This method returns an object, so the retrieved object must be cast back to our Custom ErrorMsg object as follows :

 <%= ((ErrorMsg) request.getAttribute("errorMsg") ).getErrorText()) %> 
Note  

As we cover JSP Standard Tag Library (JSTL) and the JSP Expression language in this chapter, you will learn how to access the object above without resorting to the expression tag or scriptlets.

The session scope is often one of the most misused scopes. Novice developers will often overuse the session scope by storing lots of objects that would be better stored in the request scope. The reason this is considered undesirable is that the session scope is fairly long lived. When a user creates a session, it has a specified timeout, which is set in the web.xml file. This timeout value is typically 30 minutes and sometimes longer. The session only times out when the client that owns the session has not made another request in 30 minutes. So while the user may be done, the objects that were created will not be garbage-collected for 30 minutes. The objects take up resources until the timeout is reached. Overusing the session will restrict your application s scalability. If you use the session to store transient objects, you must be sure to remove them when you re finished with them. A good general rule of thumb is to only store objects that are required throughout the life of the session. The session scope has the same set and get attribute methods as the request scope, though it does not have get and set parameter methods. All items stored in the session scope are also objects and must be typecast .

The application scope is the longest-lived scope and is available from the start of the application and exists until it is shut down. The objects in this scope are shared across all web components that make up the application. To access or set attributes in this context, you can use the getAttribute and setAttribute methods of the ServletContext interface.

Knowing what scope to put an object in is key to good web development. The rule of thumb is to place the objects you create in the shortest-lived scope possible. If you have a web form that spans multiple JSPs, use the HTML hidden fields to store and pass along the data until it is ready to be aggregated into an object. Do not create a temporary object and place it into the session.



Oracle Application Server 10g Web Development
Oracle Application Server 10g Web Development (Oracle Press)
ISBN: 0072255110
EAN: 2147483647
Year: 2004
Pages: 192

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net