[ Team LiB ] Previous Section Next Section

Recipe 10.5 Reading Cookie Values with a JSP

Problem

You want to read cookie values with a JSP.

Solution

Use the JSTL and its cookie implicit object to display the name and value of any cookies found in the request.

Discussion

The JSTL and its EL have a cookie implicit object (a variable that is automatically available to JSP or EL code) that you can use in JSPs to display any cookie names and values. For more information on the JSTL and EL, see Chapter 23.

You can access the cookie implicit object in JSP code this way:

${cookie}

This implicit object evaluates to a java.util.Map type whose values you can iterate over with the c:forEach JSTL tag. Each iteration of c:forEach returns a java.util.Map.Entry, which encapsulates a key/value pair. The key is the name of the cookie; the value is a javax.servlet.http.Cookie object.

Example 10-6 uses this code to retrieve a Cookie object from the Map of available cookies:

<c:forEach var="cookieVal" items="${cookie}">

The var attribute of c:forEach contains a Map.Entry object whose key is the cookie name; the value is the Cookie object. The code uses c:out tags to display the cookie names and values in the JSP. This odd syntax displays the value of each cookie:

<c:out value="${cookieVal.value.value}" />

The code cookieVal.value evaluates to the javax.servlet.http.Cookie object. The full phrase ${cookieVal.value.value} is the equivalent of calling Cookie.getValue( ).

Example 10-6. A JSP that reads cookie names and values
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<html>
<body>
<%-- check whether the request contains any cookies --%>
<c:choose>
  <c:when test="${empty cookie}" >
  <h2>We did not find any cookies in the request</h2>
  </c:when>
<c:otherwise>

<h2>The name and value of each found cookie</h2>

<c:forEach var="cookieVal" items="${cookie}">
<strong>Cookie name:</strong> <c:out value="${cookieVal.key}" /><br>
<strong>Cookie value:</strong> <c:out value=
    "${cookieVal.value.value}" /><br><br>
</c:forEach>
</c:otherwise>
</c:choose>

</body>
</html>

Figure 10-4 shows the JSP displaying the available cookie information.

Figure 10-4. Output of the cookieReader.jsp page
figs/jsjc_1004.gif

Make sure to include the taglib directive for the JSTL core library at the top of your JSP, so that you can use the JSTL tags to view any cookie values:

<%@ taglib uri=
"http://java.sun.com/jstl/core" prefix="c" %>

Use uri=http://java.sun.com/jsp/jstl/core when using JSTL 1.1

See Also

Recipe 10.1 on setting a cookie with a servlet; Recipe 10.2 on creating an array from all of the request's cookies; Recipe 10.3 on setting a cookie with a JSP; Recipe 10.4 on using a servlet to read cookies; Recipe 10.6 on altering or removing an existing cookie; the RFC 2109 document dealing with cookies: ftp://ftp.rfc-editor.org/in-notes/rfc2109.txt; Netscape's preliminary specification for cookies: http://wp.netscape.com/newsref/std/cookie_spec.html; The Java Cookie API: http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/Cookie.html.

    [ Team LiB ] Previous Section Next Section