ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

JSTL 1.0: Standardizing JSP, Part 1
Pages: 1, 2, 3

The JSTL Expression Language

In addition to the tag libraries, JSTL 1.0 defines a so-called Expression Language (EL). The EL is a language for accessing runtime data from various sources. Its syntax is considerably more user-friendly than Java, which is the only language supported directly by the JSP 1.2 specification. All JSTL actions recognize EL expressions in their attribute values, and custom actions may be developed to do the same. It is expected that the EL will be incorporated into the next version of the JSP specification to encourage its use for data access over the Java language. If so, you will be able to use EL expressions in an action attribute value, and even in template text.



If you've used JavaScript, you should feel right at home with the EL. The EL borrows the JavaScript syntax for accessing structured data as either a property of an object (with the . operator) or as a named array element (with the ["name"] operator). JavaBeans component properties and java.util.Map entries, using the key as the property name, can be accessed this way. Here are some examples:

${myObj.myProperty}$
${myObj["myProperty"]}$
${myObj[varWithTheName]}$

As shown here, an EL expression must always be enclosed within ${ and } characters. The first two expressions access a property named myProperty in an object represented by a variable named myObj. The third expression access a property with a name that's held by a variable. Instead of a single variable, this syntax can be used with any expression that evaluates to the property name.

The array access operator is also used for data represented as a collection of indexed elements, such as a Java array or a java.util.List:

${myList[2]}$
${myList[aVar + 1]}$

In addition to the property and array element operators and the arithmetic, relational, and logical operators, a special operator for testing if an object is "empty" or not can be used in an EL expression. The following table lists all operators:

Operator Description
. Access a property
[] Access an array/list element
() Group a subexpression
+ Addition
- Subtraction or negation of a number
/ or div Division
% or mod Modulo (remainder)
== or eq Test for equality
!= or ne Test for inequality
< or lt Test for less than
> or gt Test for greater than
<= or le Test for less than or equal
>= or gt Test for greater than or equal
&& or and Test for logical AND
|| or or Test for logical OR
! or not Unary Boolean complement
empty Test for empty value (null, empty string, or an empty collection)

What you don't find in the EL are statements such as assignments, if/else, or while. Action elements are used for this type of functionality in JSP, and the EL is not intended to be a general-purpose programming language, just a data access language.

Literals and variables are, of course, also part of the language. The EL provides the following literals, similar to what you find in JavaScript, Java, and other languages:

Literal Type Description
String Enclosed with single or double quotes. A quote of the same type within the string must be escaped with backslash: (\' in a string enclosed with single quotes; \" in a string enclosed with double quotes). The backslash character must be escaped as \\ in both cases.
Integer An optional sign (+ or -) followed by digits between 0 and 9.
Floating Point The same as an integer literal, except that a dot is used as the separator for the fractional part and an exponent can be specified as e or E, followed by an integer literal.
Boolean true or false.
Null null.

Any object in one of the JSP scopes (page, request, session, or application) can be used as a variable in an EL expression. For instance, if you have an bean with a firstName property in the request scope under the name customer, this EL expression represents the value of that bean's firstName property:

${customer.firstName}

But it doesn't stop there. The EL also makes request information and general container information available as a set of implicit variables:

Variable Description
param A collection of all request parameters as a single string value for each parameter.
paramValues A collection of all request parameters as a string array value for each parameter.
header A collection of all request headers as a single string value for each header.
headerValues A collection of all request headers as a string array value for each header.
cookie A collection of all request cookies as a single javax.servlet.http.Cookie instance value for each cookie.
initParams A collection of all application init parameters as a single string value for each parameter.
pageContext An instance of the javax.servlet.jspPageContext class.
pageScope A collection of all page scope objects.
requestScope A collection of all request scope objects.
sessionScope A collection of all session scope objects.
applicationScope A collection of all application scope objects.

The first five implicit variables in the table give you access to the parameter values, headers, and cookies for the current request. Here's an example of how to access a request parameter named listType and the User-Agent header:

${param.listType}
${header['User-Agent']}

Note how you must use the array syntax for the header, because the name includes a dash; with the property syntax, it would be interpreted as the value of the variable expression header.User minus the value of the variable named Agent.

The initParameter variable provides access to init parameters that are defined for the application in the web.xml file. The pageContext variable has a number of properties that provide access to the servlet objects that represent the request, response, session, application, etc. Look at the JSP specification to learn more about these properties.

The final four variables are collections containing all objects in each specific scope. You can use these to limit the search for an object to just one scope instead of searching all scopes, which is the default if no scope is specified. In other words, if there's an object named customer in the session scope, the first two expressions here find the same object and the third comes up empty:

${customer}
${sessionScope.customer}
${requestScope.customer}

All JSTL actions accept EL expressions as attribute values, for all attributes except var and scope, because these attribute values may be used for type checking at translation time in a future version. There's one additional JSTL action attribute that does not take an EL expression value, but it's only used in the XML library, so let's ignore that for now. One or more EL expressions can be used in the same attribute value, and fixed text and EL expressions can be mixed in the same attribute value:

First name: <c:out value="${customer.firstName}" />
<c:out value="First name: ${customer.firstName}" />

Before we jump in and look at examples using the Core actions, let me qualify something I said earlier: all JSTL actions in the EL library set accept EL expressions. There's actually a parallel set of JSTL libraries, referred to as the RT library set, that only accept the old-style Java expressions:

First name: <c_rt:out value="<%= customer.getFirstName() %>" /> 

I encourage you to use the EL libraries instead, but if you're curious, you can read about the RT libraries in the JSTL spec or in my book, JavaServer Pages (O'Reilly, 2nd edition 2002).

Pages: 1, 2, 3

Next Pagearrow