JSP Expression Language (EL) is a simple and powerful way to access Java objects in JSP pages without writing Java code (no <% %> needed).
- It allows you to access request attributes, session attributes, application attributes, JavaBeans properties, collections, and even call simple functions.
- Introduced in JSP 2.0 to replace scriptlets for reading data.
✅ It makes your JSP pages cleaner, easier to maintain, and more readable.
🔥 Syntax of EL
${expression}
${} is used to evaluate the expression at runtime.
Example:
<p>Hello, ${user.name}!</p>
Here, user is an attribute (probably stored in request/session), and .name accesses its getName() property.
🔵 Key Features of EL
| Feature | Description |
|---|---|
| Simplified access | Access objects easily without Java code |
| Automatic scope search | EL searches page, request, session, application scopes automatically |
| Bean property access | Call getters automatically (user.name → user.getName()) |
| Collection access | Access arrays, lists, maps using [ ] syntax |
| Operators support | Logical, arithmetic, relational operators |
| Functions support | Call custom or standard functions via JSTL |
🛠️ Examples of EL Usage
1. Accessing Bean Properties
${user.name}
${user.age}
Will call getName() and getAge() methods of user bean.
Accessing Maps/Lists/Arrays
${productList[0].name}
${userMap["admin"].email}
Access elements inside lists and maps.
Using Logical/Relational/Arithmetic Operators
${age > 18}
${price * quantity}
${empty shoppingCart}
EL supports operators like +, -, *, /, %, >, <, ==, !=, and, or, empty.
4. Scope-Specific Access
You can force EL to look in a particular scope:
${sessionScope.user}
${requestScope.order}
${applicationScope.globalSetting}
${pageScope.tempData}
Normally, EL automatically searches in this order:
- page
- request
- session
- application
5. Handling Null Values
If a property or variable does not exist:
- EL will return an empty string
""(not throw a NullPointerException).
Example:
<p>${nonExistentVar}</p> <!-- Will output nothing -->
⚡ Why Use EL?
| Reason | Explanation |
|---|---|
| No Java code in JSP | Cleaner, more readable code |
| Separation of concerns | JSP focuses on display only |
| Automatic handling | Nulls, missing properties are handled gracefully |
| Supports JSTL | Works beautifully with JSTL tags (<c:if>, <c:forEach>, etc.) |
🎯 Final Short Summary:
| Term | Meaning |
|---|---|
| EL | A powerful language for accessing server-side objects from JSP |
| Syntax | ${expression} |
| Goal | Make JSP pages clean, logic-free, and purely for presentation |
📢 One more important point:
- You cannot write complicated Java logic inside EL (like loops, file operations, etc.).
- EL is only for simple evaluations — business logic must stay in Java classes, services, or controllers!