Recipe 16.7 Accessing or Removing Session Attributes in Servlets


You want to access or remove a session attribute in a servlet.


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


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

The HttpSession.getAttribute( ) method returns an Object type, so the return value has to be cast to the appropriate type before calling any methods on it.

Example 16-9. Gaining access to the session attribute in a servlet
 package 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:

  1. Compile the class of the object that will be stored in the session.

  2. Place this class in WEB-INF/classes or in WEB-INF/lib if it's stored in a JAR file.

  3. Make sure a servlet, JSP, or other web component sets the attribute to the session with the HttpSession.setAttribute( ) method.

Removing the session attribute from a servlet

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

When you remove the attribute from the ServletContext , on the other hand, it is no longer available to any users, because there is only one ServletContext for each nondistributed web application.

See Also

Recipe 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 :

Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326 © 2008-2017.
If you may any questions please contact us: