| This Chapter | |
| - | Chapter 6: The Expression Language |
| - | The Expression Language Syntax |
| - | Accessing JavaBeans |
| - | EL Implicit Objects |
| - | Using Other EL Operators |
| - | The EL in JSP 2.0 |
| - | Summary |
In addition to the . and [] operators, the EL also provides several other operators: arithmetic operators, relational operators, logical operators, the conditional operator, and the empty operator. Using these operators, you can perform various operations. However, because the aim of the EL is to facilitate the authoring of script-free JSPs, these EL operators are of limited use, except for the conditional operator.
The EL operators are given in the following subsections.
There are five arithmetic operators:
The division and remainder operators have two forms, to be consistent with XPath and ECMAScript.
Note that an EL expression is evaluated from the highest to the lowest precedence, and then from left to right. The following are the arithmetic operators in the decreasing lower precedence:
* / div % mod + -
This means that *, /, div, %, and mod operators have the same level of precedence, and + has the same precedence as - , but lower than the first group. Therefore, the expression
${1+2*3}
results in 7 and not 6.
The following is the list of relational operators:
For instance, the expression ${3==4} returns false, and ${“b”<”d”} returns true.
Here is the list of logical operators:
The EL conditional operator has the following syntax:
${statement? A:B}
If statement evaluates to true, the output of the expression is A. Otherwise, the output is B.
For example, you can use the following EL expression to test whether the HttpSession object contains the attribute called loggedIn. If the attribute is found, the string “You have logged in” is displayed. Otherwise, “You have not logged in” is displayed.
${(sessionScope.loggedIn==null)? "You have not logged in" :
"You have logged in"}
The empty operator is used to examine whether a value is null or empty. The following is an example of the use of the empty operator:
${empty X}
If X is null or if X is a zero-length string, the expression returns true. It also returns true if X is an empty Map, an empty array, or an empty collection. Otherwise, it returns false.