The following table summarizes all operators used by the JSP Expression Language (EL):

Category

Operators

Variable

.and []

Arithmetic

+,-(binary), *, / and div, %and mod,-(unary)

Logical

and, &&, or,||,not,!

Relational

==, eq, !=, ne, <, lt, >, gt, <=, le, >=, ge

Conditional

A ? B : C

Empty/Null checking

empty

Let’s see each operator in details with usage examples.

 

1. Variable Operators

In EL, the dot (.) operator is used to access properties of a JavaBean object or get value of a key in a Map object. Syntax is:

${objectName.propertyName}

${mapObject.keyName}

 

For example:

${param.username}

${header.host}

${userBean.email}

The [] operator does same thing as the . operator, however, it can be also used to access elements of an array or a list by specifying an index. For example:

${param["username"]}

${header["host"]}

${userBean["email"]}

${jobTitles["1"]}

${listUsers[1].email}

It’s also possible to access nested property like this:

${userBean.currentJob.title}

 

2. Arithmetic Operators

The following table describes all the arithmetic operators as well as examples:

Operator

Meaning

EL Expression Example

Result

+

Addition

${100 + 200}

300

- (binary)

Subtraction

${100 - 200}

-100

*

Multiplication

${9 * 9}

81

/ or div

Division

${200 / 100}

${200 div 100}

${200 / 0}

2.0

2.0

Infinity

% or mod

Remainder

${2013 % 100}

${2013 mod 100}

${200 mod 0}

13

13

Exception

- (unary)

Negation

${-20}

${20 - (-20)}

-20

40



Note that while the division operator can divide by zero, the remainder operator not. If we try this:

${200 mod 0}

Then this exception will be thrown:

java.lang.ArithmeticException: / by zero

Also note that in an arithmetic expression, a null is treated as zero. For example in the following expression:

${user.age + 50}

If the variable user or age evaluates to null, then result of the expression is 50.

 

3. Logical Operators

The following table describes the logical operators along with examples:

Operator

Meaning

EL Expression Example

Result

and, &&

Logical AND

${true && false}

${false and false)

false

false

or, ||

Logical OR

${true || false}

${false or false}

true

false

not, !

Unary boolean complement

${!false}

${!true}

true

false

Note that in a logical expression, a null is treated as false. Consider the following example:

${user.logged && true}

If the variable user or logged evaluates to null, then result of the expression is false.

 

4. Relational Operators

The following table describes the relational operators as well as examples:

Operator

Meaning

EL Expression Example

Result

==, eq

Equality test

${100 == 100}

${101 eq 102}

true

false

!=, ne

Un-equality test

${100 != 200}

${200 ne 200}

true

false

<, lt

Less than test

${9 < 10}

${9 lt 8}

true

false

>, gt

Greater then test

${10 > 9}

${10 gt 20}

true

false

<=, le

less than or equal test

${100 <= 200}

${100 le 100}

true

true

>=, ge

greater than or equal test

${100 >= 200}

${100 ge 100}

false

true

Note that there is no = operator in EL.

 

5. Conditional Operators

The conditional operators ? and : must participate in this ternary form:

A ? B : C

If A evaluates to true, then evaluate B, else evaluate C. For example:

${(8 + 9) > 10 ? (11 + 12) : (13 + 14) }

Result: 23

 

6. Empty Operator

The empty operator checks a value is null or empty. Syntax:

${empty value}

It returns true if value is null or empty, and returns false otherwise. For example:

${empty param.username}

The above expression returns true if the parameter username is null or a empty String. Another example:

${empty listUsers}

The variable listUsers is an ArrayList, the expression returns true if the list has no element (empty).

 

7. Operators Precedence

Following is the precedence of operators, with priority descends from top to bottom and from left to right in a row:

Examples:

EL Expression Example

Result

${2 * 4 + 3 * 4}

20

${8 + 10 / 5 - 3}

7

${5 > 3 && 4 > 6}

false

${1 + 2 > 3 ? 4 * 5 : 6 - 7}

-1

 

Other JSP Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.