[ Team LiB ] Previous Section Next Section

Literals and Operators

One of the strengths of JSP EL is that it doesn't just access variable values, it allows you to create expressions—both arithmetic and logical. Within a JSP EL expression, you can use integers, floating point numbers, strings, the built-in constants true and false for boolean values, and null.

One of the tricky parts about using strings is that you need to be careful about your quotation marks. For example, the following expression generates an error:


<c:out value="${fn:length("This is a test string")}"/>

Although you may not know yet what the <c:out> tag or the fn:length functions do yet, you might still be able to spot the error. The problem is that JSP compiler sees the quote just after length( as the closing quote for the value attribute. It interprets the expression value="${fn:length(" and then complains about syntax errors in the rest of the expression. There are at least two things you can do to remedy this situation. The first is to use different kinds of quotes around the whole attribute value and for the string within. Either of the two following lines will work and are equivalent:


<c:out value='${fn:length("This is a test string")}'/>

<c:out value="${fn:length('This is a test string')}"/>

Notice that in each case, single quotes define one string, and double quotes define the other. If you want to stick with the same kind of quotes, you must use a backslash (\) to escape the nested quotes. For example,


<c:out value="${fn:length(\"This is a test string\")}"/>

If you need to include a backslash within a string, you must escape it as well by using \\. For example,


<c:out value="${fn:length(\"The backslash looks like \\.\")}">

Access Operators

The most common operators in JSP EL are . and []. These two operators allow you to access various attributes of Java Beans and built-in JSP objects. In Java, the [] operator is only used for referencing array elements. In other languages, however, [] can be used to access dictionary values (the equivalent of a Java Map or Hashtable). Where JSP EL lets you use the expression table["person"], Java requires you to specify table.get("person").

The . operator usually references attributes of a bean or a built-in object. For example, to access the name property of a person bean, you use the expression person.name.

One of the interesting things about the . and [] operators is that they are interchangeable. That is, person.name is the same as person["name"]. Even if person isn't a dictionary type object (Map, Hashtable, and so on), you can still treat it that way.

If you assume that the expression var.attr is treated as var["attr"], then you can define the behavior of both these operators in terms of how the [] operator behaves.

The rules for evaluating var["attr"] are

  • If var is null, the result of the expression is null (without the agony of a NullPointerException).

  • If attr is null (which is possible because attr can be an expression instead of a constant string), the result is null.

  • If var is a Map, the result is var.get(attr), and is null if var has no key corresponding to attr.

  • If var is a List or an array, attr is evaluated as an integer. If attr can't be evaluated as an integer, the result is an error. Otherwise, the result is item in the array or List corresponding to the value of attr. That is, if attr = 0, the result is the first item in the list or array. If attr is outside the bounds of the array or list, the result is null.

  • If var is a JavaBean and attr is a readable property of var, the result is the value of that property. Otherwise, the result is an error.

In simpler terms, to access an item in a map, use the expression mapvar[mapkey], to access an item in an array or List, use the expression listvar[index]; to access a JavaBean property, use the expression beanvar[propname].

Arithmetic Operators

JSP EL supports the common arithmetic operators +, -, *, /, and % just as they are supported in Java. Because JSP EL was designed to be somewhat consistent with both the XPath and ECMAScript languages, you can also use div instead of / for division, and mod instead of % for modulo (remainder).

Within any arithmetic or logical expression, you can also use parentheses to group items. For example: ${(x + y) * (z - q)}.

You can also use the unary – operator, which negates a value. That is, you can use the expression -x to represent the negative of the value of x.

Relational Operators

JSP EL supports the common relational operators ==, !=, <, >, <=, and >= just as in Java. Again, to maintain consistency with XPath and ECMAScript, JSP EL allows the following alternatives to these operators: eq, ne, lt, gt, le, and ge (for equal, not equal, less-than, greater-than, less-or-equal, and greater-or-equal, respectively).

For example, the expression ${x < 5} evaluates to true if x is less than 5, and false otherwise.

Logical Operators

The logical operators && and || perform logical AND and OR operations just as they do in Java expressions. You can also use the operators and and or instead of && and ||. For example: ${x < 5 && y < 10}.

You can also use the unary ! operator or not to express the logical negative of an expression. That is, if x is true, then !x is false, as is not x.

The empty Operator

The empty operator returns true if a value is null or if it is an empty container, array, or string. A container is empty if it contains no elements. A string or an array is empty if its size is 0. For example: ${empty employees}.

The Conditional Operator

The conditional operator takes the form bool-expr ? true-expr : false-expr. The evaluator first evaluates bool-expr, and if it is true, the result of the operator is the value of true-expr. Otherwise, if bool-expr is false, the result is false-expr. Expressed in Java terms, the operator works something like this:


if (boolExpr) {
    return trueExpr;
} else {
    return falseExpr;
}

For example, suppose you want to calculate an average and you have a variable called sum containing the sum of the items, and a numItems variable containing the number of items. If numItems is 0, computing the average as sum/numItems would normally yield an error. If you want the average to be 0 when numItems is 0, and sum/numItems in all other cases, you can use the following expression:


${numItems != 0 ? sum/numItems : 0}
    [ Team LiB ] Previous Section Next Section