What is JSTL (JavaServer Pages Standard Tag Library)?
JSTL is a collection of useful tags that encapsulate core functionality commonly used in Java-based web applications. These tags simplify the development of JSP pages by removing the need for embedding Java code in the page.
JSTL provides tags for common tasks like iteration, conditionals, formatting, and URL manipulation, improving readability and maintainability of your JSP code.
Advantages of JSTL
- Code Cleanliness: Reduces the need for Java code embedded in JSP pages, leading to cleaner and more maintainable code.
- Reusable Components: Provides reusable and standardized tags for common functionalities, reducing redundancy.
- Separation of Concerns: Enhances the separation of business logic from presentation logic by using tags for common operations.
- Improved Performance: As it reduces the amount of Java code in JSP, it improves page rendering performance.
JSTL Tags
JSTL provides several categories of tags to handle different tasks:
- Core Tags: Basic tags like
<c:forEach>,<c:if>,<c:set>, etc. - Formatting Tags: Tags for formatting data such as dates, numbers, and messages.
- SQL Tags: Tags for database operations like
<sql:query>. - XML Tags: Tags to handle XML processing.
Core JSTL Tags Examples
The JavaServer Pages Standard Tag Library (JSTL) provides a set of core tags that are commonly used in JSP pages. These tags help to perform repetitive tasks such as iteration, conditional rendering, and variable manipulation. Below are examples of various core JSTL tags:
1. <c:set> - Setting a Variable
The <c:set> tag is used to set a variable in the scope of the page, request, session, or application.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="userName" value="John Doe" />
<p>Hello, ${userName}!</p>
This sets a variable userName with the value "John Doe" and displays it in a paragraph.
2. <c:forEach> - Iteration
The <c:forEach> tag is used to iterate over a collection (e.g., List, Set, Map, etc.) and output its elements.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="fruits" value="apple,banana,orange" />
<c:forEach var="fruit" items="${fruits}">
<p>${fruit}</p>
</c:forEach>
Here, we define a list of fruits and use <c:forEach> to iterate over each fruit and display it in a paragraph.
3. <c:if> - Conditional Rendering
The <c:if> tag is used to conditionally include content based on a specified condition.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="isLoggedIn" value="true" />
<c:if test="${isLoggedIn}">
<p>Welcome back, user!</p>
</c:if>
This tag checks if the isLoggedIn variable is true and displays a message if the condition is satisfied.
4. <c:choose>, <c:when>, <c:otherwise> - Conditional Logic
The <c:choose> tag is used for more complex conditional logic, allowing for multiple <c:when> and <c:otherwise> blocks.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:choose>
<c:when test="${age > 18}">
<p>You are an adult.</p>
</c:when>
<c:otherwise>
<p>You are a minor.</p>
</c:otherwise>
</c:choose>
In this example, we use <c:choose> for complex conditional logic, checking the user's age and displaying different messages based on the result.
5. <c:import> - Importing Content
The <c:import> tag allows you to import content from a URL or file into the current page.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:import url="header.html" />
This tag imports the content of an external file (e.g., header.html) and includes it in the current page.
6. <c:redirect> - Redirecting to Another Page
The <c:redirect> tag is used to perform an HTTP redirect to another URL.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:redirect url="https://www.example.com" />
This tag redirects the current request to the specified URL.
7. <c:remove> - Removing a Variable
The <c:remove> tag is used to remove a variable from a particular scope.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="message" value="Hello, world!" />
<c:remove var="message" />
<p>${message}</p>
This example sets a variable message and then removes it using <c:remove>.
8. <c:out> - Output
The <c:out> tag is used to output a variable or expression to the response.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:out value="${userName}" />
This tag outputs the value of the userName variable to the response.
Summary of Core JSTL Tags
| Tag | Description | Example |
|---|---|---|
| <c:set> | Sets a variable in the page, request, session, or application scope. | <c:set var="userName" value="John" /> |
| <c:forEach> | Iterates over a collection or array. | <c:forEach var="item" items="${items}">${item}</c:forEach> |
| <c:if> | Conditionally includes content based on a test condition. | <c:if test="${userLoggedIn}">Welcome!</c:if> |
| <c:choose> | Provides multiple conditional checks (like if-else). | <c:choose><c:when test="${age > 18}">Adult</c:when><c:otherwise>Minor</c:otherwise></c:choose> |
| <c:import> | Imports content from a URL or file into the JSP page. | <c:import url="header.html" /> |
| <c:redirect> | Redirects the request to another URL. | <c:redirect url="https://www.example.com" /> |
| <c:remove> | Removes a variable from a scope. | <c:remove var="message" /> |
| <c:out> | Outputs a variable or expression to the response. | <c:out value="${userName}" /> |
JSTL Formatting Tags Examples
JSTL provides several tags for formatting different types of data such as numbers, dates, and strings. These tags help to format data in a way that is locale-sensitive, making it easier to display content in different formats for different regions.
1. <fmt:formatNumber> - Formatting Numbers
The <fmt:formatNumber> tag is used to format numerical values (such as currency, percentage, or general number) according to a specific pattern or locale.
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<fmt:formatNumber value="1234567.89" pattern="###,###.##" />
This example formats the number 1234567.89 with commas separating thousands and up to two decimal places.
2. <fmt:formatDate> - Formatting Dates
The <fmt:formatDate> tag is used to format dates and times into a specified pattern.
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<fmt:formatDate value="${currentDate}" pattern="yyyy-MM-dd HH:mm:ss" />
This example formats the date stored in the variable currentDate into the format yyyy-MM-dd HH:mm:ss.
3. <fmt:formatCurrency> - Formatting Currency
The <fmt:formatCurrency> tag is used to format a number as a currency value according to the locale.
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<fmt:formatCurrency value="12345.67" />
This example formats the number 12345.67 as a currency value, displaying it in the appropriate currency format based on the locale (e.g., "$12,345.67" in the US).
4. <fmt:message> - Message Formatting
The <fmt:message> tag is used to retrieve and display messages from a resource bundle. This is useful for internationalization and localization of content.
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<fmt:message key="welcome.message" />
This tag retrieves the value associated with the key welcome.message from the resource bundle and displays it.
5. <fmt:bundle> - Loading Resource Bundles
The <fmt:bundle> tag is used to load a resource bundle that contains messages for different languages and locales.
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<fmt:bundle basename="messages">
<fmt:message key="greeting" />
</fmt:bundle>
This example loads a resource bundle named "messages" and retrieves the message associated with the key greeting.
Summary of JSTL Formatting Tags
| Tag | Description | Example |
|---|---|---|
| <fmt:formatNumber> | Formats a number according to a specified pattern or locale. | <fmt:formatNumber value="1234567.89" pattern="###,###.##" /> |
| <fmt:formatDate> | Formats a date or time into a specified pattern. | <fmt:formatDate value="${currentDate}" pattern="yyyy-MM-dd HH:mm:ss" /> |
| <fmt:formatCurrency> | Formats a number as a currency value. | <fmt:formatCurrency value="12345.67" /> |
| <fmt:message> | Retrieves and displays a message from a resource bundle. | <fmt:message key="welcome.message" /> |
| <fmt:bundle> | Loads a resource bundle and allows message formatting within it. | <fmt:bundle basename="messages"><fmt:message key="greeting" /></fmt:bundle> |