[ Team LiB ] |
![]() ![]() |
Recipe 16.7 Accessing or Removing Session Attributes in ServletsProblemYou want to access or remove a session attribute in a servlet. SolutionUse the javax.servlet.http.HttpSession.getAttribute(String attributeName) method to access the attribute. Use the removeAttribute(String attributeName) method to remove the attribute from the session. DiscussionTo access a session attribute, you must first bind the attribute to a session, as in Recipe 16.5. The object attribute is now available to the user associated with that session. Example 16-9 accesses an attribute named com.jspservletcookbook.ContextObject. The example just shows the code relating to accessing an attribute from the session. Example 16-5 in Recipe 16.3 shows the entire servlet and doGet( ) method for accessing an object attribute.
Example 16-9. Gaining access to the session attribute in a servletpackage com.jspservletcookbook;
...
<!-- this code appears in the servlet's doGet or doPost method, whichever is appropriate.
The ContextObject class is stored in WEB-INF/classes/com/jspservletcookbook/ -->
//Create a session if one does not exist yet
HttpSession session = request.getSession( );
//This local variable will hold the object attribute
ContextObject contextObj = null;
//get access to an object attribute in the session
if (session != null)
contextObj = (ContextObject) session.getAttribute(
"com.jspservletcookbook.ContextObject");
//ensure the contextObj is not null before calling any methods
if (contextObj != null)
out.println( contextObj.getValues( ) );
<!-- rest of servlet class and doGet or doPost method goes here -->
You must take these steps before accessing a session attribute:
Removing the session attribute from a servletTo remove an attribute, call HttpSession.removeAttribute( ) with the name of the attribute. Use the following code in a servlet to remove the attribute this chapter has been working with: HttpSession session = request.getSession( ); <!-- HttpSession.removeAttribute will have no effect if an attribute of that name does not exist --> if (session != null) session.removeAttribute("com.jspservletcookbook.ContextObject"); Now the attribute is no longer available in the session associated with the user that requested the servlet. The session attribute is still available in other sessions where it may be stored (albeit in the form of a different instance). Each user is associated with a specific session, and each session can carry its own instance of the object attribute.
See AlsoRecipe 16.1-Recipe 16.4 on handling ServletContext attributes in servlets and JSPs; Recipe 16.5 on setting session attributes in servlets; Recipe 16.6 on setting session attributes in JSPs; Recipe 16.8 on accessing or removing session attributes in JSPs; Recipe 16.9-Recipe 16.12 on handling request attributes in servlets and JSPs; Recipe 14.6 on using a session event listener; the Javadoc for javax.servlet.http.HttpSessionAttributeListener: http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpSessionAttributeListener.html. |
[ Team LiB ] |
![]() ![]() |