16.8 Referencing Implicit Objects

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).

 ${cookie.userCookie.value} ${cookie["userCookie"].value} 

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 .

An Example

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.

Listing 16.10 implicit-objects.jsp
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD><TITLE>Using Implicit Objects</TITLE> <LINK REL=STYLESHEET       HREF="/el/JSP-Styles.css"       TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER">   <TR><TH CLASS="TITLE">   Using Implicit Objects </TABLE> <P> <UL>   <LI><B>test Request Parameter:</B>  ${param.test}  <LI><B>User-Agent Header:</B>  ${header["User-Agent"]}  <LI><B>JSESSIONID Cookie Value:</B>  ${cookie.JSESSIONID.value}  <LI><B>Server:</B>  ${pageContext.servletContext.serverInfo}  </UL> </BODY></HTML> 
Figure 16-5. A number of scoped variables are defined automatically. These predefined variables are called "implicit objects."


Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net