Nearby lessons
16 of 30JSTL - Core Library
- Understand what JSTL is and why it exists
- Install JSTL in your web application
- Use general purpose tags: c:out, c:set, c:remove, c:catch
- Use conditional tags: c:if, c:choose, c:when, c:otherwise
- Use URL-related tags: c:import, c:redirect, c:url, c:param
The JSTL Core Library provides standard tags for common programming tasks in JSP — conditions, loops, variable management, and URL handling. This lesson covers installation, general purpose tags, conditional tags, and URL-related tags with complete code examples.
What is JSTL?
JSTL (JSP Standard Tag Library) is a collection of pre-built tags created by SUN Microsystems. Instead of writing Java code inside JSP pages, developers can use these standard tags to perform common tasks.
Main objective of JSTL: Remove Java code from JSP.
While EL (Expression Language) removes simple expressions, JSTL removes procedural Java code like conditions and loops.
JSTL is divided into 5 sub-libraries:
- Core Library — conditions, loops, variable management, URL handling
- SQL Library — database operations
- Functions Library — string manipulation
- FMT (Formatting) Library — number/date formatting for internationalization
- XML Library — reading and writing XML data
Installing JSTL
By default, JSTL is not available to JSP pages. You must add two JAR files to your application:
| JAR File | Purpose |
|---|---|
jstl.jar | API classes defined by SUN |
standard.jar | Implementation classes provided by the vendor |
Where to place the JARs: It is recommended to place these files at the server level (e.g., D:\Tomcat 7.0\lib) instead of the application level, so all web applications can use them.
Taglib directive — To use the core library in a JSP page:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Core Library Overview
The core library is divided into 4 categories based on functionality:
| Category | Tags |
|---|---|
| General Purpose Tags | <c:out>, <c:set>, <c:remove>, <c:catch> |
| Conditional Tags | <c:if>, <c:choose>, <c:when>, <c:otherwise> |
| Iteration Tags | <c:forEach>, <c:forTokens> |
| URL Related Tags | <c:import>, <c:url>, <c:redirect>, <c:param> |
c:out — Writing Output
The <c:out> tag writes expressions and template text to the JSP output. It supports three attributes:
| Attribute | Description | Required |
|---|---|---|
value | The expression or text to output | Yes |
default | Value to display if result is null or missing | No |
escapeXml | If true, escapes HTML/XML characters (default: true) | No |
Form 1 — Simple output:
<c:out value="durga" /> <!-- Prints: durga -->
<c:out value="${param.user}" /> <!-- Prints the request parameter "user" -->
Form 2 — With default value:
<c:out value="${param.user}" default="Guest" />
<!-- If "user" parameter is missing, prints: Guest -->
c:set — Setting Attributes
The <c:set> tag sets attributes in any scope, or sets bean/map properties.
Form 1 — Set an attribute:
<c:set var="name" value="attributeValue" scope="request" />
The scope attribute is optional — default is page scope.
Form 2 — Set a bean or map property:
<c:set target="customer" property="name" value="pavan" />
Example:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="x" value="10" scope="request" />
<c:set var="y" value="20" scope="request" />
<c:set var="sum" value="${x + y}" scope="session" />
<h1>The result is: <c:out value="${sum}" /></h1>
Attributes of c:set:
| Attribute | Description |
|---|---|
var | Name of the attribute to set |
value | Value to assign |
scope | Scope (page, request, session, application) |
target | Target bean or map object |
property | Property name on the target |
c:remove — Removing Attributes
The <c:remove> tag removes an attribute from a specified scope.
<c:remove var="x" scope="session" />
Attributes:
| Attribute | Description |
|---|---|
var | Name of the attribute to remove |
scope | Scope where the attribute exists (optional) |
If scope is not specified, the container searches through page → request → session → application scopes in order.
Example:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="x" value="10" scope="page" />
<c:set var="y" value="60" scope="page" />
<c:set var="sum" value="${x + y}" scope="session" />
<h1>
The result is: <c:out value="${sum}" /><br>
<c:remove var="x" />
<c:remove var="y" />
<c:remove var="sum" />
The result is: <c:out value="${sum}" default="1000" />
</h1>
c:catch — Exception Handling
The <c:catch> tag catches exceptions within the JSP instead of forwarding to an error page. Place risky code inside the tag body:
<c:catch>
Risky Code
</c:catch>
If an exception is raised, this tag suppresses it and the rest of the JSP executes normally.
To capture the exception, use the var attribute:
<c:catch var="e">
<%
int age = Integer.parseInt(request.getParameter("uage"));
%>
Age: ${param.uage}<br>
</c:catch>
<c:if test="${e != null}">
Oops... Exception raised: ${e}<br>
</c:if>
Behavior:
- With valid input (
uage=47): displays the age normally - With invalid input (
uage=ten): catches the NumberFormatException and shows the error message
Summary of General Purpose Tags:
| Tag | Purpose | Attributes |
|---|---|---|
<c:out> | Write expressions and template text to JSP | value, default, escapeXml |
<c:set> | Set attributes, bean properties, or map properties | var, value, scope, target, property |
<c:remove> | Remove attributes from a scope | var, scope |
<c:catch> | Catch exceptions and continue JSP execution | var |
c:if — Conditional Tag
The <c:if> tag implements the core Java if statement. There are two forms:
Form 1 — Without body (evaluates condition and stores result):
<c:if test="test_condition" var="x" scope="session" />
Both test and var are mandatory. scope is optional (default: page). The test result is stored in variable x for later use.
Form 2 — With body (executes body if condition is true):
<c:if test="test_condition">
Body executes only if test_condition is true
</c:if>
Both var and scope are optional. The test attribute is mandatory.
Example:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="x" value="10" scope="request" />
<c:if test="${x eq '10'}">
<h1>x value is equal to 10</h1>
</c:if>
c:choose / c:when / c:otherwise — If-Else and Switch
These tags implement if-else and switch statements. JSTL has no standalone "else" tag.
If-Else pattern:
<c:choose>
<c:when test="test_condition">
Action 1 (executes if true)
</c:when>
<c:otherwise>
Action 2 (executes if false)
</c:otherwise>
</c:choose>
Switch pattern:
<c:choose>
<c:when test="${s == 1}">Sunday</c:when>
<c:when test="${s == 2}">Monday</c:when>
<c:when test="${s == 3}">Tuesday</c:when>
<c:when test="${s == 4}">Wednesday</c:when>
<c:when test="${s == 5}">Thursday</c:when>
<c:otherwise>Select between 1 and 5</c:otherwise>
</c:choose>
Rules:
<c:choose>must contain at least one<c:when><c:otherwise>is optional and must be the last child- Every
<c:when>implicitly contains a break statement — no fall-through <c:choose>and<c:otherwise>take no attributes<c:when>takes one mandatory attribute:test
c:import — Dynamic Include
The <c:import> tag includes the response of another page at request processing time (dynamic include).
Form 1 — Basic import:
<c:import url="second.jsp" />
Form 2 — Cross-context import (from another web application):
<c:import url="/second.jsp" context="/webapp2" />
Both url and context must use absolute paths (start with /).
Form 3 — Store result in a variable (reusable without re-importing):
<c:import url="second.jsp" var="x" scope="request" />
${x}<br> <!-- Use the stored result multiple times -->
Form 4 — Store as Reader (alternative to var):
<c:import url="second.jsp" varReader="r" scope="page" />
Form 5 — Pass parameters to the target page:
<c:import url="second.jsp">
<c:param name="c1" value="JAVA" />
<c:param name="c2" value="PHP" />
</c:import>
c:redirect — Redirection
The <c:redirect> tag redirects the request to another page. It is equivalent to sendRedirect() of ServletResponse.
Form 1 — Basic redirect:
<c:redirect url="second.jsp" />
The url can be relative or absolute.
Form 2 — Cross-context redirect:
<c:redirect url="/second.jsp" context="/webapp2" />
Form 3 — Redirect with parameters:
<c:redirect url="second.jsp">
<c:param name="c1" value="Java" />
<c:param name="c2" value="PHP" />
</c:redirect>
c:url — URL Rewriting
The <c:url> tag rewrites URLs to append session information (for session tracking) and form parameters.
Form 1 — Basic URL rewrite:
<c:url value="second.jsp" var="x" scope="request" />
<h1>The modified URL: ${x}</h1>
The encoded URL with session ID is stored in variable x.
Form 2 — Cross-context URL:
<c:url value="/second.jsp" context="/webapp2" var="x" scope="request" />
Form 3 — URL with parameters:
<c:url value="second.jsp" var="x">
<c:param name="c1" value="JAVA" />
<c:param name="c2" value="PHP" />
</c:url>
<a href="${x}">Click Here to go to Next Page</a>
Summary of URL Tags
| Tag | Description | Attributes |
|---|---|---|
<c:import> | Include response of another page at request time | url, var, scope, varReader, context |
<c:redirect> | Redirect the request to another page | url, context |
<c:url> | Rewrite URL to append session ID and parameters | value, var, scope, context |
<c:param> | Send parameters during import or redirect | name, value |
Complete Core Library Summary:
| Tag | Purpose |
|---|---|
<c:out> | Display expressions and template text |
<c:set> | Set attributes and bean/map properties |
<c:remove> | Remove attributes from a scope |
<c:catch> | Suppress exceptions and continue JSP |
<c:if> | Implement Java if statement |
<c:choose>, <c:when>, <c:otherwise> | Implement if-else and switch statements |
<c:forEach> | General purpose for loop |
<c:forTokens> | String tokenization |
<c:import> | Dynamic include of other pages |
<c:redirect> | Redirect to another page |
<c:param> | Send parameters during import/redirect |
<c:url> | Rewrite URL for session management |
- JSTL removes Java code from JSP pages
- Core library covers conditions, loops, variables, and URLs
- Ready-to-use code examples for every tag