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.
<p>Welcome, <%= request.getParameter("username") %>!</p><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.
<p>2 + 2 = ${2 + 2}</p><p>Is 10 greater than 5? ${10 > 5}</p><p>Username: ${param.username}</p>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.
request.setAttribute("pageTitle", "Dashboard");request.getSession().setAttribute("username", "Asha");request.getServletContext().setAttribute("appVersion", "3.2");request.getRequestDispatcher("/dashboard.jsp").forward(request, response);<h1>${pageTitle}</h1><p>Logged in as: ${username}</p><p>App version: ${appVersion}</p>Click Run to see what this code prints.
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 Object | Purpose |
|---|---|
| param | A single request parameter value, e.g. ${param.username} |
| paramValues | All values of a request parameter that can appear multiple times (e.g. checkboxes) |
| header | A single HTTP request header value, e.g. ${header["User-Agent"]} |
| cookie | Access to request cookies by name |
| pageScope | Attributes stored at page scope |
| requestScope | Attributes stored at request scope |
| sessionScope | Attributes stored at session scope |
| applicationScope | Attributes stored at application 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.
| Category | Operators |
|---|---|
| Arithmetic | + - * / (or div) % (or mod) |
| Relational | == != < > <= >= (or eq, ne, lt, gt, le, ge) |
| Logical | && || ! (or and, or, not) |
| Empty check | empty - true if a value is null or an empty string/collection |
<c:if test="${empty username}"> <p>No username provided.</p></c:if>
<p>Total: ${price * quantity}</p><p>Eligible: ${age >= 18}</p>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.
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"));<p>Name: ${currentUser.name}</p><p>Email: ${currentUser.email}</p>
<p>First role: ${roles[0]}</p><p>Setting value: ${settingsMap["theme"]}</p>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
- 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.