Recipe 23.11 Using the EL to Access Request Headers


Problem

You want to use the EL to access the value of various HTTP request headers.

Solution

Use the header implicit object that the EL makes available for custom tags.

Discussion

The header implicit object is a java.util.Map type that contains a request header value mapped to each header key (which is the header name , such as "accept" or " user -agent"). Web clients (usually browsers) send these headers or name/value pairs along with the web address of the page they are interested in.

Example 23-12 uses the c:forEach iteration tag to cycle through each of the Map's stored request headers. The variable reqHead stores the current header/value pair. The code uses the EL to display the header name ("${reqHead.key}") and header value ("${reqHead.value}").

Example 23-12. Using the JSTL to display request headers
  <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>  <html> <head><title>Request header display</title></head> <body> <h2>Here are all the Request Headers</h2>  <%-- 'header' represents a java.util.Map type holding request-header names and values --%> <c:forEach var="reqHead" items="${header}">     <strong><c:out value=         "${reqHead.key}"/></strong>: <c:out value="${reqHead.value}"/><br />          </c:forEach>  </body> </html> 

Figure 23-7 shows the result of requesting this JSP in a web browser.

Figure 23-7. Displaying request headers in a JSP
figs/jsjc_2307.gif

See Also

Chapter 18 on working with the client request; 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.4 and Recipe 23.5 on using the SQL JSTL tags; Recipe 23.12-Recipe 23.14 on using the EL to access one request header, cookies, and JavaBean properties.



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