Open In App

JSTL Core Tags

JSTL(JSP Standard Tag Library) is a collection of custom tags that provide common functionalities like flow control, database operations, etc. JSTL tags can be embedded in Java Server Pages just like other HTML tags. It is convenient for front-end developers to work with HTML-like tags for including logic in webpages rather than writing Java code in scripts. To use JSTL tags, the following dependencies must be included in pom.xml in a maven project:




<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

Alternatively, you can download the jar files from this link. JSTL tags are grouped into five major categories:



  1. Core Tags
  2. Formatting Tags
  3. SQL Tags
  4. XML Tags
  5. Function Tags

This article focuses on JSTL Core tags.

JSTL Core Tags

Core tags in JSTL are used for iteration, conditional logic, url management, handling exceptions, redirect, etc. 



The following tags are included in the JSTL Core tag library:

c:set

It is used to set a value to a variable that can be used within the specified scope in a JSP. 

Example

set.jsp




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Set Tag</title>
   </head>
   <body>
      <h1>
         <c:set var="name" value="John"/>
         My Name is  
         <c:out value="${name}"/>
      </h1>
   </body>
</html>

Output:

My Name is John

c:remove

removes the specified variable.

Example

remove.jsp




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Remove Tag</title>
   </head>
   <body>
      <h1>
         <c:set var="name" value="John"/>
         My Name is 
         <c:out value="${name}"/>
         <br>
         <c:remove var="name"/>
         My Name is 
         <c:out value="${name}"/>
      </h1>
   </body>
</html>

Output:

My Name is John 
My Name is

In the above example, the variable name is removed, therefore, the second <c:out> doesn’t display the name. 

c:out

It is used to display the content on the web page similar to the expression tag (<%=  %).

Example:

out.jsp




page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Out Tag</title>
   </head>
   <body>
      <h1>
         <c:out value="We are learning JSTL Core Tags"/>
      </h1>
   </body>
</html>

Output: 

We are learning JSTL Core Tags

c:forEach

used to iterate over a collection or an array.

Example:

forEach.jsp




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>ForEach Tag</title>
   </head>
   <body>
      <h1>
         <c:forEach var="i" begin="1" end="10" step="2">
            <c:out value="${i}"/>
         </c:forEach>
         <br>
         <%
            int[] marks={10,12,15,14,9};
            session.setAttribute("marks",marks);
            %>
         <c:forEach var="mark" items="${marks}">
            <c:out value="${mark}"/>
         </c:forEach>
      </h1>
   </body>
</html>

Output:

1 3 5 7 9
10 12 15 14 9

c:import

It is used to include the contents from relative or absolute urls within or outside the server.

Example:

import.jsp




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Import Tag</title>
   </head>
   <body>
      <c:import var="content" url="http://www.google.com"/>
      <c:out value="${content}"/>
   </body>
</html>

c:param

It is used with <c:import> or <c:redirect> to send  parameters in the url to the imported or redirected pages respectively. 

c:redirect

It is used to redirect a page to another url.  

Example:

redirect.jsp




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Redirect Tag</title>
   </head>
   <body>
      <h1>
         <c:redirect url="welcome.jsp">
            <c:param name="name" value="John"/>
         </c:redirect>
      </h1>
   </body>
</html>

welcome.jsp




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Welcome</title>
   </head>
   <body>
      <h1>
         Welcome <%= request.getParameter("name") %>! 
      </h1>
   </body>
</html>

Output:

Welcome John! 

c:catch

It is used to catch any exception of type Throwable that occurs in the body.

Example:

catch.jsp




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Catch Tag</title>
   </head>
   <body>
      <h1>
         <c:catch var="e">
            <%int x=2/0; %>
         </c:catch>
         <c:if test="${e!=null }">
            <c:out value="${e }"></c:out>
         </c:if>
         <br>
      </h1>
   </body>
</html>

Output:

java.lang.ArithmeticException:/by zero

c:if

It is used to include the conditional statement in the java server pages. Body content is evaluated only when the condition evaluates to true.

Example:

if.jsp




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>If Tag</title>
   </head>
   <body>
      <h1>
         <c:set var="marks" value="80"/>
         <c:if test="${marks>33 }">
            Qualified!!
         </c:if>
      </h1>
   </body>
</html>

Output:

Qualified!!

c:forTokens

It is used to iterate over given tokens separated by the specified delimiters.

Example

forToken.jsp




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>ForToken Tag</title>
   </head>
   <body>
      <h1>
         <c:forTokens var="token" items="10-20-30-40-50" delims="-">
            <c:out value="${token }"/>
         </c:forTokens>
      </h1>
   </body>
</html>

Output:

10 20 30 40 50

c:choose, c:when, c:otherwise

It is used to include a conditional statement on the page. It allows multiple conditions similar to if-else ladder or switch case statements. 

<c:choose>: It marks the beginning of conditional and encloses <c:when> and <c:otherwise>. 

<c:when>: It is used to provide the condition and encloses the body to be executed if the condition evaluates to true. 

<c:otherwise>: It encloses the content to be executed if all the above conditions evaluate as false. 

Example:

choose.jsp




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Choose Tag</title>
   </head>
   <body>
      <h1>
         <c:set var="marks" value="95"/>
         <c:choose>
            <c:when test="${marks>90}">A Grade</c:when>
            <c:when test="${marks>80}">B Grade</c:when>
            <c:when test="${marks>70}">C Grade</c:when>
            <c:when test="${marks>60}">D Grade</c:when>
            <c:when test="${marks>50}">E Grade</c:when>
            <c:otherwise>Not satisfactory</c:otherwise>
         </c:choose>
            
      </h1>
   </body>
</html>

Output:

A Grade

c:url

It is used to create a formatted url along with parameters using a nested <c:param> tag. 

Example:




<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      <title>Url Tag</title>
   </head>
   <body>
      <h1>
         <a href = "
         <c:url value = "/jsp/welcome.jsp"/>
         ">Click here</a>
      </h1>
   </body>
</html>

Output:

Click here

Article Tags :