Recipe 23.9 Accessing Scoped Variables with the EL


You want to grab and display the value of an object attribute using a JSTL custom tag.


Use the EL and the c:out tag to get the value of an attribute that has been stored in a certain scope.


An object such as a java.util.Date , a java.lang.Integer , or an object that you design, can be stored in four different scopes:

  • page , so that it's only available in the servlet or JSP where it is created

  • request scope, which makes the object available to any pages that interact with the JSP using a RequestDispatcher , such as a request that is forwarded from one JSP to another

  • session scope stores object attributes for any servlets or JSPs that participate in the same session (see Chapter 11)

  • application scope, which represents the entire servlet context for one web application

Example 23-10 uses the c:set JSTL tag to set a variable named com.jspservletcookbook.SessionObject to session scope. Then c:out accesses and displays the value of the variable.

Example 23-10. Accessing the value of an object stored in session scope
 <%@ taglib uri="" prefix="c" %> <html> <head><title>Accessing a Scoped Value</title></head> <body> <h2>Here is the value of the Session-Scoped Attribute</h2>  <c:set var=   "com.jspservletcookbook.SessionObject" value=     "My object attribute.<br />" scope="session" /> <c:out value=   "${sessionScope[\"com.jspservletcookbook.SessionObject\"]}" escapeXml="false" />  </body> </html> 

By convention, object attributes are named after fully qualified Java classes (usually, after the Java type of the stored object). Therefore, the attribute name has period characters (.) in it. This is the purpose of the syntax ${sessionScope[\"com.jspservletcookbook.SessionObject\"]} . If the attribute name does not contain periods, then you can use an EL expression consisting of just the variable name, without the sessionScope JSTL implicit object, in order to access the object attribute:

 <c:out value=   "${SessionObject}" escapeXml="false" /> 

If you just include the scoped object's name, as in the prior code fragment, then the JSTL will search the page , request , session , and application scopes for an attribute of that name, returning null if the JSTL does not find one.

You must use the required characters of an EL expression (the dollar sign and curly braces surrounding the expression: "${ ... }"). Otherwise the c:out tag will just output a String literal such as "SessionObject."

See Also

The Jakarta Project's Taglibs site: taglibs /index.html; Sun Microsystem's JSTL information page:; Recipe 23.3 on using the core tags; Recipe 23.4 and Recipe 23.5 on using the XML tags; Recipe 23.6 on using the formatting tags; Recipe 23.7 and Recipe 23.8 on using the SQL JSTL tags; Recipe 23.10-Recipe 23.14 on using the EL to access request parameters, cookies, and JavaBean properties.

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

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: