Expression Language in JSP
Last Updated :
08 Dec, 2023
Expression Language was introduced in JSP 2.0, it is a simplified language that is used to access and manipulate data within JSP pages also it provides a convenient way to access the data stored in objects like requests, sessions, applications, JavaBeans, etc.
We know there are n number of objects that store data, and if we want to retrieve that data then we'll have to write a few lines of code. If we have an object request we want to retrieve data from this object, so for this-
- use request.getParameter() says we have an object session
- If we want to retrieve data from the session, then we will use the session.getAttribute() but this would require extra code.
Accessing data from objectsHere comes the use of Expression Language, with Expression Language we can directly access the data easily.
Necessity of Expression Language :
- 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, ${ expression}. It can also include literals.
Syntax
It represents a placeholder which represents that the value of the specified expression should be dynamically inserted at runtime
${expression}
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)
3. Using Expression Language in forms:
Arithmetic Expression
This code snippet demonstrates the use of EL for performing arithmetic operations. It evaluates the expression 10+90
and 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
Example 2:
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.
Similar Reads
JSP | Expression tag
Expression tag is one of the scripting elements in JSP. Expression Tag in JSP is used for writing your content on the client-side. We can use this tag for displaying information on the client's browser. The JSP Expression tag transforms the code into an expression statement that converts into a valu
2 min read
Spring - Expression Language (SpEL)
Spring Expression Language (SpEL) is a feature of the Spring framework that enables querying and manipulating object graphs at runtime. It can be used in both XML and annotation-based configurations, offering flexibility for developers. Dynamic Expression Evaluation: SpEL allows evaluation of expres
6 min read
Block Lambda Expressions in Java
Lambda expression is an unnamed method that is not executed on its own. These expressions cause anonymous class. These lambda expressions are called closures. Lambda's body consists of a block of code. If it has only a single expression they are called "Expression Bodies". Lambdas which contain expr
4 min read
Java Lambda Expressions
Lambda expressions in Java, introduced in Java SE 8. It represents the instances of functional interfaces (interfaces with a single abstract method). They provide a concise way to express instances of single-method interfaces using a block of code.Key Functionalities of Lambda ExpressionLambda Expre
5 min read
What is Angular Expression ?
Angular is a great, reusable UI (User Interface) library for developers that help in building attractive, and steady web pages and web application. In this article, we will learn about Angular expression. Table of Content Angular ExpressionDifferent Use Cases of Angular ExpressionsSyntaxApproach Ang
4 min read
Custom Tags in JSP
Custom tags are user-defined action tags that can be used within Java Server Pages. A tag handler is associated with each tag to implement the operations. Therefore, it separates the business logic from JSP and helps avoid the use of scriptlet tags. The scriptlet tag embeds java code inside the JSP
8 min read
JSP - Include Action Tag
JavaServer pages in Java provide a powerful way to build web pages. The <jsp:include> action tag in JSP consists of the content of one JSP page into another JSP page at the request time, not during the translation phase. This approach allows developers to reuse common elements across multiple
2 min read
JSP | Declaration Tag
Declaration tag is one of the scripting elements in JSP.This Tag is used for declare the variables. Along with this, Declaration Tag can also declare method and classes. Jsp initializer scans the code and find the declaration tag and initializes all the variables, methods, and classes. JSP container
2 min read
JSP - Custom URI in Custom Tag
JavaServer Pages (JSP) provide a dynamic environment for developing various online applications. Setting the custom Uniform Resource Identifiers (URIs) for custom tags opens up the way for management and organization. While the default pairing of JSP custom tags with URIs based on Java package struc
6 min read
Introduction to Spring Security Expressions
Spring Security expressions offer a powerful way to secure applications by using expressions that evaluate security constraints at runtime. These expressions are integrated into the Spring Security framework, allowing for fine-grained access control directly in the application's configuration. Prere
3 min read