Open In App

Expression Language in JSP

Last Updated : 08 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.
flow of Expression Language

Accessing data from objects

Here comes the use of Expression Language, with Expression Language we can directly access the data easily.

Necessity of Expression Language :

  1. Expression Language makes the data access work much easier.
  2. The data stored in the objects can be directly retrieved with the help of the Expression Language
  3. The data is stored in the JavaBeans Component.
  4. 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:

  1. requestScope: contains attributes that are specific to the current HTTP request.
  2. param: provides access to request parameters sent to the server.
  3. paramValues: provides access to request parameter values as an array.
  4. sessionScope: Contains attributes that are specific to the current user session.
  5. pageContent: Provides access to various objects and methods related to the JSP page content.
  6. pageScope: Contains attributes that are specific to the current JSP Page.
  7. 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.




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads