Recipe 16.6 Setting Session Attributes in JSPs


Problem

You want to bind an object to a session in a JSP.

Solution

Use the jsp:useBean and c:set tags to create an instance of an object and assign it as an attribute to the session.

Discussion

The JSTL core tags and the jsp:useBean standard action can be used to manage session attributes in JSPs. Example 16-8 binds an object attribute to a session, displays a value from the object, and then shows the session ID of the client who requested the JSP. The bound object is the ContextObject that I have used throughout this chapter as the stored attribute. It contains a java.util.Map type for storing the IP addresses of users who request the JSP (see Example 16-1 and the accompanying description of the code).

Example 16-8. Setting a session attribute in a JSP
  <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>  <html> <head><title>Context binding JSP</title></head> <body> <h2>Here are the values from the bound ContextObject</h2> <%-- Create instances of the ContextObject and Date classes --%>  <jsp:useBean id="contextObj" class=     "com.jspservletcookbook.ContextObject" />  <jsp:useBean id="date" class="java.util.Date" /> <%-- Bind the object attribute to the session scope--%> <c:set var=     "com.jspservletcookbook.ContextObject" value="${contextObj}" scope=         "session" /> <%-- Put a value in the object, then display the value--%> <c:set target=     "${sessionScope[\"com.jspservletcookbook.ContextObject\"].map}" value=         "${date}" property="${pageContext.request.remoteAddr}"/> <c:out value="${sessionScope[\"com.jspservletcookbook.ContextObject\"].     values}" escapeXml="false" />  <h2>Here is the session ID</h2>  <c:out value="${pageContext.session.id}" />  </body> </html> 

This code from Example 16-8 binds the object to the session:

 <c:set var=     "com.jspservletcookbook.ContextObject" value="${contextObj}" scope=         "session" /> 

The only difference between Example 16-8 and the JSP of Recipe 16.2, which binds the object to the ServletContext , is the value of the scope attribute in the c:set tag ( session in this case). In similar fashion, the c:set tag sets a value in the session attribute by referring to the sessionScope implicit variable:

 <c:set target=     "${  sessionScope  [\"com.jspservletcookbook.ContextObject\"].map}" value=         "${date}" property="${pageContext.request.remoteAddr}"/> 

The EL mechanism automatically makes available the sessionScope implicit variable, which represents a java.util.Map type that stores any object variables in session scope.

If you have an attribute name that does not include period characters in it, you can provide the attribute name without any further context, and the EL will search the page , request , session , and application scopes for an attribute of that name. For example, the following EL syntax returns a session object attribute named contextObj without using an implicit variable (or null if that session attribute does not exist) to further qualify the name:

 ${contextObj} 

See Also

Chapter 23 on using the JSTL; Recipe 16.1-Recipe 16.4 on handling ServletContext attributes in servlets and JSPs; Recipe 16.7 on accessing or removing session attributes in servlets; 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.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon

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