In most cases, you use the JSP expression language in conjunction with the Model-View-Controller architecture (Chapter 15) in which the servlet creates the data and the JSP page presents the data. In such a scenario, the JSP page is usually only interested in the objects that the servlet created, and the general bean-access and collection-access mechanisms are sufficient.
However, the expression language is not restricted to use in the MVC approach; if the server supports JSP 2.0 and the web.xml file refers to servlets 2.4, the expression language can be used in any JSP page. To make this capability useful, the specification defines the following implicit objects.
pageContext
The pageContext object refers to the PageContext of the current page. The PageContext class, in turn , has request , response , session , out , and servletContext properties (i.e., getRequest , getResponse , getSession , getOut , and getServletContext methods ). So, for example, the following outputs the current session ID.
${pageContext.session.id}
param and paramValues
These objects let you access the primary request parameter value ( param ) or the array of request parameter values ( paramValues ). So, for example, the following outputs the value of the custID request parameter (with an empty string, not null , returned if the parameter does not exist in the current request).
${param.custID}
For more information on dealing with request parameters, see Chapter 4.
header and headerValues
These objects access the primary and complete HTTP request header values, respectively. Remember that dot notation cannot be used when the value after the dot would be an illegal property name . So, for example, to access the Accept header, you could use either
${header.Accept}
or
${header["Accept"]}
But, to access the Accept-Encoding header, you must use
${header["Accept-Encoding"]}
For more information on dealing with HTTP request headers, see Chapter 5.
cookie
The cookie object lets you quickly reference incoming cookies. However, the return value is the Cookie object, not the cookie value. To access the value, use the standard value property (i.e., the getValue method) of the Cookie class. So, for example, either of the following outputs the value of the cookie named userCookie (or an empty string if no such cookie is found).
${cookie.userCookie.value} ${cookie["userCookie"].value}
For more information on using cookies, see Chapter 8.
initParam
The initParam object lets you easily access context initialization parameters. For example, the following outputs the value of the init param named defaultColor .
${initParam.defaultColor}
For more information on using initialization parameters, see Volume 2 of this book.
pageScope, requestScope, sessionScope, and applicationScope
These objects let you restrict where the system looks for scoped variables . For example, with
${name}
the system searches for name in the PageContext , the HttpServletRequest , the HttpSession , and the ServletContext , returning the first match it finds. On the other hand, with
${requestScope.name}
the system only looks in the HttpServletRequest .
The JSP page of Listing 16.10 uses the implicit objects to output a request parameter, an HTTP request header, a cookie value, and information about the server. Figure 16-5 shows the results.