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 common, structured tasks such as iteration, conditional judgment, XML document manipulation, internationalization tags, and SQL tags. In addition to these, it also provides a framework for using custom tags that integrate with JSTL.
According to the functionality provided by JSTL tags, they can be divided into 5 categories.
- Core Tags
- Formatting Tags
- SQL Tags
- XML Tags
- JSTL Functions
JSTL Library Installation
The steps to install the JSTL library in Apache Tomcat are as follows:
The binary package (jakarta-taglibs-standard-current.zip) downloaded from Apache's Standard Tag Library.
- Official download address:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
- This site's download address:jakarta-taglibs-standard-1.1.2.zip
Downloadjakarta-taglibs-standard-1.1.2.zipthe package and extract it, thenjakarta-taglibs-standard-1.1.2/lib/the two jar files under:standard.jarandjstl.jarCopy the files to/WEB-INF/lib/under.
Copy the required tld files from the tld directory to the WEB-INF directory.
Next, add the following configuration to the web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/fmt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/fmt-rt</taglib-uri>
<taglib-location>/WEB-INF/fmt-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core-rt</taglib-uri>
<taglib-location>/WEB-INF/c-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/sql</taglib-uri>
<taglib-location>/WEB-INF/sql.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/sql-rt</taglib-uri>
<taglib-location>/WEB-INF/sql-rt.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/x</taglib-uri>
<taglib-location>/WEB-INF/x.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/x-rt</taglib-uri>
<taglib-location>/WEB-INF/x-rt.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
To use any library, you must include the<taglib>tag at the top of every JSP file.
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 |
|---|---|
| <c:out> | Used to display data in JSP, just like <%= ... > |
| <c:set> | Used to save data |
| <c:remove> | Used to delete data |
| <c:catch> | Used to handle exceptional conditions that produce errors, and to store the error information |
| <c:if> | Same as the if used in general programming |
| <c:choose> | Itself only serves as the parent tag of <c:when> and <c:otherwise> |
| <c:when> | A child tag of <c:choose>, used to determine whether a condition is true |
| <c:otherwise> | A child tag of <c:choose>, placed after the <c:when> tag, executed when the <c:when> tag evaluates to false |
| <c:import> | Retrieves an absolute or relative URL, then exposes its content to the page |
| <c:forEach> | Basic iteration tag, accepts multiple collection types |
| <c:forTokens> | Separates content according to the specified delimiter and iteratively outputs it |
| <c:param> | Used to pass parameters to included or redirected pages |
| <c:redirect> | Redirects to a new URL. |
| <c:url> | Creates a URL with 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 numbers using the specified format or precision |
| <fmt:parseNumber> | Parses a string representing a number, currency, or percentage |
| <fmt:formatDate> | Formats dates and times using the 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 file information |
| <fmt:requestEncoding> | Sets the request character encoding |
SQL Tags
The JSTL SQL tag library provides tags for interacting with relational databases (Oracle, MySQL, SQL Server, etc.). 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 the data source |
| <sql:query> | Runs SQL query statements |
| <sql:update> | Runs SQL update statements |
| <sql:param> | Sets the parameters in an SQL statement to specified values |
| <sql:dateParam> | Sets the date parameter in an SQL statement to the specified java.util.Date object value |
| <sql:transaction> | Provides nested database behavior 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 XML tags, you must copy the XML and XPath related packages to your <Tomcat installation directory>\lib:
- XercesImpl.jar
Download address:http://www.apache.org/dist/xerces/j/
- xalan.jar
Download address:http://xml.apache.org/xalan-j/index.html
| Tag | Description |
|---|---|
| <x:out> | Similar to <%= ... >, but only used for XPath expressions |
| <x:parse> | Parses XML data |
| <x:set> | Sets the XPath expression |
| <x:if> | Evaluates the XPath expression; if true, executes the content within the body, otherwise skips the body |
| <x:forEach> | Iterates over nodes in an XML document |
| <x:choose> | The parent tag of <x:when> and <x:otherwise> |
| <x:when> | A child tag of <x:choose>, used for conditional judgment |
| <x:otherwise> | A child tag of <x:choose>, executed when <x:when> evaluates to false |
| <x:transform> | Applies XSL transformation to an XML document |
| <x:param> | Used together with <x:transform> to set the XSL stylesheet |
JSTL Functions
JSTL contains 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 the specified substring |
| fn:containsIgnoreCase() | Tests whether the input string contains the specified substring, case-insensitive |
| fn:endsWith() | Tests whether the input string ends with the specified suffix |
| fn:escapeXml() | Escapes characters that can be used as XML markup |
| fn:indexOf() | Returns the position of the specified string 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 the specified position in the input string with the specified string and returns it |
| fn:split() | Splits the string using the specified delimiter to form an array of substrings and returns it |
| fn:startsWith() | Tests whether the input string starts with the specified prefix |
| fn:substring() | Returns a subset of the string |
| fn:substringAfter() | Returns the subset of the string after the specified substring |
| fn:substringBefore() | Returns the subset of the string before the specified substring |
| fn:toLowerCase() | Converts the characters in the string to lowercase |
| fn:toUpperCase() | Converts the characters in the string to uppercase |
| fn:trim() | Removes leading and trailing whitespace |