2.7 Implicit ObjectsArguably, the most useful feature of the JSTL expression language is the implicit objects it defines for accessing all kinds of application data. Those implicit objects are listed in Table 2.5.
[a] All keys are strings. There are three types of JSTL implicit objects:
The rest of this section examines each of the JSTL implicit objects in the order listed above; the first category begins at "Accessing Request Parameters" below, the second category begins at "Accessing Scoped Attributes" on page 78, and use of the pageContext implicit object begins at "Accessing JSP Page and Servlet Properties" on page 80. Accessing Request ParametersRequest parameters are the lifeblood of most Web applications, passing information from one Web component to another. That crucial role makes the param and paramValues implicit objects, both of which access request parameters, the most heavily used JSTL implicit objects.The param and paramValues implicit objects are both maps of request parameters. For both the param and paramValues maps, keys are request parameter names, but the values corresponding to those keys are different for param and paramValues; param stores the first value specified for a request parameter, whereas paramValues stores a String array that contains all the values specified for a request parameter.[11] [11] Two of the implicit objects listed in Table 2.5 have plural names: paramValues and headerValues; both are maps that associate keys with String arrays. The other implicit objects associate keys with scalar values. Most often, the overriding factor that determines whether you use param or paramValue is the type of HTML element a request parameter represents; for example, Figure 2-5 shows a Web application that uses both param and paramValues to display request parameters defined by a form. Figure 2-5. Accessing Request Parameters with the param and paramValues Implicit Objects![]() The Web application shown in Figure 2-5 consists of two JSP pages, one that contains a form (top picture) and another that interprets the form's data (bottom picture). Listing 2.13 lists the JSP page that contains the form. Listing 2.13 index.jsp
The preceding JSP page is unremarkable; it creates an HTML form with two textfields and a select element that allows multiple selection. That form's action, param.jsp, is the focus of our discussion. It is listed in Listing 2.14. Listing 2.14 param.jsp
The preceding JSP page does four things of interest. First, it displays the lastName and firstName request parameters, using the param implicit object. Since we know that those request parameters represent textfields, we know that they are a single value, so the param implicit object fits the bill.Second, the JSP page displays all of the request parameters and their values, using the paramValues implicit object and the <c:forEach> action.The <c:forEach> Action" on page 154 for more information about <c:forEach>. Because the paramValues implicit object is a map, you can access its values directly if you know the keys, meaning the request parameter names. For example, the third point of interest in the preceding JSP page iterates over the array of strings representing selected languagesparamValues.languages. The selected languages are accessed through the paramValues map by use of the key languages.To emphasize the difference between param and paramValues, the fourth point of interest is the value of the param.languages request parameter, which contains only the first language selected in the HTML select element. A <c:out> action uses the EL expression ${'${'} to display the characters ${ and another EL expression${param.languages}to display the first value for the languages request parameter. Accessing Request HeadersYou can access request headers just as you can access request parameters, except that you use the header and headerValues implicit objects instead of param and paramValues.Like the param and paramValues implicit objects, the header and headerValues implicit objects are maps, but their keys are request header names. The header map's values are the first value specified for a particular request header, whereas the headerValues map contains arrays of all the values specified for that request header.Figure 2-6 shows a JSP page that uses the header implicit object to display all of the request headers and the first value defined for each of them. Figure 2-6. Accessing Request Headers with the header Implicit Object![]() The JSP page shown in Figure 2-6 is listed in Listing 2.15.The keys stored in the header map are request header names and the corresponding values are strings representing request header values. You can also use the headerValues implicit object to iterate over request headers, like this:
Listing 2.15 Accessing Requests Headers with the header Implicit Object
Unlike request parameters, request headers are rarely duplicated; instead, if multiple strings are specified for a single request header, browsers typically concatenate those strings separated by semicolons. Because of the sparsity of duplicated request headers, the header implicit object is usually preferred over headerValues. Accessing Context Initialization ParametersYou can have only one value per context initialization parameter, so there's only one JSTL implicit object for accessing initialization parameters: initParam. Like the implicit objects for request parameters and headers, the initParam implicit object is a map. The map keys are context initialization parameter names and the corresponding values are the context initialization parameter values.Figure 2-7 shows a JSP page that iterates over all the context initialization parameters and prints their values. That JSP page also accesses the parameters directly. Figure 2-7. Accessing Initialization Parameters with the initParam Implicit Object![]() Before we discuss the listing for the JSP page shown in Figure 2-7, let's look at the deployment descriptor, listed in Listing 2.16, which defines two context initialization parameters: com.acme.invaders.difficulty and com.acme.invaders. gameLevels.The context initialization parameters defined above are accessed by the JSP page shown in Figure 2-7 and listed in Listing 2.17.The preceding JSP page uses the <c:forEach> action to iterate over the key/value pairs stored in the initParam map. The body of that action displays each key/value pair.In the example discussed in "Accessing Request Parameters" on page 65, we accessed a request parameter by name like this: ${paramValues. languages}. In the preceding JSP page, can we access an initialization parameter in a similar fashion with the initParam implicit object? The answer is yes, but in this case we have a problem because the initialization parameter name has . characters, which have special meaning to the expression language. If we try to access the com.acme.invaders.difficulty parameter like this: ${initParam.com.acme.invaders.difficulty}, the expression language will interpret that expression as an object's property named difficulty, which is not the interpretation we want. Listing 2.16 WEB-INF/web.xml
Listing 2.17 Accessing Context Initialization Parameters
The solution to this difficulty is to use the [] A Closer Look at the [] Operator" on page 56 for more information about the [] operator. Accessing CookiesIt's not uncommon to read cookies in JSP pages, especially cookies that store user-interface-related preferences. The JSTL expression language lets you access cookies with the cookie implicit object. Like all JSTL implicit objects, the cookie implicit object is a map.[13] That map's keys represent cookie names, and the values are the cookies themselves. [13] The sole exception is the pageContext implicit object, which is not a map. Figure 2-8 shows a JSP page that reads cookie values, using the cookie implicit object. Figure 2-8. Accessing Cookies with the cookie Implicit Object![]() The JSP page shown in Figure 2-8 uses the cookie implicit object to iterate over all cookies and also accesses Cookie objects and their values directly. That JSP page is invoked with the URL /cookieCreator, which is mapped to a servlet that creates cookies. That servlet, after creating cookies, forwards to the JSP page shown in Figure 2-8. Listing 2.18 lists the Web application's deployment descriptor, which maps the URL /cookieCreator to the CookieCreatorServlet class. Listing 2.18 WEB-INF/web.xml
The CookieCreatorServlet class is listed in Listing 2.19. Listing 2.19 WEB-INF/classes/CookieCreatorServlet.java
The cookie creator servlet creates three cookies and adds them to the response before forwarding to cookies.jsp. That JSP page is listed in Listing 2.20. Listing 2.20 cookies.jsp
The preceding JSP page uses the <c:forEach> action to iterate over the entries contained in the cookie map. For each entry, the body of the <c:forEach> action displays the cookie's name and value. Notice that cookie values are accessed with the expression ${mapEntry.value.value}. The map entry's value is a cookie, which also has a value property.The rest of the JSP page accesses cookie objects and their values directly. Because the cookie names contain . characters, they cannot be used as identifiers, so the preceding JSP page uses the [] operator to directly access cookies and their values. Accessing Scoped AttributesSince we started discussing JSTL implicit objects at "Implicit Objects" on page 64, we've seen how to access four types of objects:
In addition to the specific types listed above, you can access any type of object that's stored in one of the four JSP scopes: page, request, session, or application. The expression language provides one implicit object for each scope:
Remember from our discussion in "Identifiers" on page 43 that identifiers refer to scoped variables; for example, the expression ${name} refers to a scoped variable named name. That scoped variable can reside in page, request, session, or application scope. The expression language searches those scopes, in that order, for scoped variables.The implicit objects listed above let you explicitly access variables stored in a specific scope; for example, if you know that the name scoped variable resides in session scope, the expression ${sessionScope.name} is equivalent to ${name}, but the latter unnecessarily searches the page and request scopes before finding the name scoped variable in session scope. Because of that unnecessary searching, ${sessionScope.name} should be faster than ${name}.The scope implicit objects listed abovepageScope, requestScope, sessionScope, and applicationScopeare also handy if you need to iterate over attributes stored in a particular scope; for example, you might look for a timestamp attribute in session scope. The scope implicit objects give you access to a map of attributes for a particular scope.Figure 2-9 shows a Web application that displays all of the attributes from the scope of your choosing. The top picture in Figure 2-9 shows a JSP page that lets you select a scope, and the bottom picture shows a JSP page that lists the attributes for the selected scope. Figure 2-9. Accessing Scoped Variables for a Specific Scope with the pageScope Implicit Object![]() The JSP page shown in the top picture in Figure 2-9 is listed in Listing 2.21. Listing 2.21 Choosing a Scope
The preceding JSP page creates an HTML form that lets you select a scope. That form's action is show_scope_attributes.jsp, which is listed in Listing 2.22.The preceding JSP page is passed a request parameter named scope whose value is "page", "request", "session", or "application". The JSP page creates a page-scoped variable, also named scope, and sets it to the appropriate JSTL implicit objectpageScope, requestScope, sessionScope, or applicationScopebased on the scope request parameter. Then the JSP page loops over that implicit object and displays each scoped variable's name and value. Accessing JSP Page and Servlet PropertiesNow that we've seen how to access request parameters and headers, initialization parameters, cookies, and scoped variables, the JSTL implicit objects have one more feature to explore: accessing servlet and JSP properties, such as a request's protocol or server port, or the major and minor versions of the servlet API your container supports. You can find out that information and much more with the pageContext implicit object, which gives you access to the request, response, session, and application (also known as the servlet context). Useful properties for the pageContext implicit object are listed in Table 2.6. Listing 2.22 Showing Scoped Variables for a Specific Scope
Figure 2-10. Using the pageContext Implicit Object![]() The JSP page shown in Figure 2-10 is listed in Listing 2.23. Listing 2.23 Accessing Servlet and JSP Properties
The preceding JSP page accesses request, response, session, and application properties, using the pageContext implicit object. The end of that JSP page creates a page-scoped variable named app that references the servlet context (meaning the application). That page-scoped variable is subsequently used to access the Servlet API version supported by the JSP container. Sometimes it's convenient, for the sake of readability, to store a reference to one of the objects listed in Table 2.6 on page 82 in a page-scoped variable, as does the preceding JSP page. |