Expression Language (EL)

We now turn our attention to the Expression Language. Although EL is not a general purpose programming language, it does share many characteristics with common languages with which you will be familiar. For example, while it has no flow-control it does have variables and operators.

EL Variables

EL variables are implicit JSP variables, additional EL-specific variables, and any user-defined variables. Today's discussion will focus on using implicit variables and using JavaBeans (see the section “Simplifying JSP pages with JavaBeans”). Setting variables using the JSTL is covered on Day 14.

Implicit Objects

Like JSP, EL defines a number of implicit objects that provide access to request parameters, HTTP headers, cookies, and other container information. However the EL implicit variables and the JSP implicit variables shown in Table 13.3 are different. Table 13.4 lists all the implicit objects available in EL.

Table 13.4. EL Implicit Objects.
Implicit ObjectDescriptionExample of Use
paramA Map of request parameters and their values${param.customer}
paramValuesA Map of String[] containing all the values for each request parameter${paramValues.uriList}
headerA Map of HTTP headers and their values${header.Host}
headerValuesA Map of String[] containing all the values for each HTTP header${headerValues.["Accept-Encoding"]}
cookieA Map of cookie names and cookie objects${cookie["URI-list"]}
initParamA Map of context parameters and their values${initParam.WebMaster}
pageContextA javax.servlet.jsp.PageContext object${pageContext.request.locale}

The pageContext implicit object provides access to various servlet objects including

  • request— The HTTP request object

  • response— The HTTP response object

  • session— The client session object

For a full list see the API documentation for a description of the classes for the implicit objects.

You have already seen an example of using the request object in Listing 13.3 when accessing the remote host submitting a request:

<P>Remote Host ${pageContext.request.remoteHost}</P>

Looking at this example and the last column in Table 13.4 you may have spotted that there is a general method access mechanism going on here. In JavaBeans, properties of a bean are exposed through getter and setter methods. The EL syntax uses this JavaBean naming mechanism to map identifiers to getter methods.

In the last example, request and remoteHost are properties of the PageContext and ServletRequest objects. The Java expression corresponding to the EL expression is

pageContext.getRequest().getRemoteHost()

Any object that supports JavaBean getter methods can use the EL structured dataoperators.

You must use the “[]” operator instead of the “.” operator when the property name is supplied as a runtime expression rather than as a compile-time property name. The following example picks up the value of an HTTP request parameter using the value of another request parameter called paramName.

<P>Value of ${param.paramName} is ${param[param.paramName]}</P>

It might help to understand this example seeing the equivalent JSP code:

<P>Value of <%= request.getParameter("paramName") %>
is <%= request.getParameter(request.getParameter("paramName")}) %> </P>

EL Operators

In addition to the “.” and “[]” structured operators just discussed, EL provides operators commonly found in many programming languages, but does not have assignment or a string concatenation operator. To avoid confusion with special XML characters, some EL operators have alternate forms.

EL provides the following operators:

  • Arithmetic— "+", "-", "*", "/" (also "div") "%" (also "mod")

  • Relational— "==" or "eq", "!=" or "ne", "<" or "lt", ">" or "gt", "<=" or "le", ">=" or "ge"

  • Unary minus— "-"

  • Logical— "&&" or "and", "||" or "or", "!" or "not"

  • Empty— "empty" (used to test for a null value, an empty string ("") or an empty collection)

Use of most of these operators is the same as for Java. The empty operator is used to test whether a value is empty or not and is discussed in more detail with the JSTL on Day 14.

Apart from its use for accessing object properties, the “[]” operator can also be used to access indexed elements in a Java collection. The value inside the “[]” operator must be appropriate for the type of collection.

Operator precedence is the same as Java (or C), with “[]” and “.” binding most tightly. As with Java, parentheses can be used to change the order of precedence.

String concatenation is achieved by using two or more EL expressions within the JSP or element attribute, as in

<H2>${pageContext.request.serverName}:${pageContext.request.serverPort}</H2>

which will translate to something like

<H2>localhost:8000</H2>

EL has conversion rules for operands, which help reduce the number of exceptions caused by incorrect data and allow the EL to be written like an untyped language (such as JavaScript or Visual Basic).

The basic conversion rules are as follows:

  • null is converted to "", 0, 0.0 or false as required.

  • Numbers are narrowed or widened to match the required data type (for example, double to int or int to double).

  • Primitives are converted to the appropriate wrapper class (for example int to Integer).

  • String objects are converted to numbers using the valueOf() method of the appropriate wrapper class.

  • All objects are converted to String using the java.lang.toString() method.

Generally, variables and expressions will be converted as expected.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.104.153