JSP Expression Language (EL)
What is Expression Language?
Expression Language (EL) was introduced in JSP 2.0 to simplify access to data stored in JavaBeans, request parameters, session attributes, and other objects. EL expressions use the syntax ${expression} and can be used directly in HTML without scriptlets.
EL makes JSP pages cleaner and easier to maintain by eliminating verbose Java code like <%= (String) session.getAttribute("user") %> and replacing it with ${sessionScope.user}.
EL Implicit Objects
| EL Object | Type | Description |
|---|---|---|
param | Map<String,String> | Request parameters (single value) |
paramValues | Map<String,String[]> | Request parameters (multiple values) |
header | Map<String,String> | HTTP request headers |
headerValues | Map<String,String[]> | HTTP headers (multiple values) |
cookie | Map<String,Cookie> | Cookies from the request |
pageScope | Map | Page-scoped attributes |
requestScope | Map | Request-scoped attributes |
sessionScope | Map | Session-scoped attributes |
applicationScope | Map | Application-scoped attributes |
initParam | Map | Context init parameters from web.xml |
pageContext | PageContext | Access to JSP page context |
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%
// Set attributes in different scopes
request.setAttribute("message", "Hello from Request!");
session.setAttribute("username", "Alice");
session.setAttribute("age", 25);
application.setAttribute("appVersion", "2.0");
%>
<html><body>
<h3>Accessing Scoped Attributes</h3>
<p>Request: ${requestScope.message}</p>
<p>Session username: ${sessionScope.username}</p>
<p>Session age: ${sessionScope.age}</p>
<p>App version: ${applicationScope.appVersion}</p>
<%-- EL searches all scopes automatically (page, request, session, app) --%>
<p>Auto-search: ${username}</p>
<h3>Request Parameters</h3>
<p>Name param: ${param.name}</p>
<p>Age param: ${param.age}</p>
<h3>Headers and Cookies</h3>
<p>User-Agent: ${header['User-Agent']}</p>
<p>Cookie value: ${cookie.JSESSIONID.value}</p>
<h3>pageContext</h3>
<p>Context path: ${pageContext.request.contextPath}</p>
<p>Server name: ${pageContext.request.serverName}</p>
</body></html>
EL Operators
EL supports arithmetic, relational, logical, and the empty operator:
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html><body>
<h3>Arithmetic Operators</h3>
<p>10 + 5 = ${10 + 5}</p>
<p>10 - 3 = ${10 - 3}</p>
<p>4 * 3 = ${4 * 3}</p>
<p>10 / 3 = ${10 / 3}</p>
<p>10 mod 3 = ${10 mod 3}</p>
<h3>Relational Operators</h3>
<p>5 == 5: ${5 == 5}</p>
<p>5 != 3: ${5 != 3}</p>
<p>10 > 5: ${10 gt 5}</p>
<p>3 < 7: ${3 lt 7}</p>
<p>5 >= 5: ${5 ge 5}</p>
<h3>Logical Operators</h3>
<p>true && false: ${true and false}</p>
<p>true || false: ${true or false}</p>
<p>!true: ${not true}</p>
<h3>Empty Operator</h3>
<p>Is null empty: ${empty null}</p>
<p>Is "" empty: ${empty ""}</p>
<p>Is "hello" empty: ${empty "hello"}</p>
<h3>Ternary / Conditional</h3>
<p>${param.age gt 18 ? "Adult" : "Minor"}</p>
</body></html>
Ready to Level Up Your Skills?
Explore 500+ free tutorials across 20+ languages and frameworks.