JSP Standard Tag Library (JSTL) Tutorial

The JSP Standard Tag Library (JSTL) is a collection of custom tags that provide core functionality common to many JSP applications. By using JSTL, you can simplify the JSP code, making it more readable and maintainable. In this tutorial, we will cover the core tags provided by JSTL and demonstrate their usage with examples.

What is JSTL?

JSTL is a standard library of tags that can be used in JSP pages to perform common tasks such as:

  • Iteration and conditionals
  • Formatting and internationalization
  • XML data processing
  • Database access

JSTL is part of the Java EE API and is divided into several sub-libraries:

  • Core (c)
  • Formatting (fmt)
  • SQL (sql)
  • XML (x)

Adding JSTL to Your Project

To use JSTL in your JSP pages, you need to include the JSTL library in your project. You can add the JSTL dependency to your pom.xml file if you are using Maven. Make sure to use the latest version available:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

Alternatively, you can download the JSTL JAR files and add them to your project's WEB-INF/lib directory.

JSTL Core Tags

The core tags in JSTL are the most commonly used tags. They include conditional tags, iteration tags, and others that help control the flow of the page.

Importing JSTL Core Library

To use JSTL core tags, you need to include the following directive at the top of your JSP page:

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

Common JSTL Core Tags

1. c:out

The c:out tag is used to display the result of an expression, similar to the <%= %> expression in JSP.

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

2. c:if

The c:if tag is used for conditional execution of code.

<c:if test="${user.loggedIn}">
    <p>Welcome, ${user.name}!</p>
</c:if>

3. c:choose, c:when, and c:otherwise

These tags are used for multiple conditional checks, similar to an if-else-if ladder.

<c:choose>
    <c:when test="${user.role == 'admin'}">
        <p>Admin Section</p>
    </c:when>
    <c:when test="${user.role == 'user'}">
        <p>User Section</p>
    </c:when>
    <c:otherwise>
        <p>Guest Section</p>
    </c:otherwise>
</c:choose>

4. c:forEach

The c:forEach tag is used for iterating over a collection of items.

<ul>
    <c:forEach var="item" items="${items}">
        <li>${item}</li>
    </c:forEach>
</ul>

5. c:set

The c:set tag is used to set the value of a variable.

<c:set var="message" value="Hello, JSTL!" />

JSTL Formatting Tags

The formatting tags are used to format text, numbers, and dates.

Importing JSTL Formatting Library

To use JSTL formatting tags, include the following directive:

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

Common JSTL Formatting Tags

1. fmt:formatNumber

Formats numbers, currency, and percentages.

<fmt:formatNumber value="${price}" type="currency" />

2. fmt:formatDate

Formats dates and times.

<fmt:formatDate value="${now}" pattern="yyyy-MM-dd" />

3. fmt:message

Retrieves localized messages from resource bundles.

<fmt:message key="welcome.message" />

JSTL SQL Tags

The SQL tags are used for interacting with databases directly from JSP pages.

Importing JSTL SQL Library

To use JSTL SQL tags, include the following directive:

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

Common JSTL SQL Tags

1. sql:setDataSource

Defines the data source to be used for SQL operations.

<sql:setDataSource var="dataSource" driver="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost:3306/mydb"
                   user="root" password="password"/>

2. sql:query

Executes a SQL query and stores the result.

<sql:query dataSource="${dataSource}" var="result">
    SELECT * FROM users
</sql:query>

3. sql:update

Executes a SQL update operation.

<sql:update dataSource="${dataSource}">
    INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]')
</sql:update>

JSTL XML Tags

The XML tags are used for processing XML data.

Importing JSTL XML Library

To use JSTL XML tags, include the following directive:

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

Common JSTL XML Tags

1. x:parse

Parses an XML document.

<x:parse var="doc" xml="${xmlData}" />

2. x:out

Outputs the result of an XPath expression.

<x:out select="$doc/root/element" />

Conclusion

JSTL provides a powerful set of tags that simplify JSP development by abstracting common tasks into easy-to-use tags. By incorporating JSTL into your JSP pages, you can improve the readability and maintainability of your code. Use the latest version of the JSTL library to ensure you have access to the latest features and improvements.

Comments