Updates
  • Starting New Weekday Batch for Full Stack Java Development on 27 September 2025 @ 03:00 PM to 06:00 PM
  • Starting New Weekday Batch for MERN Stack Development on 29 September 2025 @ 04:00 PM to 06:00 PM

JSTL

JSTL stands for JSP Standard Tag Library.
JSTL is a collection of utility tags implemented by SUN that can be used by developers in JSP.

JSTL tags are divided into the following types:

Core Tags
Formating Tags
SQL Tags
XML Tags

Steps to use JSTL tags:

1) Copy jstl.jar and standard.jar to your project WEB-INF/lib folder.
2) Use the following taglib directive in any JSP.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

List of JSTL Core Tags

1) <c:out>
2) <c:set>
3) <c:remove>
4) <c:if>
5) <c:choose>
6) <c:when>
7) <c:otherwise>
8) <c:forEach>
9) <c:import>
10) <c:redirect>
11) <c:param>
12) <c:url>

<c:out>

<c:out> is used to display data to JSP output stream.
Attributes : value.

Usage:

<c:out value="Som"/>
<c:out value="msg"/>
<c:out value="${msg}"/>

<c:set>

<c:set> is used to set the attribute in the required scope. Attributes : var, value, scope

Usage:

<c:set var="EM" value="som@jtc.com" scope="session"/>
=> session.setAttribute("EM","som@jtc");

<c:remove>

<c:remove> is used to remove the attribute from the required scope.
Attributes : var,scope

Usage:

<c:remove var="EM" scope="session"/>
=> session.removeAttribute("EM");

<c:if>

<c:if> is used to perform simple conditional checks.
Attributes : test

Usage:

<c:if test="${sname eq 'som'}">
Your name is Som
</c:if>
<c:if test="${sname ne 'som'}">
Your name is Not Som
</c:if>

<c:choose>, <c:when> and <c:otherwise>

These tags are used to perform simple conditional checks like switch statement.
Attributes :
choose - no attributes available
when - test
otherwise - no attributes available

Usage:

<c:choose>
<c:when test="${sid eq 99}">
Id is 99
</c:when>
<c:when test="${sid eq 88}">
Id is 88
</c:when>
<c:otherwise>
Id is unknown
</c:otherwise>
</c:choose>

Files Required For Program

1. Lab1.jsp
2. Lab2.jsp
3. Lab3.jsp
4. web.xml

1. Lab1.jsp

            
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html> 
<html>
   <body>
      <h3> I am Lab1.jsp </h3>
      <h4>
         <c:out value="Hello Guys" />
      </h4>
      <c:set var="SID" value="101" scope="request"/>
      <c:set var="Email" value="som@jtc.com" scope="session"/>
      <c:set var="Phone" value="12345" scope="application"/>
      <h4> Sid : ${requestScope.SID}</h4>
      <h4> Email : ${sessionScope.Email} </h4>
      <h4> Phone : ${applicationScope.Phone} </h4>
      <hr/>
      <h4>
         Sid : 
         <c:out value="${requestScope.SID}"/>
      </h4>
      <h4>
         Email : 
         <c:out value="${sessionScope.Email} "/>
      </h4>
      <h4>
         Phone : 
         <c:out value="${applicationScope.Phone} "/>
      </h4>
      <hr/>
      <c:remove var="SID" scope="request"/>
      <c:remove var="Email" scope="session"/>
      <c:remove var="Phone" scope="application"/>
      <hr/>
      <h4> Sid : ${requestScope.SID}</h4>
      <h4> Email : ${sessionScope.Email} </h4>
      <h4> Phone : ${applicationScope.Phone} </h4>
      <hr/>
   </body>
</html>
        
            

2. Lab2.jsp

            
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html> 
<html>
   <body>
      <h3> I am Lab2.jsp </h3>
      <c:if test="true">
         <h4> I will be displayed always</h4>
      </c:if>
      <c:if test="false">
         <h4> I will never be displayed </h4>
      </c:if>
      <c:set var="SID" value="101" scope="request"/>
      <c:if test="${requestScope.SID eq 101}">
         <h4> Hello Guys !!!! </h4>
         <h4> My Sid is 101 </h4>
      </c:if>
      <c:if test="${requestScope.SID ne 101}">
         <h4> I dont know My Sid </h4>
      </c:if>
      <c:set var="ShowBooks" value="FALSE" scope="session"/>
      <c:if test="${ShowBooks eq 'TRUE' }">
         <h4> Display the Books Here </h4>
      </c:if>
      <c:if test="${ShowBooks eq 'FALSE' }">
         <h4> OOOPS !! No Books </h4>
      </c:if>
   </body>
