Expression Language

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 following form:

${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>Code language: HTML, XML (xml)

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 the 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 EL’s operators:

OperatorAlternativeMeaning
[] Collection member access
· Property  access
() Grouping
 Unary Negation
!notLogical not
Empty Empty test
* Multiplication
/divDivision
%modModulo or division remainder
+ Addition
 Subtraction
<ltLess than
>gtGreater than
<=leLess than or equal
>=geGreater than or equal
==eqEquality
!=neInequality
&&andLogical and
||orLogical or
= Assignment
?: Conditional operator