JSP Tutorial - JSP Standard Tag Library JSTL

The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags.

The JSTL tags can be grouped as:

  • Core Tags

  • Formatting tags

  • SQL tags

  • XML tags

  • JSTL Functions

Core Tags

The following code shows the syntax to include JSTL Core library in JSP:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:out >Like <%= ... >, but for expressions.
<c:set >Sets the result of an expression evaluation
<c:remove >Removes a scoped variable from a particular scope, if specified.
<c:catch>Catches Throwable in its body and optionally exposes it.
<c:if>if statement.
<c:choose>Conditional tag for mutually exclusive conditional operations, marked by <when> and <otherwise>
<c:when>Work with <choose> to include its body if the condition is 'true'.
<c:otherwise >Together with <choose> to run if all prior conditions are 'false'.
<c:import>Retrieves a URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'.
<c:forEach >Iteration tag.
<c:forTokens>Iterates over tokens, separated by the supplied delimeters.
<c:param>Adds a parameter to a containing 'import' tag's URL.
<c:redirect >Redirects to a new URL.
<c:url>Creates a URL with optional query parameters

Formatting tags

JSTL formatting tags formats and displays text, the date/time, and numbers.

The following code shows how to include format tags.

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:formatNumber>Format numerical value.
<fmt:parseNumber>Parses the string to a number, currency, or percentage.
<fmt:formatDate>Format a date/time
<fmt:parseDate>Parses the string to create a date/time
<fmt:bundle>Loads a resource bundle.
<fmt:setLocale>Set the given locale.
<fmt:setBundle>Loads a resource bundle
<fmt:timeZone>Set the time zone for time formatting
<fmt:setTimeZone>Stores the given time zone in the time zone configuration variable
<fmt:message>To display an internationalized message.
<fmt:requestEncoding>Sets the request character encoding

SQL tags

JSTL SQL tag library is used to work with databases.

Use the following syntax to include JSTL SQL library in JSP:

<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<sql:setDataSource>Creates a DataSource
<sql:query>Executes the SQL query in its body or the sql attribute.
<sql:update>Executes the SQL update in its body or the sql attribute.
<sql:param>Sets a parameter in an SQL statement to the specified value.
<sql:dateParam>Sets a parameter in an SQL statement to the specified java.util.Date value.
<sql:transaction >Work with transaction.

XML tags

JSTL XML tags are used to create and manipulate XML documents.

We can use the following syntax to include JSTL XML library in a JSP.

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

It can parse XML, transform XML data, and work with XPath expressions.

It depends on the following two jar files.

Install those jar files into your <Tomcat Installation Directory>\lib:

<x:out>Like <%= ... >, but for XPath expressions.
<x:parse>Parse XML data in an attribute or in the tag body.
<x:set >Sets value to a variable in an XPath expression.
<x:if >if statement on a XPath expression
<x:forEach>To loop over nodes in an XML document.
<x:choose>Conditional tag, and work with <when> and <otherwise>
<x:when >Work with <choose> for 'true' condition
<x:otherwise >Work with of <choose> for 'false' condition
<x:transform >Applies an XSL transformation on a XML document
<x:param >Work with the transform tag to set a parameter in the XSLT stylesheet.

JSTL Functions

JSTL has standard functions for string manipulation.

Use the following syntax to include JSTL Functions library in a JSP

<%@ taglib prefix="fn" 
           uri="http://java.sun.com/jsp/jstl/functions" %>
Function Description
fn:contains() Tests if a string contains a substring.
fn:containsIgnoreCase() Tests if a string contains a substring in a case insensitive way.
fn:endsWith() Tests if an input string ends with the suffix.
fn:escapeXml() Escapes characters for XML markup.
fn:indexOf() Returns the index within a string of the first occurrence of a substring.
fn:join() Joins all elements of an array into a string.
fn:length() Returns the number of items in a collection, or the number of characters in a string.
fn:replace() Replace in an input string with a given string.
fn:split() Splits a string into an array of substrings.
fn:startsWith() If an input string starts with the prefix.
fn:substring() Returns a sub string.
fn:substringAfter() Returns a sub string following a specific substring.
fn:substringBefore() Returns a subset of a string before a specific substring.
fn:toLowerCase() Converts a string to lower case.
fn:toUpperCase() Converts a string to upper case.
fn:trim() Removes white spaces from both ends of a string.