</html>
        
            

3. Lab3.jsp

            
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html> 
<html>
   <body>
      <h3> I am Lab3.jsp </h3>
      <c:set var="SID" value="103" scope="session"/>
      <c:choose>
         <c:when test="${SID eq 101}">
            <h4> My Sid is 101 </h4>
         </c:when>
         <c:when test="${SID eq 102}">
            <h4> My Sid is 102 </h4>
         </c:when>
         <c:when test="${SID eq 103}">
            <h4> My Sid is 103 </h4>
         </c:when>
         <c:otherwise>
            <h4> I dont know My Sid </h4>
         </c:otherwise>
      </c:choose>
      <c:set var="OpType" value="View" scope="session"/>
      <c:choose>
         <c:when test="${OpType eq 'Add'}">
            <h4> Add Book Operation </h4>
         </c:when>
         <c:when test="${OpType eq 'Update'}">
            <h4> Update Book Operation </h4>
         </c:when>
         <c:when test="${OpType eq 'Delete'}">
            <h4> Delete Book Operation </h4>
         </c:when>
         <c:otherwise>
            <h4> OOOPS !! I dont Know </h4>
         </c:otherwise>
      </c:choose>
   </body>
</html>
        
            

4. web.xml

            
<?xml version="1.0" encoding="UTF-8"?> 
<web-app …>
   <display-name>Lab16</display-name>
   <welcome-file-list>
      <welcome-file>Lab1.jsp</welcome-file>
   </welcome-file-list>
</web-app>
        
            

<c:forEach>

<c:forEach> is used to access the elements of collection or array.
Attributes : var , items , start , end , step , varStatus

Usage:

Using <c:forEach>,You can access the following types of data.

1) Collection of Strings, Wrappers and Date.
2) Collection of Collections
3) Collection of User defined class object
4) Map objects
5) Collection of Map objects

Collection of Strings,Wrappers and Date

In Servlet In jsp
ArrayList al=new ArrayList();
al.add(new Integer(123));
String str[]={"dd","ss",...};
req.setAttribute("AL",al);
ses.setAttribute("STR",str);
<c:forEach var="x" items="${requestScope.AL}">
<font color=red>${x}</font> <br>
</c:forEach>
<c:forEach var="x" items="${sessionScope.STR}">
<br> ${x}
</c:forEach>

Collection of Collections

In Servlet In jsp
ArrayList al=new ArrayList();
ArrayList al1=new ArrayList();
al1.add(new Integer(123));
ArrayList al2=new ArrayList();
al2.add(new Integer(123));
al.add(al1);
al.add(al2);
ses.setAttribute("AL",al);
<c:forEach var="list" items="${sessionScope.AL}">
<c:forEach var="x" items="${list}">
<br>${x}
</c:forEach>
</c:forEach>

Collection of User defined class object

In Servlet In jsp
ArrayList al=new ArrayList();
...
al.add(cust1);
al.add(cust2);
ses.setAttribute("AL",al);
<c:forEach var="cust" items="${sessionScope.AL}">
<br>Cid : ${cust.cid}
<br>Cname : ${cust.cname}
<br>Email : ${cust.email}
<br>Street: ${cust.address.street}
<br>City: ${cust.address.city}
</c:forEach>

Map object

In Servlet In jsp
<br> Sid : ${sessionScope.HM["sid"]}
<br> Sname : ${sessionScope.HM["sname"]}
<c:forEach var="entry"
items="${sessionScope.HM}">
<br>${entry.key} : ${entry.value}
</c:forEach>
<br> Sid : ${sessionScope.HM["sid"]}
<br> Sname : ${sessionScope.HM["sname"]}
<c:forEach var="entry"
items="${sessionScope.HM}">
<br>${entry.key} : ${entry.value}
</c:forEach>

Collection of Map objects

