Tutorials Logic, IN info@tutorialslogic.com

JSP Expression Language (EL): Syntax, Operators and Examples

What is Expression Language?

JSP Expression Language (EL) reads values prepared by a servlet, controller, or tag and renders them with concise ${...} expressions.

EL can access JavaBean properties, maps, lists, request parameters, headers, cookies, and page, request, session, or application attributes without embedding Java scriptlets in the view.

Use explicit scope names when the same attribute could exist in more than one scope, and use the empty operator for safe null, empty string, array, collection, and map checks.

JSP Expression Language uses `${expression}` to read scoped attributes, bean properties, collections, request parameters, and supported functions without embedding Java scriptlets. EL support requires JSP 2.0 or later.

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

EL Syntax and Implicit Objects

EL Syntax and Implicit Objects
<%@ 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:

EL Operators - Java Example

EL Operators - Java Example
<%@ 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>

Servlet prepares data for JSP EL

Servlet prepares data for JSP EL
request.setAttribute("pageTitle", "Order summary");
request.setAttribute("order", orderService.find(orderId));
request.getRequestDispatcher("/WEB-INF/views/order.jsp")
       .forward(request, response);

JSP reads properties and handles missing data

JSP reads properties and handles missing data
<h1>${requestScope.pageTitle}</h1>

<c:choose>
  <c:when test="${empty requestScope.order}">
    <p>Order not found.</p>
  </c:when>
  <c:otherwise>
    <p>Order: <c:out value="${order.id}" /></p>
    <p>Total: ${order.total}</p>
  </c:otherwise>
</c:choose>
Before you move on

JSP Expression Language (EL): Syntax, Operators and Examples Mastery Check

4 checks
  • When to use requestScope, sessionScope, param, header, and cookie.
  • I keep business logic in Java code and presentation conditions in EL/JSTL.
  • JSP 2.0+ Expression Language reads scoped data and bean properties with `${expression}` syntax.
  • EL expressions use the syntax ${expression} and can be used directly in HTML without scriptlets.

Expression Language Boundary

  • Silent null result

    A misspelled scope or property can render as an empty value instead of a clear failure. Use explicit scoped attributes for ambiguous names and test missing-data behavior.

Java Server Page Questions Learners Ask

It resolves values from page, request, session, application scopes, and implicit objects.

EL looks up user, then reads its name property through JavaBean-style access.

No. Use c:out or another context-appropriate escaping mechanism.

Next Step
Next Practice

Finish the concept here, then reinforce it with hands-on coding, interview prep, or a tool that matches the topic.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.