Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JSP Standard Tag Library (JSTL)

JSP Standard Tag Library (JSTL)

The JSP Standard Tag Library (JSTL) is a collection of JSP tags that encapsulates the common core functionality of JSP applications.

JSTL supports general, structured tasks such as iteration, conditional evaluation, XML document manipulation, internationalization tags, and SQL tags. In addition, it provides a framework for using custom tags integrated with JSTL.

Based on the functionality provided by JSTL tags, they can be divided into 5 categories.


JSTL Library Installation

The steps to install the JSTL library on Apache Tomcat are as follows:

Download the binary package (jakarta-taglibs-standard-current.zip) from Apache's standard tag library.

Download jakarta-taglibs-standard-1.1.2.zip the package and extract it, then copy the jakarta-taglibs-standard-1.1.2/lib/ two jar files under the following directory:standard.jar and jstl.jar and copy the files to /WEB-INF/lib/ the following directory.

Copy the required tld files under tld to the WEB-INF directory.

Next, we add the following configuration to the web.xml file:







    

    

    http://java.sun.com/jsp/jstl/fmt

    /WEB-INF/fmt.tld

    

    

    http://java.sun.com/jsp/jstl/fmt-rt

    /WEB-INF/fmt-rt.tld

    

    

    http://java.sun.com/jsp/jstl/core

    /WEB-INF/c.tld

    

    

    http://java.sun.com/jsp/jstl/core-rt

    /WEB-INF/c-rt.tld

    

    

    http://java.sun.com/jsp/jstl/sql

    /WEB-INF/sql.tld

    

    

    http://java.sun.com/jsp/jstl/sql-rt

    /WEB-INF/sql-rt.tld

    

    

    http://java.sun.com/jsp/jstl/x

    /WEB-INF/x.tld

    

    

    http://java.sun.com/jsp/jstl/x-rt

    /WEB-INF/x-rt.tld

    

    



To use any library, you must include at the top of every JSP file the tag.


Core Tags

Core tags are the most commonly used JSTL tags. The syntax for referencing the core tag library is as follows:



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

tag Description
Used to display data in JSP, just like<%= ... >
Used to save data
Used to delete data
Used to handle exceptional conditions that produce errors and store the error information
The same as the if used in ordinary programs
By itself it serves only asanda parent tag of
a child tag of, used to determine whether a condition holds
a child tag of, following thetag, when thetag evaluates to false, this is executed
Retrieves an absolute or relative URL and then exposes its content to the page
The basic iteration tag, which accepts various collection types
Splits content according to a specified delimiter and iterates over the output
Used to pass parameters to an included or redirected page
Redirects to a new URL.
Creates a URL using optional query parameters

Formatting Tags

JSTL formatting tags are used to format and output text, dates, times, and numbers. The syntax for referencing the formatting tag library is as follows:



<%@ taglib prefix="fmt" 

           uri="http://java.sun.com/jsp/jstl/fmt" %>

tag Description
fmt:formatNumber Formats a number using a specified format or precision
fmt:parseNumber Parses a string representing a number, currency, or percentage
fmt:formatDate Formats dates and times using a specified style or pattern
fmt:parseDate Parses a string representing a date or time
fmt:bundle Binds resources
fmt:setLocale Specifies the locale
fmt:setBundle Binds resources
fmt:timeZone Specifies the time zone
fmt:setTimeZone Specifies the time zone
fmt:message Displays resource bundle configuration information
fmt:requestEncoding Sets the character encoding of the request

SQL Tags

The JSTL SQL tag library provides tags for interacting with relational databases (Oracle, MySQL, SQL Server, and so on). The syntax for referencing the SQL tag library is as follows:



<%@ taglib prefix="sql" 

           uri="http://java.sun.com/jsp/jstl/sql" %>

tag Description
sql:setDataSource Specifies a data source
sql:query Runs an SQL query statement
sql:update Runs an SQL update statement
sql:param Sets the parameters in an SQL statement to specified values
sql:dateParam Sets the date parameters in an SQL statement to the specified java.util.Date object value
sql:transaction Provides nested database action elements within a shared database connection, running all statements as a single transaction

XML tags

The JSTL XML tag library provides tags for creating and manipulating XML documents. The syntax for referencing the XML tag library is as follows:



<%@ taglib prefix="x" 

           uri="http://java.sun.com/jsp/jstl/xml" %>

Before using the xml tags, you must copy the XML and XPath related packages to your\lib directory:

tag Description
The difference between<%= ... >, similar, but only for XPath expressions
Parses XML data
Sets an XPath expression
Evaluates an XPath expression; if true, the content of the body is executed, otherwise the body is skipped
Iterates over nodes in an XML document
anda parent tag of
a child tag of, used for conditional evaluation
a child tag of, whenevaluates to false, it is executed
Applies an XSL transformation to an XML document
The difference betweenUsed together to set an XSL stylesheet

JSTL Functions

JSTL includes a series of standard functions, most of which are general-purpose string processing functions. The syntax for referencing the JSTL function library is as follows:



<%@ taglib prefix="fn" 

           uri="http://java.sun.com/jsp/jstl/functions" %>

function Description
fn:contains() Tests whether the input string contains a specified substring
fn:containsIgnoreCase() Tests whether the input string contains a specified substring, case-insensitively
fn:endsWith() Tests whether the input string ends with a specified suffix
fn:escapeXml() Escapes characters that can serve as XML markup
fn:indexOf() Returns the position at which a specified string appears in the input string
fn:join() Joins the elements of an array into a string and outputs it
fn:length() Returns the length of a string
fn:replace() Replaces a specified part of the input string with a specified string and returns the result
fn:split() Splits a string by a specified delimiter into an array of substrings and returns it
fn:startsWith() Tests whether the input string starts with a specified prefix
fn:substring() Returns a subset of a string
fn:substringAfter() Returns the subset of a string after a specified substring
fn:substringBefore() Returns the subset of a string before a specified substring
fn:toLowerCase() Converts the characters in a string to lowercase
fn:toUpperCase() Converts the characters in a string to uppercase
fn:trim() Removes leading and trailing whitespace