[ Team LiB ] |
![]() ![]() |
Recipe 16.8 Accessing or Removing Session Attributes in JSPsProblemYou want to access or remove a session attribute in a JSP. SolutionUse the c:out JSTL core tag to display the value of an attribute and the c:remove tag to remove the attribute from the session. DiscussionHere are the steps to access or remove a session-scoped variable with the JSTL and a JSP:
The code in this recipe shows how to reference a session-scoped variable, as opposed to a ServletContext attribute (shown in Recipe 16.4). This code uses the sessionScope implicit object of the EL, which is an automatically available variable in EL format that contains any session-scoped object attributes. This code represents a portion of a JSP that displays the values contained in an attribute named com.jspservletcookbook.ContextObject.
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> //HTML or other presentation code here... <c:out value= "${sessionScope[\"com.jspservletcookbook.ContextObject\"].values}" escapeXml="false" />
This JSP code removes a session-scoped variable using the c:remove core tag: <c:remove var= "com.jspservletcookbook.ContextObject" scope="session" /> The object attribute is no longer available for the individual session associated with the user that requested this JSP. In other words, the c:remove tag does not remove all session attributes of the specified name, just the session attribute(s) associated with any user who requests the JSP containing the c:remove tag. See AlsoChapter 23 on using the JSTL; 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.7 on accessing or removing session attributes in servlets; 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 ] |
![]() ![]() |