[ Team LiB ] Previous Section Next Section

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="http://java.sun.com/jstl/core" prefix="c" %>

<head><title>Accessing a Scoped Value</title></head>
<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" />


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: http://jakarta.apache.org/taglibs/index.html; Sun Microsystem's JSTL information page: http://java.sun.com/products/jsp/jstl/; 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.

    [ Team LiB ] Previous Section Next Section