Expression Language

The expression language of JSP is used to access the data that is stored in the JavaBeans components. We can create both arithmetic and logical expressions by using JSP expression language. We can use integers, floating point numbers, strings, Boolean and null values within the JSP expression language. The expression language was originally added to the JSP 2.0. We use the JSP expression language to make scriptless JSP pages. The following is the syntax of JSP expression language:

SYNYAX:

$ {expression}

In the above syntax the expression is any valid expression of JSP expression language. An expression can be a combination of more than one expression and can contain static values or static text.

 

How EL expression is used?

In a JSP page we can use EL expression in two different ways:

  1. The EL expression can be used as the attribute values for the custom or standard tags. Consider the following example in which we have used the EL expression as the attribute value of the attribute page of action tag jsp: include:

< jsp: include page = “${count}” >

In this example the variable count is defined in the JSP page. The EL expression can also be used with the jsp: setProperty tag to set values for properties.

Consider the following example in which we have a bean named as “Rectangle” and it has properties length and width and area.

< jsp: setProperty name = “rectangle” property = “area” value = “${rectangle. length * rectangle. width } ” / >

  1. The EL expression is also used to output in the HTML tag. Consider the following example:

<h1 > Hello ${name} </ h1>

We can also deactivate the evaluation of EL expression by using the attribute of page directive that is isELIgnored. This can be done as follows:

< % @ page isELIgnored = “true|false” % >

 

EL Implicit Objects:

The following are the implicit objects of JSP expression language with description:

Implicit Objects Description
pageContent This object is used to represent the object of PageContext.
pageScope Through this object we can have the access to any variable that is set in the Page scope.
requestScope Through this object we can have the access to any variable that is set in the Request scope.
sessionScope Through this object we can have the access to any variable that is set in the Session scope.
applicationScope Through this object we can have the access to any variable that is set in the Application scope.
param This object is used to map a request parameter to a single variable.
paramValues This object is used to map a request parameter to an array of string values.
header This object is used to map the header names and string values.
headerValues This object is used to map the header names and the array of string values.
cookie This object is used to map the cookie names and string values.

 

Example of EL:

Consider the following example to demonstrate the use of EL expression and we have created a JSP page and it will use the request parameter to get the user name from an HTML page and the HTML page will get the name of the user from the user.

HTML page:

In the HTML page we will be creating a form to get the name of the user:

< form method = “POST” action = “asd. jsp” >

    Username  < input type = “text” name = “name” >

< input type = “submit” value = “Enter” >

</ form >

The input type of the form is text and when the user enters his name he will submit the form by clicking on the enter button. The values will then be passed to asd.jsp when the enter button is clicked by making a post request as we have used the post method that is method = “POST”.

JSP page:

< html >

< head >

<title > JSP Page </ title >

</ head >

< body >

< h1 > Name of the user: ${param. name} </ h1 >

</ body >

</ html >

In the JSP page the name of the user will be get from the HTML page and for this purpose we used the EL expression.

 

Arithmetic Operations available in EL:

The following are the operations that are available in EL (expression language):

Arithmetic Operation Operators
Addition +
Subtraction
Multiplication *
Division / and div
Remainder % and mod

 

Logical and Relational Operators available in EL:

The following are the logical and relational operators that are available in expression language:

Logical and Relational Operator Operator
Equals == and eq
Not equals != and ne
Less than < and lt
Greater than > and gt
Greater than or equal >= and ge
Less than or equal < = and le
and && and and
or || and or
not ! and not