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.
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.
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).
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
But, to access the Accept-Encoding header, you must use
For more information on dealing with HTTP request headers, see Chapter 5.
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).
For more information on using cookies, see Chapter 8.
The initParam object lets you easily access context initialization parameters. For example, the following outputs the value of the init param named 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
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
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.