![]() |
VOOZH | about |
Expression Language (EL) is a feature introduced in JSP 2.0 to simplify accessing data in JSP pages. It allows developers to retrieve values from objects like request, session, and application without writing Java code. EL improves readability and reduces the use of scriptlets.
Syntax:
${expression}
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}
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}
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:
Output:
GeeksforGeeksAfter Expression Language:
Output:
GeeksforGeeksThis 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:
Output:
GeeksforGeeksAfter Expression Language:
output:
GeeksforGeeksSome implicit objects in Expression Language are:
Expression Language also works for
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.
output:
100These keywords cannot be used as variable names in Expression Language: ge, ne, lt, le, gt, eq, true, false, null, and, or, div, not, instanceof, mod, empty.
Output:
Hey, Shekhar
Age, 20
yes You are eligible to vote.