Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

JSP and Servlets
  • Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
  • Set Up Tomcat 11 and a Maven Web Project in IntelliJ IDEA
  • The Servlet Request Lifecycle: @WebServlet, web.xml, init, doGet and destroy
  • Servlet Request Parameters, and Forward vs Redirect
  • Run a Java Web App in Docker: Tomcat 11 With One Compose Command
  • Sessions, Cookies and Filters in Java Servlets: Log In Once, Stay Logged In
  • Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
  • JSP Scripting: Expressions, Scriptlets, Declarations, Comments and Includes
  • JavaBeans and JSP Scopes: Page, Request, Session and Application
  • JSP Expression Language Step by Step: ${...} for Beans, Maps, Lists, Params and Scopes
  • JSTL Core Tags: c:set, c:out, c:if, c:choose and c:forEach
  • JSTL Functions and Formatting: fn:length, fn:toUpperCase, fn:contains, fmt:formatNumber and fmt:formatDate
  • Maintaining and Migrating Legacy JSP With AI: Explain the Page, Refactor to a DAO, Servlet and JSTL, Then Move It to React
  • MVC and the Front Controller With Servlets and JSP
  • Forms With JSP and Servlets: Handle the POST and Validate on the Server
  • Mini App With Servlets and JSP: Login, Logout and a Shared Web Template
  • SQLite and a JDBC Connection Pool in a Java Web App
  • Users CRUD With Servlets and JSP, Part 1: Add and List Through a DAO
  • Users CRUD With Servlets and JSP, Part 2: Update and Delete
  1. Blog
  2. /
  3. JSP and Servlets
  4. /
  5. JSP

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026JSP

What you will learn

  • Add the JSTL jars with Maven and turn the tags on with a taglib line
  • Print values safely with c:out and see why plain EL can run HTML
  • Make decisions with c:if and c:choose
  • Build a table row per item with c:forEach and varStatus

Before you start

  • JSP Expression Language Step by Step
  • MVC and the Front Controller With Servlets and JSP

Expression Language prints values. JSTL core tags let the page decide and repeat, still with no Java code in it. Each tag does one job:

JobTag
Keep a valuec:set
Print a value safelyc:out
Show a part when a test is truec:if
Pick one of many (like a switch)c:choose, c:when, c:otherwise
Repeat for each itemc:forEach

Add the JSTL jars

JSTL, the JavaServer Pages Standard Tag Library, comes as jar files, and plain Tomcat doesn't ship it, so your war has to carry it. With Maven, add two dependencies to pom.xml (without Maven you'd copy the jars into WEB-INF/lib):

<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>6.1.0</version>
    <scope>provided</scope>
</dependency>
<!-- JSTL: the API ... (plain Tomcat ships no JSTL, so the war carries it) -->
<dependency>
    <groupId>jakarta.servlet.jsp.jstl</groupId>
    <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
    <version>3.0.2</version>
    <exclusions>
        <!-- the server already has EL 6.0; the old EL 5 jar would clash -->
        <exclusion>
            <groupId>jakarta.el</groupId>
            <artifactId>jakarta.el-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- ... and the implementation that does the work -->
<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>jakarta.servlet.jsp.jstl</artifactId>
    <version>3.0.1</version>
</dependency>

The servlet API is provided because the server has it. The JSTL API excludes the EL jar because Tomcat 11 already has EL 6.0. Click Load Maven Changes so the jars download. Then every JSP that uses the tags needs one line:

<%@ taglib prefix="c" uri="jakarta.tags.core" %>

You don't need to remember any of this; "update my pom for JSTL on Tomcat 11" is a fine prompt for an AI assistant.

The data: three orders

An Order class has id, customer, total and status with getters, so order.customer calls getCustomer(). One servlet serves three pages:

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;

// three orders, three pages
@WebServlet({"/basics", "/decisions", "/orders"})
public class OrderServlet extends HttpServlet {

    private static final List<Order> ORDERS = List.of(
            new Order(1001, "Emma Clark", 120.0, "DELIVERED"),
            new Order(1002, "Liam Turner", 80.5, "SHIPPED"),
            new Order(1003, "<b>Noah</b> Hayes", 45.0, "NEW"));   // HTML typed into a name

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String page = req.getServletPath().substring(1);
        switch (page) {
            case "basics" -> req.setAttribute("order", ORDERS.get(2));
            case "decisions" -> req.setAttribute("order", find(req.getParameter("id")));
            default -> req.setAttribute("orders", ORDERS);
        }
        System.out.println(page + " -> " + page + ".jsp");
        req.getRequestDispatcher("/WEB-INF/views/" + page + ".jsp").forward(req, resp);
    }

    // /decisions?id=1002 picks an order; no id: the first one
    private Order find(String id) {
        return ORDERS.stream()
                .filter(o -> String.valueOf(o.getId()).equals(id))
                .findFirst()
                .orElse(ORDERS.get(0));
    }
}

