Datasheet

Patrick c01.tex V3 - 09/18/2009 12:15pm Page 17
Chapter 1: Building Web Applications in WebLogic
string within the braces as an expression that should be parsed and treated as a request for data from
some source available to the page.
By specifying
requestScope
in the expression, we are indicating that the tag should look only in the
HttpServletRequest
object, and by specifying
employeeNum
we are telling the tag which parameter or
attribute we want from the request object. The tag will find the object located in the request under this
key, invoke
toString()
on it, and place the result in the output. The users will, hopefully, see a valid
employee number on their web page.
As a second example, consider the following tag invocation:
<c:out value="${employee.myAddress.line1}" />
This example does not specify a source scope (
requestScope, sessionScope,
and so on), and has
three parts. The tag implementation will perform the following steps as it parses and processes this
expression:
It will first search within all of the scopes available to it (starting with
pageScope
and ending
with
applicationScope
) for some attribute stored using
employee
as the key. Let’s assume it
finds an object of the class
CompanyEmployee
located in the
HttpSession
stored with this key.
The tag will then examine the retrieved
CompanyEmployee
object and attempt to invoke a
get
method based on the next identifier in the expression. In this case the method attempted would
be
getMyAddress()
. Assuming such a method exists, the underlying
Address
object is extracted
and the processing continues.
The tag will next attempt to invoke
getLine1()
on the
Address
object and extract the resulting
object, most likely a simple
String
object in our example.
Finally, the tag will invoke
toString()
on the object returned by
getLine1()
to obtain the text
that should be placed in the HTML response output.
This chaining of identifiers using the dot operator is very common in JSTL attribute values. It allows
access to specific nested properties within objects stored on the request or session with a simple,
compact syntax.
Entries in a
List
or
Map
object can be accessed with the same dot operator or through the use of an
alternate bracket syntax as shown in these two equivalent tag invocations:
<c:out value="${sessionScope.stateNames.NY}" />
<c:out value="${sessionScope.stateNames["NY"]}" />
Hard-coding the map’s key into the JSP page is clearly of limited value, and it too can be replaced by an
expression that returns the key to be used in the
Map
lookup. The following example replaces the fixed
value of
NY
with the value passed in through a request parameter called
stateCode
:
<c:out value="${sessionScope.stateNames[param.stateCode]}" />
In this example we look in the session for the
Map
, and look in the passed-in request parameters for
the key. These are two examples of implicit objects that are available within the Expression Language.
Table 1-4 presents a complete set of these implicit objects.
17