[ Team LiB ] |
![]() ![]() |
Recipe 23.13 Accessing Cookies with the ELProblemYou want to take a look at all of the cookie names and values using EL code. SolutionUse the cookie EL implicit object in the JSP to display any cookie names and values. DiscussionThe cookie EL implicit object is a java.util.Map type that maps cookie names (like "JSESSIONID") to javax.servlet.Cookie objects. Since the cookies are stored in a Map, you can use the c:forEach tag to cycle through the map and display each cookie name and value using c:out (see Example 23-14).
Example 23-14. using the EL to display each cookie name and value in a JSP<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html> <head><title>Cookie display</title></head> <body> <h2>Here are all the Available Cookies</h2> <%-- ${cookies.key}equals the cookie name; ${cookies.value} equals the Cookie object; ${cookies.value.value} returns the cookie value --%> <c:forEach var="cookies" items="${cookie}"> <strong> <c:out value="${cookies.key}"/> </strong>: Object= <c:out value="${cookies.value}"/>, value= <c:out value="${cookies.value.value}"/><br /> </c:forEach> </body> </html> The c:forEach tag stores the entry in the Map for each cookie in a variable named cookies. The code uses the EL phrase "${cookies.key}" to access the name of each cookie. You would think "${cookies.value}" returns the value for each cookie; however, this syntax returns the Cookie object itself. The weird syntax "${cookies.value.value}" returns the value of the cookie. Figure 23-8 shows how the JSP displays this information. Figure 23-8. Displaying a cookie object and value with the JSTL![]() See AlsoChapter 10 on reading and setting cookies; the Jakarta Project's Taglibs site: http://jakarta.apache.org/taglibs/index.html; the Sun Microsystems 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.9-Recipe 23.12 on using the EL to access scoped variables, request parameters, and request headers; Recipe 23.14 on using the EL to access JavaBean properties. ![]() |
[ Team LiB ] |
![]() ![]() |