JSP Expression Language

JSP Expression Language (EL) makes it very simple to access data stored in JavaBeans. JSP EL can be used to create both arithmetic expressions and logical expressions. Within JSP EL expressions, you can use integers, floating-point numbers, strings, the constants true and false, and null.


A simple syntax

Typically, when you need to specify an attribute value in a JSP tag, you simply use a string:

<jsp:setProperty name="box" property="perimeter" value="100"/>

JSP EL allows you to specify an expression to represent an attribute value. A simple expression syntax is as follows:

${expr}

Here, expr refers to the expression. The common operators in JSP EL are.and{}. These two operators allow you to access various JavaBean properties through embedded JSP objects.

For example, the <jsp:setProperty> tag above can be rewritten using expression language as follows:

<jsp:setProperty name="box" property="perimeter" 
                 value="${2*box.width+2*box.height}"/>

When the JSP compiler encounters the "${}" format in an attribute, it generates code to evaluate this expression and produces a substitute to replace the value of the expression.

You can also use expression language in the template text of tags. For example, the <jsp:text> tag simply inserts the text from its body into the JSP output:

<jsp:text>
<h1>Hello JSP!</h1>
</jsp:text>

Now, use an expression in the body of the <jsp:text> tag, like this:

<jsp:text>
Box Perimeter is: ${2*box.width + 2*box.height}
</jsp:text>

Parentheses can be used in EL expressions to group subexpressions. For example, ${(1 + 2) * 3} equals 9, but ${1 + (2 * 3)} equals 7.

To disable the evaluation of EL expressions, you need to use the page directive to set the value of the isELIgnored attribute to true:

<%@ page isELIgnored ="true|false" %>

In this way, EL expressions will be ignored. If set to false, the container will evaluate EL expressions.


Basic operators in EL

EL expressions support most of the arithmetic and logical operators provided by Java:

Operator Description
. Access a Bean property or a map entry
[] Access an element of an array or a list
( ) Group a subexpression to change precedence
+ add
- Subtraction or unary minus
* multiply
/ or div divide
% or mod Modulus
== or eq Test for equality
!= or ne Test for inequality
< or lt Test for less than
> or gt Test for greater than
<= or le Test for less than or equal to
>= or ge Test for greater than or equal to
&& or and Test logical AND
|| or or Test logical OR
! or not Test logical NOT
empty Test for empty value

Functions in JSP EL

JSP EL allows you to use functions in expressions. These functions must be defined in custom tag libraries. The syntax for using a function is as follows:

${ns:func(param1, param2, ...)}

ns refers to the namespace, func refers to the name of the function, param1 refers to the first parameter, param2 refers to the second parameter, and so on. For example, there is a function fn:length, defined in the JSTL library, that can be used to get the length of a string as follows:

${fn:length("Get my length")}

To use functions from any tag library, you need to install these libraries on the server, and then include them in JSP files using the <taglib> tag.


JSP EL implicit objects

JSP EL supports the implicit objects listed in the following table:

Implicit object Description
pageScope Page scope
requestScope Request scope
sessionScope Session scope
applicationScope Application scope
param Request object parameters, String
paramValues Request object parameters, String collection
header HTTP header, String
headerValues HTTP header, String collection
initParam Context initialization parameters
cookie Cookie values
pageContext The pageContext of the current page

You can use these objects in expressions just like variables. Several examples will be given next to better understand this concept.


The pageContext object

The pageContext object is a reference to the pageContext object in JSP. Through the pageContext object, you can access the request object. For example, to access the query string passed by the request object, do it like this:

${pageContext.request.queryString}

Scope objects

The pageScope, requestScope, sessionScope, and applicationScope variables are used to access variables stored at various scope levels.

For example, if you need to explicitly access the box variable in the applicationScope layer, you can access it like this: applicationScope.box.


The param and paramValues objects

The param and paramValues objects are used to access parameter values by using the request.getParameter method and the request.getParameterValues method.

For example, to access a parameter named order, you can use the expression: ${param.order}, or ${param["order"]}.

The following example shows how to access the username parameter in the request:

<%@ page import="java.io.*,java.util.*" %>
<%
    String title = "Accessing Request Param";
%>
<html>
<head>
<title><% out.print(title); %></title>
</head>
<body>
<center>
<h1><% out.print(title); %></h1>
</center>
<div align="center">
<p>${param["username"]}</p>
</div>
</body>
</html>

The param object returns a single string, while the paramValues object returns an array of strings.


The header and headerValues objects

The header and headerValues objects are used to access headers by using the request.getHeader method and the request.getHeaders method.

For example, to access a header named user-agent, you can use the expression: ${header.user-agent}, or ${header["user-agent"]}.

The following example shows how to access the user-agent header:

<%@ page import="java.io.*,java.util.*" %>
<%
    String title = "User Agent Example";
%>
<html>
<head>
<title><% out.print(title); %></title>
</head>
<body>
<center>
<h1><% out.print(title); %></h1>
</center>
<div align="center">
<p>${header["user-agent"]}</p>
</div>
</body>
</html>

The output is as follows:

jsp-expression-language

The header object returns a single value, while headerValues returns an array of strings.

Other extensions