Expression Language in JSP
Last Updated :
01 Aug, 2025
Expression Language (EL) was introduced in JSP 2.0 as a simpler way to access and manage data in JSP pages. It provides an easy syntax to retrieve values from commonly used objects like request, session, application and JavaBeans.
Normally, to get data from these objects, we need to write extra code. For example:
1. To get data from a request, we write.
request.getParameter("name")
2. To get data from a session, we write.
session.getAttribute("user")
This makes the code longer and harder to read. But with Expression Language, we can do the same in a much simpler way:
${param.name}
${sessionScope.user}
Accessing data from objectsHere comes the use of Expression Language, with Expression Language we can directly access the data easily.
Why Use Expression Language (EL)?
- Expression Language makes the data access work much easier.
- The data stored in the objects can be directly retrieved with the help of the Expression Language
- The data is stored in the JavaBeans Component.
- It allows to access a bean by following a short syntax, It can also include literals.
EL Syntax
It represents a placeholder which represents that the value of the specified expression should be dynamically inserted at runtime
${expression}
Common Usage Patterns
1. Variable Access:
Used to access the value of a variable
${variableName}
${xxxScope.variableName}
2. Property Access:
Used to access the value of a property
${object.property}
3. Arithmetic and Logical Operators:
Performs arithmetic or logical operations on numbers
${num1>num2}
${num1+num2}
4. Conditional Expressions:
Evaluate a condition and return one of two values.
${condition ? value1 : value2 }
5. Collection Iterator :
Iterates over a collection and prints each item
<c:forEach var="item" items = "${collection}" > ${item} </c:forEach >
6. Accessing Request Parameters:
Accesses the value of a request parameter
${param. parametersName}
How to use Expression Language?
1. Request Object
This code snippet demonstrates how to access and display the value of a request attribute using Expression Language (EL). It sets the request attribute request_name to "GeeksforGeeks" and then displays its value using EL within an HTML element.
Before Expression Language:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Expression Language</title>
</head>
<!--(jsp code)-->
<body>
<%
request.setAttribute("request_name", "GeeksforGeeks");
out.println(request.getAttribute("request_name"));
%>
</body>
<!--(jsp code)-->
</html>
Output:
GeeksforGeeks
After Expression Language:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Expression Language</title>
</head>
<body>
<% request.setAttribute("request_name", "GeeksforGeeks"); %>
<h3>${requestScope.request_name}</h3>
</body>
</html>
Output:
GeeksforGeeks
2. Using the instanceof session as object
This code snippet showcases how to access and display the value of a session attribute using EL. It sets the session attribute request_name to "GeeksforGeeks" and then displays its value using EL within an HTML element.
Before Expression Language:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Expression Language</title>
</head>
<body>
<%
session.setAttribute("request_name", "GeeksforGeeks");
out.println(session.getAttribute("request_name"));
%>
</body>
</html>
Output:
GeeksforGeeks
After Expression Language:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Expression Language</title>
</head>
<body>
<% request.setAttribute("request_name", "GeeksforGeeks"); %>
<h3> ${requestScope.request_name} </h3>
</body>
</html>
output:
GeeksforGeeks
Some implicit objects in Expression Language are:
- requestScope: contains attributes that are specific to the current HTTP request.
- param: provides access to request parameters sent to the server.
- paramValues: provides access to request parameter values as an array.
- sessionScope: Contains attributes that are specific to the current user session.
- pageContent: Provides access to various objects and methods related to the JSP page content.
- pageScope: Contains attributes that are specific to the current JSP Page.
- applicationScope: Contains attributes that are specific to the entire web application, etc.
Expression Language also works for
- Arithmetic Operators ( +, - , * , / , % )
- Logical Operators ( &&, ||, ! )
- Relational Operators (==, !=, >, <, <=, >= )
- Conditional Operator ( ? )
- Empty Operator (empty, used to check if a value is empty, such as null or an empty collection/string)
Arithmetic Expression
This code snippet demonstrates the use of EL for performing arithmetic operations. It evaluates the expression10+90and displays the result inline within an HTML element.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
${10+90}
</body>
</html>
output:
100
Reserved words for Expression language:
list of reserved words in EL that should not be used as variable names or identifiers within EL expressions:
- ge
- ne
- lt
- le
- gt
- eq
- true
- false
- null
- and
- or
- div
- not
- instanceof
- mod
- empty
This code snippet demonstrates the use of EL for conditional expressions. It evaluates the condition user.age >= 18 and assigns the value yes or no to the variable vote accordingly.
Then, it displays the value vote within an HTML element along with the corresponding message.
HTML
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>Expression Language</title>
</head>
<body>
<h1>Hey, ${user.name}</h1>
<p>Age, ${user.age}</p>
<c:set var="vote" value="${user.age >= 18 ? "yes" : "no"}" />
<p>${vote} You are eligible to vote.</p>
</body>
</html>
Output:
Hey, Shekhar
Age, 20
yes You are eligible to vote.
Explore
Java Enterprise Edition
Multithreading
Concurrency
JDBC (Java Database Connectivity)
Java Frameworks
JUnit