JSP Expression Language
The JSP Expression Language (EL) makes it very simple to access data stored in JavaBeans. JSP EL can be used to create both arithmetic 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 EL allows you to specify an expression to represent an attribute value. The syntax of a simple expression 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 built-in JSP objects.
For example, the abovejsp:setPropertytag can be rewritten using the expression language as follows:
When the JSP compiler sees the "${}" format in an attribute, it generates code to evaluate the expression and produces a replacement for the value of the expression.
You can also use the expression language in the template text of a tag. For example,jsp:textthe tag simply inserts the text in its body into the JSP output:
Hello JSP!
Now, in thejsp:textUsing an expression in a tag body looks like this:
Box Perimeter is: ${2*box.width + 2*box.height}
You can use parentheses in EL expressions to group subexpressions. For example, ${(1 + 2) * 3} equals 9, but ${1 + (2 * 3)} equals 7.
To disable evaluation of EL expressions, use the page directive to set the isELIgnored attribute to true:
<%@ page isELIgnored ="true|false" %>
In this way, EL expressions are 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 |
|---|---|
| . | Accesses a Bean property or a map entry |
| [] | Accesses an element of an array or a list |
| ( ) | Groups a subexpression to change precedence |
| + | Addition |
| - | Subtraction or negation |
| * | Multiplication |
| / or div | Division |
| % or mod | Modulo |
| == or eq | Tests whether equal |
| != or ne | Tests whether not equal |
| < or lt | Tests whether less than |
| > or gt | Tests whether greater than |
| <= or le | Tests whether less than or equal to |
| >= or ge | Tests whether greater than or equal to |
| && or and | Tests logical AND |
| || or or | Tests logical OR |
| ! or not | Tests negation |
| empty | Tests whether null |
Functions in JSP EL
JSP EL allows you to use functions in expressions. These functions must be defined in a custom tag library. The syntax for using a function is as follows:
${ns:func(param1, param2, ...)}
ns refers to the namespace, func refers to the function name, param1 refers to the first parameter, param2 refers to the second parameter, and so on. For example, the function fn:length, defined in the JSTL library, 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 use the
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, collection of strings |
| header | HTTP headers, string |
| headerValues | HTTP headers, collection of strings |
| initParam | Context initialization parameters |
| cookie | Cookie values |
| pageContext | The pageContext of the current page |
You can use these objects in expressions just like variables. A few examples will be given below 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 in by the request object, do this:
${pageContext.request.queryString}
Scope Objects
The pageScope, requestScope, sessionScope, and applicationScope variables are used to access variables stored at each scope level.
For example, if you need to explicitly access the box variable at the applicationScope level, you can do it like this: applicationScope.box.
The param and paramValues Objects
The param and paramValues objects are used to access parameter values through the request.getParameter and request.getParameterValues methods.
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";
%>
<% out.print(title); %>
<% out.print(title); %>
${param["username"]}
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 through the request.getHeader and request.getHeaders methods.
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";
%>
<% out.print(title); %>
<% out.print(title); %>
${header["user-agent"]}
The running result is as follows:

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