Note Noah's name: it has an HTML bold tag typed into it. That's on purpose.

c:set and c:out

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%-- STEP 1: c:set makes a variable, c:out prints a value safely --%>
<!DOCTYPE html>
<html>
<head><title>Basics</title></head>
<body>
<c:set var="shop" value="Corner Shop" />
<h1>${shop}</h1>
<p>EL: ${order.customer}</p>
<p>c:out: <c:out value="${order.customer}" /></p>
</body>
</html>
Corner Shop
EL: Noah Hayes          (Noah in bold)
c:out: <b>Noah</b> Hayes

c:set makes a page variable, shop, and ${shop} prints it. Then the interesting part. Plain EL writes the customer name straight into the HTML, so the browser runs the <b> tag and Noah shows up in bold. If the value held a script instead, that would run too. c:out escapes the value, so the whole thing shows as plain text, tags and all. Use c:out for anything a user typed.

c:if and c:choose

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%-- STEP 2: c:if shows a part only when true; c:choose picks one of many --%>
<!DOCTYPE html>
<html>
<head><title>Decisions</title></head>
<body>
<h1>Order ${order.id}</h1>
<p>Total: ${order.total}</p>
<c:if test="${order.total > 100}">
    <p>Free shipping</p>
</c:if>
<p>Status:
    <c:choose>
        <c:when test="${order.status == 'NEW'}">Being packed</c:when>
        <c:when test="${order.status == 'SHIPPED'}">On the way</c:when>
        <c:otherwise>Delivered</c:otherwise>
    </c:choose>
</p>
</body>
</html>

Open /decisions with no id and you get the first order:

Order 1001
Total: 120.0
Free shipping
Status: Delivered

c:if's test, total greater than 100, is true, so "Free shipping" shows. In c:choose, each c:when has its own test and the first true one wins; if none is true, c:otherwise takes over. The status is DELIVERED, so neither test matches and the page says Delivered. Try /decisions?id=1002 to get "On the way".

c:forEach: one row each

<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%-- STEP 3: c:forEach writes one row per order --%>
<!DOCTYPE html>
<html>
<head><title>Orders</title></head>
<body>
<h1>Orders</h1>
<table border="1" cellpadding="6">
    <tr><th>#</th><th>Order</th><th>Customer</th><th>Total</th></tr>
    <c:forEach var="order" items="${orders}" varStatus="row">
    <tr>
        <td>${row.count}</td>
        <td>${order.id}</td>
        <td><c:out value="${order.customer}" /></td>
        <td>${order.total}</td>
    </tr>
    </c:forEach>
</table>
</body>
</html>
Orders
# | Order | Customer | Total
1 | 1001 | Emma Clark | 120.0
2 | 1002 | Liam Turner | 80.5
3 | 1003 | <b>Noah</b> Hayes | 45.0

items is the list and var names the current order. varStatus="row" gives you loop details, so row.count counts 1, 2, 3. And c:out again keeps Noah's name as plain text.

The orders list with three orders, 1001 Emma Clark, 1002 Liam Turner and 1003 Noah Hayes. Each pass of c:forEach takes the next order and writes one table row, with row.count 1, 2 and 3. orders (List) table rows 1001 Emma Clark 1002 Liam Turner 1003 <b>Noah</b> Hayes 1 | 1001 | Emma Clark 2 | 1002 | Liam Turner 3 | 1003 | <b>Noah</b> Hayes pass 1 pass 2 pass 3, escaped
Each pass puts the next order in order and writes one row.

Common mistakes

  • Forgetting the taglib line. The server doesn't know the c: tags, so they reach the browser untouched and do nothing.
  • Printing user input with plain ${...}. Any HTML or script in it runs in the browser. Use c:out.
  • Leaving out the EL exclusion on the JSTL API. An old EL jar in the war can clash with the one Tomcat already has.

Quick recap

TagResult in this project
c:setCorner Shop
c:outthe bold tag stayed as text
c:ifFree shipping
c:chooseDelivered
c:forEachthree rows

Next: text, number and date helpers in JSTL functions and formatting.

Keep going in JSP and Servlets · 11 of 19

Previous
JSP Expression Language Step by Step: ${...} for Beans, Maps, Lists, Params and Scopes
Next post
JSTL Functions and Formatting: fn:length, fn:toUpperCase, fn:contains, fmt:formatNumber and fmt:formatDate
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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.

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.

On this page

  • Add the JSTL jars
  • The data: three orders
  • c:set and c:out
  • c:if and c:choose
  • c:forEach: one row each
  • Common mistakes
  • Quick recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.