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.
- Core Tags
- Formatting Tags
- SQL Tags
- XML tags
- JSTL Functions
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.
- Official download address:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
- Download address on this site:jakarta-taglibs-standard-1.1.2.zip
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
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" %>
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
- XercesImpl.jar
Download address: http://www.apache.org/dist/xerces/j/
- xalan.jar
Download address: http://xml.apache.org/xalan-j/index.html
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 |