login

Expression Language - EL

Expression Language (EL) was introduced in JSP 2.0 specification. You can do almost everything like scriptlet by using EL which is simpler to understand.

Basic syntax

The syntax of expression language is very simple. No matter where it is called, it always follows the form as bellows:

${expr}

In the syntax above, expr is an expression. When the Java compiler sees the sign ${}, it evaluates the expr and injects the result in the place where ${expr} is called. Let take a look at an example:

<html>
    <head>
        <title>Expression Language</title>
    </head>
    <body>
        <jsp:useBean id="msg"
                     class="com.jsptutorial.Message" />
        <jsp:setProperty name="msg" 
                         property="text"
                         value="this is an message" />
        <span>${msg.text}</span>

      
    </body>
</html>

In the above example, first we use the action useBean to instantiate a new object of the class Message and set its text property. Then we use ${msg.text} as an expression. When compiler sees this syntax, it evaluates the expression which is msg.text and invokes the appropriate method of the object which returns the value of text property. In this case, on the screen you will see the message "this is a message" is displayed.

Literal Values

Literal values are constants with a specific data type and they can be used in expression along with variables. There are five basic five types as follow:

  1. Boolean: true and false
  2. Integer: a combination of numbers from 0 to 9
  3. Floating Point
  4. String
  5. Null: null

Operators

Expression language supports a wide range of operators including arithmetic, relational and logical operators. EL allows you to apply those operators to the literal and variables for calculations. Here is the list of operators with its alternative version.  

Operator Alternative Meaning
[]
 
Collection member access
·
 
Property  access
()
 
Grouping
-
 
Unary Negation
!
not
Logical not
Empty
 
Empty test
*
 
Multiplication
/
div
Division
%
mod
Modulo or division remainder
+
 
Addition
-
 
Subtraction
lt
Less than
gt
Greater than
<=
le
Less than or equal
>=
ge
Greater than or equal
==
eq
Equality
!=
ne
Inequality
&&
and
Logical and
||
or
Logical or
=
 
Assignment
?:
 
Conditional operator