JSP Expression Language Step by Step: ${...} for Beans, Maps, Lists, Params and Scopes
Expression Language prints a value in a JSP with ${...} and no Java on the page. Read a Student bean, a price map, a tag list and ?city=Boston, run small checks, and see the order EL searches the scopes.
What you will learn
- Print a bean property with ${student.name} instead of a Java cast
- Read a map by key, a list by index and a URL parameter with param
- Use EL operators: arithmetic, comparisons, and, empty and the ternary
- Predict which scope ${message} reads when the name is set in several
Before you start
- JavaBeans and JSP Scopes
- MVC and the Front Controller With Servlets and JSP
Expression Language, EL for short, prints a value in a JSP with a small ${...} expression, with no Java code on the page. Think of a JSP as a form letter with blanks: "Hello, ____. Your order is ready." The page sits on the server, and when a request comes in the server fills every blank before the page leaves. An EL expression is how you write one blank.
The data: one servlet sets the attributes
A Student class holds name, age and active, plus a middleName that is never set, so it stays null. Remember that one. One servlet answers three URLs, loads the data and forwards to the page named by the URL:
package org.studyeasy.web;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
// the same data for every step; each URL shows it on its own page
@WebServlet({"/basics", "/access", "/operators"})
public class StepServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setAttribute("student", new Student("Emma Clark", 21, true));
req.setAttribute("prices", Map.of("Coffee", 2.75, "Green Tea", 3.5));
req.setAttribute("tags", List.of("java", "sql", "html"));
String step = req.getServletPath().substring(1);
System.out.println(step + " -> " + step + ".jsp");
req.getRequestDispatcher("/WEB-INF/views/" + step + ".jsp")
.forward(req, resp);
}
}
basics -> basics.jsp
access -> access.jsp
operators -> operators.jsp
getServletPath().substring(1) strips the slash, so /basics becomes the view name basics. That's all setup; the interesting part is in the JSPs.
Reading ${student.name}
EL doesn't touch the field. student.name calls getName(), and for a boolean the getter starts with is, so student.active calls isActive().
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="org.studyeasy.web.Student" %>
<%-- STEP 1: the same value read twice, once with Java, once with EL --%>
<!DOCTYPE html>
<html>
<head><title>Basics</title></head>
<body>
<h1>Basics</h1>
<p>Java: <%= ((Student) request.getAttribute("student")).getName() %></p>
<p>EL: ${student.name}</p>
<p>Age: ${student.age}</p>
</body>
</html>
Basics
Java: Emma Clark
EL: Emma Clark
Age: 21
The Java line has to get the attribute from the request, cast it to Student and call the getter. The EL line gets the same value in a few characters.
Beans, maps, lists and the URL
<%@ page contentType="text/html;charset=UTF-8" %>
<%-- STEP 3: beans, maps, lists and URL parameters --%>
<!DOCTYPE html>
<html>
<head><title>Access</title></head>
<body>
<h1>Access</h1>
<p>Bean: ${student.name}</p>
<p>Map: ${prices['Green Tea']}</p>
<p>List: ${tags[0]}</p>
<p>URL parameter: ${param.city}</p>
<p>Missing: [${student.middleName}]</p>
</body>
</html>
Open /access?city=Boston:
Access
Bean: Emma Clark
Map: 3.5
List: java
URL parameter: Boston
Missing: []
- A map takes the key in square brackets:
prices['Green Tea']. Square brackets are needed here because the key has a space. - A list takes an index:
tags[0]is the first element. param.cityreads the URL parameter. Remove?city=Bostonand that line just goes blank.middleNameis null, so nothing prints between the brackets, and there's no error either.
Small checks in the page
<%@ page contentType="text/html;charset=UTF-8" %>
<%-- STEP 4: small calculations and checks inside ${...} --%>
<!DOCTYPE html>
<html>
<head><title>Operators</title></head>
<body>
<h1>Operators</h1>
<p>Two coffees: ${prices.Coffee * 2}</p>
<p>Adult: ${student.age >= 18}</p>
<p>Can enrol: ${student.active and student.age >= 18}</p>
<p>No middle name: ${empty student.middleName}</p>
<p>Status: ${student.active ? 'Active' : 'Suspended'}</p>
</body>
</html>
Operators
Two coffees: 5.5
Adult: true
Can enrol: true
No middle name: true
Status: Active
prices.Coffee * 2 is 2.75 times 2. Comparisons give true or false, and and needs both sides. empty is true for null (and for empty strings and lists). The ternary ? : prints Active or Suspended. You don't need to memorise any of it; ask an AI assistant for the expression you want, then read it.
One name, three scopes
package org.studyeasy.web;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
// one name, "message", in three scopes - with a different text in each
@WebServlet("/scopes")
public class ScopeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setAttribute("message", "Hello from the request");
req.getSession().setAttribute("message", "Hello from the session");
getServletContext().setAttribute("message", "Hello from the application");
System.out.println("message in request, session, application");
req.getRequestDispatcher("/WEB-INF/views/scopes.jsp").forward(req, resp);
}
}
The name message now sits in three places: the request (this request only), the session (this browser) and the servlet context (the whole app). What does ${message} print?
<%@ page contentType="text/html;charset=UTF-8" %>
<%-- STEP 2: one name in three scopes --%>
<!DOCTYPE html>
<html>
<head><title>Scopes</title></head>
<body>
<h1>Scopes</h1>
<p>message: ${message}</p>
<p>sessionScope: ${sessionScope.message}</p>
<p>applicationScope: ${applicationScope.message}</p>
</body>
</html>
Scopes
message: Hello from the request
sessionScope: Hello from the session
applicationScope: Hello from the application
A plain name searches four scopes from the top: page, request, session, application. Page is empty, request has the value, so it stops there and session and application are never asked. To read a particular scope, name it: sessionScope.message, applicationScope.message.
Common mistakes
- Spelling the property differently from the getter.
${student.Name}with a capital N fails, because there is nogetNamematch for it. Write it the way the getter reads, minusget, with a small first letter. - Expecting an error for a missing value. EL prints nothing for null, which can hide a typo in an attribute name.
- Setting the same name in several scopes and forgetting which one wins. The narrowest scope that has it wins.
Quick recap
| Expression | Gives |
|---|---|
${student.name} | Emma Clark, through getName() |
${message} | the first match: page, request, session, application |
${prices['Green Tea']} | 3.5, a map by key |
${param.city} | Boston, from the URL |
${student.active ? 'Active' : 'Suspended'} | Active |
EL prints values. To decide and repeat without Java, add the tags in JSTL core tags.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
A JSP looks like HTML with a little Java, yet Tomcat runs it as a servlet. See what a servlet container does for each request, what the generated hello_jsp.java looks like, what a .war holds, and Tomcat vs TomEE.
JSP Scripting: Expressions, Scriptlets, Declarations, Comments and Includes
The JSP tags you'll read in older Java web apps: expressions, scriptlets, declarations, page imports, two kinds of comment, and the include directive versus the jsp:include action, each shown with what Tomcat turns it into.
JavaBeans and JSP Scopes: Page, Request, Session and Application
A Student bean filled from a form with jsp:useBean and setProperty, then one Counter class kept in four scopes. Three visits, two browsers and an include page show exactly who sees which number.
JSTL Core Tags: c:set, c:out, c:if, c:choose and c:forEach
Five JSTL core tags let a JSP decide and repeat with no Java on the page. Add the jars to pom.xml, then use c:set, c:out (and see why it is safer than plain EL), c:if, c:choose and c:forEach on three orders.
