Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaJSTL Core Tags

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:

XML




<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. 

  • prefix: c
  • uri: http://java.sun.com/jsp/jstl/core

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

  • catch 
  • choose 
  • if
  • import
  • forEach
  • forTokens
  • out
  • otherwise 
  • param
  • redirect 
  • remove 
  • set
  • url 
  • when

c:set

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

  • Tag handler class:  org.apache.taglibs.standard.tag.rt.core.SetTag
  • Body content: jsp
  • Attributes: 
    • var: It is used to provide the name of the variable. 
    • value: It is used to provide the value for the variable. It can contain any expression. 
    • scope: It is used to set the scope of the variable.
    • target: It is used to provide the target object whose property is to be set.
    • property: It is used to specify the name of the property to be set for the target object.

Example

set.jsp

HTML




<%@ 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.

  • Tag handler class: org.apache.taglibs.standard.tag.common.core.RemoveTag
  • Body-content: empty
  • Attributes: 
    • var: It is used to specify the variable to be removed.

Example

remove.jsp

HTML




<%@ 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 (<%=  %).

  • Tag handler class: org.apache.taglibs.standard.tag.rt.core.OutTag.
  • Body content: jsp
  • Attributes:
    • value: It is used to provide the value to be displayed.
    • default: It is used to provide the default value, in case, the value provided is null.

Example:

out.jsp

HTML




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.

  • Tag handler class: org.apache.taglibs.standard.tag.rt.core.ForEachTag
  • Body content: jsp
  • Attributes:
    • items: It is used to provide a collection of objects to iterate over.
    • begin: It is used to specify the beginning of the iteration.
    • end: It is used to specify the end of the iteration.
    • step: It is used to provide the steps to be taken between two consecutive iterations. 
    • var: It is used to provide a variable for a current item of the iteration.

Example:

forEach.jsp

HTML




<%@ 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.

  • Tag handler class: org.apache.taglibs.standard.tag.rt.core.ImportTag
  • Body content: jsp
  • Attributes:
    • url: It is used to specify the url of the resource to be imported.
    • var: It is used to provide the var in which contents of the imported resource are to be stored.
    • scope: It is used to specify the scope.

Example:

import.jsp

HTML




<%@ 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. 

  • Tag handler class: org.apache.taglibs.standard.tag.rt.core.ParamTag
  • Body content: jsp
  • Attributes: 
    • name: It is used to provide the name of the parameter. 
    • value: It is used to provide value for the parameter. 

c:redirect

It is used to redirect a page to another url.  

  • Tag handler class: org.apache.taglibs.standard.tag.rt.core.RedirectTag
  • Body content: jsp
  • Attributes:
    • url: It is used to provide the url of the resource to be redirected to. 

Example:

redirect.jsp

HTML




<%@ 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

HTML




<%@ 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.

  • Tag handler class: org.apache.taglibs.standard.tag.common.core.CatchTag
  • Body-content: jsp
  • Attributes:
    • var: used to store the exception thrown by body-content.

Example:

catch.jsp

HTML




<%@ 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.

  • Tag handler class: org.apache.taglibs.standard.tag.rt.core.IfTag
  • Body-content: jsp
  • Attributes:
    • test: used to provide the testing condition.

Example:

if.jsp

HTML




<%@ 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.

  • Tag handler class: org.apache.taglibs.standard.tag.rt.core.ForTokensTag
  • Body-content: jsp
  • Attributes:
    • items: It is used to provide the collection of objects to iterate over.
    • begin: It is used to specify the beginning of the iterations.
    • end: It is used to specify the end of the iterations.
    • step: It is used to provide the steps to be taken between to consecutive iterations
    • var: used to provide a variable for a current item of the iteration.
    • delims: used to provide a set of delimiters.

Example

forToken.jsp

HTML




<%@ 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>. 

  • Tag handler class: org.apache.taglibs.standard.tag.common.core.ChooseTag
  • Body content: jsp
  • Attributes: no attribute 

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

  • Tag handler class: org.apache.taglibs.standard.tag.rt.core.WhenTag
  • Body content: jsp
  • Attributes:
    • test: It is used to provide the condition. 

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

  • Tag handler class: org.apache.taglibs.standard.tag.common.core.OtherwiseTag
  • Body content: jsp
  • Attributes: no attribute 

Example:

choose.jsp

HTML




<%@ 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. 

  • Tag handler class: org.apache.taglibs.standard.tag.rt.core.UrlTag
  • Body content: jsp
  • Attributes:
    • var: It is used to provide the variable that holds the url. 
    • scope: It is used to set the scope. 
    • value: It is used to provide the url to be formatted. 

Example:

HTML




<%@ 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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments