Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

JSTL - JSP Standard Tag Library

What is JSTL?

JSTL (JSP Standard Tag Library) is a collection of useful JSP tags that encapsulate common functionality like iteration, conditionals, XML processing, internationalization, and SQL. JSTL eliminates the need for scriptlets in JSP pages, making them cleaner and more maintainable.

To use JSTL, add the JSTL JAR to your project and declare the taglib directive:

LibraryURIPrefixPurpose
Corehttp://java.sun.com/jsp/jstl/corecVariables, flow control, I/O
Formattinghttp://java.sun.com/jsp/jstl/fmtfmtDates, numbers, i18n
SQLhttp://java.sun.com/jsp/jstl/sqlsqlDatabase access
XMLhttp://java.sun.com/jsp/jstl/xmlxXML processing
Functionshttp://java.sun.com/jsp/jstl/functionsfnString functions
JSTL Core Tags - Variables and Output
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html><body>

<h3>c:set and c:out</h3>
<%-- c:set: sets a variable in a scope --%>
<c:set var="name" value="Alice" scope="session"/>
<c:set var="score" value="95"/>
<c:set var="greeting" value="Hello, ${name}!"/>

<%-- c:out: outputs a value (escapes HTML by default) --%>
<p><c:out value="${greeting}"/></p>
<p>Score: <c:out value="${score}" default="N/A"/></p>

<%-- c:remove: removes a variable --%>
<c:remove var="score"/>
<p>After remove: <c:out value="${score}" default="Removed!"/></p>

<h3>c:if and c:choose</h3>
<c:set var="age" value="20"/>

<c:if test="${age ge 18}">
    <p>${name} is an adult.</p>
</c:if>

<c:choose>
    <c:when test="${score ge 90}"><p>Grade: A</p></c:when>
    <c:when test="${score ge 80}"><p>Grade: B</p></c:when>
    <c:when test="${score ge 70}"><p>Grade: C</p></c:when>
    <c:otherwise><p>Grade: F</p></c:otherwise>
</c:choose>

</body></html>
JSTL Core - Iteration Tags
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="java.util.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    List<String> fruits = Arrays.asList("Apple","Banana","Cherry","Date","Elderberry");
    request.setAttribute("fruits", fruits);

    Map<String, Integer> scores = new LinkedHashMap<>();
    scores.put("Alice", 95);
    scores.put("Bob", 82);
    scores.put("Charlie", 78);
    request.setAttribute("scores", scores);
%>
<html><body>

<h3>c:forEach - List Iteration</h3>
<ul>
<c:forEach var="fruit" items="${fruits}" varStatus="status">
    <li>${status.index + 1}. ${fruit}
        <c:if test="${status.first}"> (First)</c:if>
        <c:if test="${status.last}"> (Last)</c:if>
    </li>
</c:forEach>
</ul>

<h3>c:forEach - Map Iteration</h3>
<table border="1">
    <tr><th>Student</th><th>Score</th></tr>
    <c:forEach var="entry" items="${scores}">
        <tr><td>${entry.key}</td><td>${entry.value}</td></tr>
    </c:forEach>
</table>

<h3>c:forEach - Range</h3>
<c:forEach var="i" begin="1" end="5" step="1">
    <span>${i} </span>
</c:forEach>

<h3>c:forTokens - Tokenize String</h3>
<c:forTokens var="color" items="red,green,blue,yellow" delims=",">
    <span style="color:${color}">${color} </span>
</c:forTokens>

</body></html>

JSTL Formatting Tags

JSTL fmt Tags
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="java.util.Date"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%
    request.setAttribute("now", new Date());
    request.setAttribute("price", 1234567.89);
    request.setAttribute("percent", 0.756);
%>
<html><body>

<h3>fmt:formatDate</h3>
<p>Default: <fmt:formatDate value="${now}"/></p>
<p>Date only: <fmt:formatDate value="${now}" type="date" dateStyle="long"/></p>
<p>Time only: <fmt:formatDate value="${now}" type="time" timeStyle="short"/></p>
<p>Custom: <fmt:formatDate value="${now}" pattern="dd-MMM-yyyy HH:mm:ss"/></p>

<h3>fmt:formatNumber</h3>
<p>Number: <fmt:formatNumber value="${price}" type="number"/></p>
<p>Currency: <fmt:formatNumber value="${price}" type="currency" currencySymbol="$"/></p>
<p>Percent: <fmt:formatNumber value="${percent}" type="percent"/></p>
<p>Pattern: <fmt:formatNumber value="${price}" pattern="#,##0.00"/></p>

<h3>c:url and c:redirect</h3>
<%-- c:url: creates a URL with proper encoding --%>
<c:url var="searchUrl" value="/search">
    <c:param name="q" value="java jsp"/>
    <c:param name="page" value="1"/>
</c:url>
<a href="${searchUrl}">Search</a>

</body></html>

Ready to Level Up Your Skills?

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