Java Notes
Complete guide to JSP Expression Language (EL) - syntax, operators, implicit objects, accessing beans, collections, and EL functions for clean JSP development.
What is Expression Language?
Expression Language (EL) is a simple scripting language used in JSP to access data stored in JavaBeans, request/session attributes, and other sources without writing Java code. EL uses the syntax ${expression}.
EL Syntax
Basic Syntax
${expression} <%-- Immediate evaluation --%>
#{expression} <%-- Deferred evaluation (used in JSF) --%>Accessing Properties
EL Operators
Arithmetic Operators
${10 + 5} <%-- 15 --%>
${10 - 5} <%-- 5 --%>
${10 * 5} <%-- 50 --%>
${10 / 5} <%-- 2 (or use: ${10 div 5}) --%>
${10 % 3} <%-- 1 (or use: ${10 mod 3}) --%>Comparison Operators
${age == 18} <%-- or: ${age eq 18} --%>
${age != 18} <%-- or: ${age ne 18} --%>
${age > 18} <%-- or: ${age gt 18} --%>
${age < 18} <%-- or: ${age lt 18} --%>
${age >= 18} <%-- or: ${age ge 18} --%>
${age <= 18} <%-- or: ${age le 18} --%>Logical Operators
Empty Operator
${empty name} <%-- true if name is null, "", empty collection/array --%>
${not empty users} <%-- true if users has elements --%>
${empty param.search} <%-- true if no search parameter --%>Ternary Operator (EL 3.0+)
${user.active ? "Active" : "Inactive"}
${empty cart ? "Empty Cart" : cart.size().toString().concat(" items")}EL Implicit Objects
| Object | Type | Description |
|---|---|---|
pageScope | Map | Page-scoped attributes |
requestScope | Map | Request-scoped attributes |
sessionScope | Map | Session-scoped attributes |
applicationScope | Map | Application-scoped attributes |
param | Map | Request parameters (single value) |
paramValues | Map | Request parameters (array) |
header | Map | HTTP headers (single value) |
headerValues | Map | HTTP headers (array) |
cookie | Map | Cookies |
initParam | Map | Context init parameters |
pageContext | PageContext | Access to request, session, etc. |
Using Implicit Objects
EL Scope Resolution
When you write ${user}, EL searches in this order:
- pageScope — Page attributes
- requestScope — Request attributes
- sessionScope — Session attributes
- applicationScope — Application attributes
To be explicit:
${requestScope.user} <%-- only look in request --%>
${sessionScope.user} <%-- only look in session --%>Accessing JavaBeans with EL
JavaBean Class
package com.example;
public class Student {
private String name;
private String email;
private int age;
private Address address;
private List<String> courses;
// Getters and setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public Address getAddress() { return address; }
public void setAddress(Address address) { this.address = address; }
public List<String> getCourses() { return courses; }
public void setCourses(List<String> courses) { this.courses = courses; }
}Servlet Sets the Bean
Student student = new Student();
student.setName("Rahul");
student.setEmail("rahul@example.com");
student.setAge(22);
student.setCourses(Arrays.asList("Java", "Spring", "React"));
request.setAttribute("student", student);
request.getRequestDispatcher("/profile.jsp").forward(request, response);JSP Accesses with EL
EL with Collections
EL 3.0+ Features (Java EE 7+)
Lambda Expressions
Collection Operations
String Operations
${"Hello World".toUpperCase()}
${"Hello World".substring(0, 5)}
${"Hello World".replace("World", "Java")}Disabling EL
<%-- Disable EL for entire page --%>
<%@ page isELIgnored="true" %>
<%-- Or in web.xml for all pages --%>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>Complete Example: Product Listing Page
Servlet (Controller)
@WebServlet("/products")
public class ProductServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Product> products = productService.getAllProducts();
request.setAttribute("products", products);
request.setAttribute("totalCount", products.size());
request.setAttribute("category", request.getParameter("cat"));
request.getRequestDispatcher("/products.jsp").forward(request, response);
}
}JSP (View) with EL
<%@ taglib uri="jakarta.tags.core" prefix="c" %>
<%@ taglib uri="jakarta.tags.fmt" prefix="fmt" %>
<%@ taglib uri="jakarta.tags.functions" prefix="fn" %>
<h2>Products (${totalCount} found)</h2>
<c:if test="${not empty category}">
<p>Filtered by: ${fn:toUpperCase(category)}</p>
</c:if>
<c:choose>
<c:when test="${empty products}">
<p>No products found.</p>
</c:when>
<c:otherwise>
<table>
<c:forEach var="product" items="${products}" varStatus="s">
<tr class="${s.index % 2 == 0 ? 'row-even' : 'row-odd'}">
<td>${s.count}</td>
<td>${product.name}</td>
<td><fmt:formatNumber value="${product.price}" type="currency" /></td>
<td>${product.inStock ? 'Available' : 'Out of Stock'}</td>
</tr>
</c:forEach>
</table>
</c:otherwise>
</c:choose>Interview Key Points
- EL uses
${expr}syntax to access data without Java code - EL is null-safe — returns "" instead of NullPointerException
- Scope search order: page → request → session → application
${bean.property}calls the getter method automaticallyemptyoperator checks null, "", empty collections/arrays- EL 3.0 supports lambdas and stream operations
- Use bracket notation for dynamic property names:
${bean[varName]}
Summary
In this chapter, we learned about Expression Language (EL) in detail. Here are the key points:
- Basic introduction to the concept and why it is needed
- Syntax and structure with complete examples
- Internal working and best practices
- Real-world applications and use cases
- Common mistakes and how to avoid them
- How this topic is asked in interviews
To practice these concepts, write and run the code in your IDE and verify the output. Modify each example and experiment so that you deeply understand the concept.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Expression Language (EL).
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java Master Course topic.
Search Terms
java-master-course, java master course, java, master, course, servlets, jsp, expression
Related Java Master Course Topics