In Servlet In jsp
HashMap hm=new HashMap();
hm.put("sid","11");
hm.put("sname","som");
..
ArrayList al=new ArrayList();
al.add(hm);
al.add(hm);
ses.setAttribute("AL",al);
<c:forEach var="map" items="${sessionScope.AL}">
<c:forEach var="entry" items="${map}">
<br>${entry.key} : ${entry.value}
</c:forEach>
</c:forEach>

Files Required for Program

1. Lab1.jsp
2. Lab2.jsp
3. web.xml

1. Lab1.jsp

            
<%@page import="java.util.*"%> 
<%@ taglib prefix="jtc" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html> 
<html>
   <body>
      <h3> I am Lab1.jsp </h3>
      <hr>
      <jtc:forEach var="i" begin="1" end="5" step="1">
         <h4> Hello Guys, Value is ${i}</h4>
      </jtc:forEach>
      <hr>
      <jtc:forEach var="i" begin="1" end="5" step="2">
         <h4> Hai Guys, Value is ${i}</h4>
      </jtc:forEach>
      <hr>
      <% 
         List<String> courseList = new ArrayList<>(); 
         courseList.add("Java8"); 
         courseList.add("Spring 5"); 
         courseList.add("Spring Boot"); 
         courseList.add("DevOps"); 
         session.setAttribute("MyCourseList",courseList); 
         %> 
      <h3> Courses @ JTC </h3>
      <ul>
         <jtc:forEach var="mycourse" items="${MyCourseList}">
            <li>
               <h4> ${mycourse}</h4>
            </li>
         </jtc:forEach>
      </ul>
   </body>
</html>
        
            

2. Lab2.jsp

            
<%@page import="java.util.*"%> 
<%@ taglib prefix="jtc" uri="http://java.sun.com/jsp/jstl/core" %> 
<!DOCTYPE html> 
<html>
   <body>
      <h3> I am Lab2.jsp </h3>
      <hr>
      <% 
         String msg="Hello Guys How are you Now"; 
         session.setAttribute("MSG",msg); 
         %> 
      <h3> Message from JTC </h3>
      <ul>
         <jtc:forTokens var="myword" items="${MSG}" delims=" ">
            <li>
               <h4> ${myword}</h4>
            </li>
         </jtc:forTokens>
      </ul>
      <hr>
      <% 
         String courseList="Java8,Spring5,Spring Boot2,DevOps,React"; 
         session.setAttribute("MyCourseList",courseList); 
         %> 
      <h3> Courses from JTC </h3>
      <ul>
         <jtc:forTokens var="mycourse" items="${MyCourseList}" delims=",">
            <li>
               <h4> ${mycourse}</h4>
            </li>
         </jtc:forTokens>
      </ul>
   </body>
</html>
        
            

3. Lab3.jsp

            
<?xml version="1.0" encoding="UTF-8"?> 
<web-app …>
   <display-name>Lab17</display-name>
   <welcome-file-list>
      <welcome-file>Lab1.jsp</welcome-file>
   </welcome-file-list>
</web-app>
        
            

<c:import>

<c:import> is used to include the resource(html or jsp).
Attributes: url

Usage:

<jsp:include page="header.jsp"/>
<c:import url="header.jsp"/>
<c:import url="http://www.google.co.in"/>
<c:import url="http://www.jtcindia.org/contactus.jsp"/>
<jsp:include> can include resources that are on the same server.
<c:import> can include resources that are on the same server or on different servers.

<c:redirect>

<c:redirect> is used to redirect the request to the specified resource (html or jsp).
Attributes: url

Usage:

<c:redirect url="header.jsp"/>
<c:redirect url="http://www.google.co.in"/>
<c:redirect url="http://www.jtcindia.org/contactus.jsp"/>
<jsp:forward> can forward requests to resources that are in the same application.
<c:redirect> can forward requests to resources that are in the same application and different applications running on the same server or different servers.

<c:param>

<c:param> is used to define parameters.
<c:param> must be used along with <c:import> and <c:redirect>.

Usage:

<c:param name="email" value="som@jtc.com"/>

<c:url>

<c:url> is used to encode the url with the sessionid.
Attributes: value

Usage:

<c:url value="hello.jsp"/>
<a href="hello.jsp"> Click Here</a>
<a href='<c:url value="hello.jsp"/>'> Click Here</a>
<a href='<%= response.encodeURL("hello.jsp")%>'> Click Here</a>