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.
Table of Contents
Common JSTL Core Tags:
1. Â c:out
- Purpose: Â Outputs the value of an expression, similar to <%= … %>.
- Example: Â <c:out value=”${name}” />
2. Â c:set
- Â Â Purpose: Â Sets the value of a variable in a specified scope.
- Â Â 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>