Recipe 23.12 Using the EL to Access One Request Header


Problem

You want to use the EL to access the value of one particular HTTP request header.

Solution

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

Discussion

The headerValues implicit object is a java.util.Map type that contains a String array (a String[] type) for every header name . Example 23-13 displays only the value of the " user -agent" request header, which identifies the type of browser the client is using.

Example 23-13. Using the JSTL to display request headers
  <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>  <html> <head><title>User Agent</title></head> <body> <h2>Here is your user agent</h2>  <%-- 'headerValues' represents a java.util.Map type holding a String[] type for every  request header--%>     <strong><c:out value=         "${headerValues[\"user-agent\"][0]}"/> </strong>  </body> </html> 

The code accesses only the first member of the String array (it is highly likely that the user-agent request header only involves one value). The expression:

 ${headerValues[\"user-agent\"]} 

returns the array , and the entire expression, including the "[0]" array operator, which returns the name of the user agent, such as "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)."

See Also

Chapter 18 on working with the client request; Recipe 23.11 on how to use the EL to access all the available request headers; 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.10 on using the EL to access request parameters; Recipe 23.13 and Recipe 23.14 on using the EL to access 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