Explain the JSTL core tags with example

Explain the JSTL core tags with example

The JavaServer Pages Standard Tag Library (JSTL) is a collection of custom tags that encapsulate core functionality common to many JSP applications. The core library provides tags for common tasks such as iteration, conditionals, and variable manipulation.

JSTL core tags

Common JSTL Core Tags:

1.  c:out

  • Purpose:  Outputs the value of an expression, similar to <%= … %>.
  • Example:  <c:out value=”${name}” />

2.  c:set

  1.   Purpose:  Sets the value of a variable in a specified scope.
  2.   Example:  <c:set var=”userName” value=”John Doe” />

3.  c:remove

  • Purpose:  Removes a variable from a specified scope.
  • Example:  <c:remove var=”userName” scope=”session” />

4.  c:if

    Purpose:  Conditionally executes its body content.

Example
 jsp

     <c:if test="${user.loggedIn}">

         <p>Welcome, ${user.name}!</p>

     </c:if>

5.  c:choose, c:when, c:otherwise

    Purpose:  Provides a way to create multiple conditional branches.

  

Example
-  Example:

     jsp

     <c:choose>

         <c:when test="${user.role == 'admin'}">

             <p>Admin Panel</p>

         </c:when>

         <c:otherwise>

             <p>User Dashboard</p>

         </c:otherwise>

     </c:choose>

6.  c:forEach

    Purpose:  Iterates over a collection of items.

Example
jsp

     <c:forEach var="item" items="${itemList}">

         <p>${item.name}</p>

     </c:forEach>

7.  c:forTokens

    Purpose:  Iterates over tokens separated by a delimiter.

Example
jsp

     <c:forTokens var="token" items="apple,orange,banana" delims=",">

         <p>${token}</p>

     </c:forTokens>

8.  c:import

  • Purpose:  Imports the content of a URL or file into the JSP page.
  • Example:  <c:import url=”header.html” />

9. c:catch

 Purpose:  Catches any throwable that occurs in its body and allows handling of the exception.

Example
 jsp

     <c:catch var="exception">

         <c:out value="${1 / 0}" />

     </c:catch>

     <c:if test="${not empty exception}">

         <p>Error: ${exception.message}</p>
     </c:if>

Example
Example in JSP:
Here's a simple JSP page using various JSTL core tags:

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

<html>
<head>
    <title>JSTL Core Tags Example</title>
</head>
<body>
    <h1>JSTL Core Tags Example</h1>
    
    <!-- c:out -->
    <c:set var="name" value="John Doe" />
    <p>Name: <c:out value="${name}" /></p>
    
    <!-- c:if -->
    <c:if test="${name == 'John Doe'}">
        <p>Welcome, John Doe!</p>
    </c:if>
    
    <!-- c:choose, c:when, c:otherwise -->
    <c:choose>
        <c:when test="${name == 'John Doe'}">
            <p>Hello, John Doe!</p>
        </c:when>
        <c:otherwise>
            <p>Hello, Guest!</p>
        </c:otherwise>
    </c:choose>
    
    <!-- c:forEach -->
    <c:set var="items" value="${['Item 1', 'Item 2', 'Item 3']}" />
    <ul>
        <c:forEach var="item" items="${items}">
            <li>${item}</li>
        </c:forEach>
    </ul>
    
    <!-- c:import -->
    <c:import url="footer.html" />
</body>
</html>

Homepage

Readmore