LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 1120 min read

JSP Expression Language (EL)

Learn JSP Expression Language syntax and how it lets you access request, session, and application data cleanly, without scriptlets.

Introduction

You have now seen the old way of embedding Java in JSP - scriptlets and expressions - and the objects those relied on. This lesson introduces Expression Language (EL), the syntax that modern JSP development actually uses. If you have noticed ${ ... } appearing in earlier lessons' code snippets, this is where you learn exactly what that syntax means and how it works.

Why EL Was Introduced

Recall from Lesson 8 that heavy scriptlet use makes JSP pages hard to read and hard for non-Java developers to safely edit. Expression Language, introduced as a built-in JSP feature in JSP 2.0, solves this by providing a small, simple expression syntax - no loops, no semicolons, no explicit Java casting - purpose-built for reading and displaying data inside a page.

Scriptlet style (old)
<p>Welcome, <%= request.getParameter("username") %>!</p>
EL style (modern)
<p>Welcome, ${param.username}!</p>

Both lines produce identical output, but the EL version is shorter, does not require Java knowledge to understand, and cannot accidentally introduce a compile error the way a mistyped scriptlet can.

Basic EL Syntax

An EL expression is always wrapped in ${ and }. Anything inside is evaluated by the container and the resulting value is inserted into the page as text, similar to an old-style <%= %> expression, but with a friendlier syntax.

Simple EL expressions
<p>2 + 2 = ${2 + 2}</p>
<p>Is 10 greater than 5? ${10 > 5}</p>
<p>Username: ${param.username}</p>
Rendered HTML for a request like ?username=Asha

Click Run to see what this code prints.

Accessing Scoped Attributes

The real power of EL is how naturally it reads attributes from the request, session, and application scopes you learned about in the previous lesson - without ever calling getAttribute() explicitly. EL automatically searches page, then request, then session, then application scope (in that order) until it finds a matching name.

A Servlet setting attributes at different scopes
request.setAttribute("pageTitle", "Dashboard");
request.getSession().setAttribute("username", "Asha");
request.getServletContext().setAttribute("appVersion", "3.2");
request.getRequestDispatcher("/dashboard.jsp").forward(request, response);
dashboard.jsp reading those attributes with EL
<h1>${pageTitle}</h1>
<p>Logged in as: ${username}</p>
<p>App version: ${appVersion}</p>
Rendered HTML

Click Run to see what this code prints.

No Casting Required

Notice there is no (String) cast anywhere in the EL version, unlike the scriptlet equivalent request.getAttribute("username") would have required. EL handles this automatically, which removes an entire category of ClassCastException bugs.

EL Implicit Objects

EL has its own set of implicit objects, separate from (but related to) the scripting implicit objects from the previous lesson. These give direct, convenient access to parameters, headers, and cookies without any Java method calls.

EL Implicit ObjectPurpose
paramA single request parameter value, e.g. ${param.username}
paramValuesAll values of a request parameter that can appear multiple times (e.g. checkboxes)
headerA single HTTP request header value, e.g. ${header["User-Agent"]}
cookieAccess to request cookies by name
pageScopeAttributes stored at page scope
requestScopeAttributes stored at request scope
sessionScopeAttributes stored at session scope
applicationScopeAttributes stored at application scope
Being explicit about scope
<p>${requestScope.pageTitle}</p>
<p>${sessionScope.username}</p>
<p>${applicationScope.appVersion}</p>

Using ${pageTitle} (unscoped) and ${requestScope.pageTitle} (explicitly scoped) produce the same result here, since pageTitle only exists at request scope. Being explicit is useful when two different scopes accidentally use the same attribute name.

Operators in EL

EL supports arithmetic, relational, and logical operators, including easier-to-read word alternatives for some of them.

CategoryOperators
Arithmetic+ - * / (or div) % (or mod)
Relational== != < > <= >= (or eq, ne, lt, gt, le, ge)
Logical&& || ! (or and, or, not)
Empty checkempty - true if a value is null or an empty string/collection
Using operators and empty
<c:if test="${empty username}">
<p>No username provided.</p>
</c:if>
<p>Total: ${price * quantity}</p>
<p>Eligible: ${age >= 18}</p>
A Preview of JSTL

The <c:if> tag above belongs to JSTL, which pairs naturally with EL and is the subject of the next lesson. EL supplies the expressions; JSTL supplies the tags (like if/for loops) that use them - together they replace scriptlets almost entirely.

Accessing Objects and Collections

EL can navigate into JavaBean properties using dot notation, and into arrays, Lists, and Maps using bracket notation - all without any explicit getter calls or casting.

A simple User class (JavaBean) set as a request attribute
public class User {
private String name;
private String email;
public String getName() { return name; }
public String getEmail() { return email; }
// constructor omitted for brevity
}
// In a Servlet:
request.setAttribute("currentUser", new User("Asha", "asha@example.com"));
Reading bean properties and a Map with EL
<p>Name: ${currentUser.name}</p>
<p>Email: ${currentUser.email}</p>
<p>First role: ${roles[0]}</p>
<p>Setting value: ${settingsMap["theme"]}</p>
Rendered HTML

Click Run to see what this code prints.

Behind the scenes, ${currentUser.name} calls currentUser.getName() automatically - EL follows the standard JavaBean getter naming convention, so any class exposing a getName() method can be read this way.

Common Mistakes

Avoid These Mistakes
  • Writing ${ } inside a JavaScript template literal or another language's string without realizing it may conflict with that language's own syntax.
  • Expecting ${someName} to find a variable declared in a scriptlet - EL only reads from page/request/session/application scope attributes, not local Java variables.
  • Forgetting the getter method must follow JavaBean naming (getName() for ${bean.name}) or EL will not find it.
  • Setting isELIgnored="true" on the page directive and then being confused when ${ } prints literally instead of evaluating.

Best Practices

  • Prefer EL over scriptlet expressions <%= %> for all new JSP code.
  • Use the explicit scope objects (requestScope, sessionScope, and so on) whenever attribute names might collide across scopes.
  • Combine EL with JSTL tags rather than falling back to scriptlets for logic like loops and conditionals.
  • Use the empty operator instead of manually checking for null and empty string separately.

Frequently Asked Questions

No. EL is a small, deliberately limited expression language for reading and displaying data inside JSP pages. It cannot declare loops, classes, or complex logic - that still belongs in Servlets or Java helper classes.

By default, EL fails silently and evaluates to an empty string rather than throwing an exception, which is different from how a scriptlet would behave with a null reference.

Not directly - EL can read JavaBean properties (via getters) and call methods exposed as EL functions (often through JSTL's fn: functions), but it cannot call arbitrary Java methods the way a scriptlet can.

Yes, mainly for reading legacy JSP code, which remains extremely common in enterprise systems, as discussed earlier in this course.

Key Takeaways

  • Expression Language uses ${ ... } syntax to read and display data without scriptlets.
  • EL automatically searches page, request, session, and application scope for a matching attribute name.
  • EL implicit objects like param, header, and sessionScope give direct access to common request data.
  • EL supports arithmetic, relational, logical operators, and an empty check.
  • EL can navigate JavaBean properties with dot notation and collections with bracket notation.

Summary

Expression Language replaces the need for most scriptlet expressions, letting you read request, session, and application data with clean, safe, ${ } syntax. In the final lesson of this batch, you will pair EL with JSTL Core Tags to handle conditionals and loops - completing the modern, scriptlet-free way of writing JSP pages.

Next Lesson →

JSTL Core Tags