JSP Standard Tag Library (JSTL)


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

JSTL supports common, structured tasks, such as iteration, conditional judgment, XML document operations, international tags, and SQL tags. In addition to these, it also provides a framework to use custom tags integrated with JSTL.

According to the functions provided by JSTL tags, they can be divided into 5 categories.

  • Core tags

  • ##Formatting tags

  • SQL Tag

  • XML Tag

  • JSTL Function


JSTL library installation

Apache Tomcat installation JSTL library steps are as follows:

  • From the Apache standard tag The binary package downloaded from the library (jakarta-taglibs-standard-current.zip). Download address: http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/

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

Next we add the following configuration in the web.xml file:

……
	<jsp-config>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
	<taglib-location>/WEB-INF/fmt.tld</taglib-location>
	</taglib>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/fmt-rt</taglib-uri>
	<taglib-location>/WEB-INF/fmt-rt.tld</taglib-location>
	</taglib>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
	<taglib-location>/WEB-INF/c.tld</taglib-location>
	</taglib>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/core-rt</taglib-uri>
	<taglib-location>/WEB-INF/c-rt.tld</taglib-location>
	</taglib>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/sql</taglib-uri>
	<taglib-location>/WEB-INF/sql.tld</taglib-location>
	</taglib>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/sql-rt</taglib-uri>
	<taglib-location>/WEB-INF/sql-rt.tld</taglib-location>
	</taglib>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/x</taglib-uri>
	<taglib-location>/WEB-INF/x.tld</taglib-location>
	</taglib>
	<taglib>
	<taglib-uri>http://java.sun.com/jstl/x-rt</taglib-uri>
	<taglib-location>/WEB-INF/x-rt.tld</taglib-location>
	</taglib>
	</jsp-config>
……

To use any library, you must include < in the head of each JSP file taglib>Tag.


Core tag

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

<%@ taglib prefix="c" 
           uri="http://java.sun.com/jsp/jstl/core" %>
##<c:set> Used to save data<c:remove> Used to delete data<c:catch> Used to handle exceptions that cause errors and store error information<c:if> It’s the same as the if we use in general programs##<c:choose><c:when><c:otherwise><c :import><c:forEach><c:forTokens><c:param><c:redirect><c:url>
Label Description
<c:out> Used to display data in JSP, like <%= ... >
It is only used as the parent tag of <c:when> and <c:otherwise>
The sub-tag of <c:choose> is used to determine whether the condition is true
The sub-tag of <c:choose> is connected after the <c:when> tag and is executed when the <c:when> tag is judged to be false
Retrieve an absolute or relative URL, then expose its contents to the page
Basic iteration tag, accepting multiple collection types
Separate content according to the specified delimiter and iterate the output
Used to pass parameters to included or redirected pages
Redirect to a new URL.
Use optional query parameters to create a URL
Formatting tag

JSTL formatting tag is used to format and output text, date, time, and numbers. The syntax for referencing the formatting tag library is as follows:

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

Label<fmt:formatNumber><fmt:parseNumber><fmt:formatDate><fmt:parseDate><fmt:bundle><fmt:setLocale><fmt:setBundle><fmt:timeZone><fmt:setTimeZone><fmt:message><fmt:requestEncoding>

SQL tag

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" %>
Description
Format a number using the specified format or precision
Parse a string representing a number, currency or percentage
Format dates and times using the specified style or pattern
Parse a string representing a date or time
Binding resources
Specify region
Binding resources
Specify time zone
Specify time zone
Display resource configuration file information
Set the character encoding of request
Label Description
<sql:setDataSource> Specify data source
<sql:query> Run SQL query statement
<sql:update> Run SQL update statement
<sql:param> Set the parameters in the SQL statement to the specified value
<sql:dateParam> Set the date parameter in the SQL statement to the specified java.util.Date object value
<sql:transaction> Provide nested database behavior elements in a shared database connection to run all statements as a transaction

XML tag

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 tag, 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/

Label Description
<x:out> Similar to <%= ... >, but only used in XPath expressions
<x:parse> Parse XML data
<x:set> Set XPath expression
<x:if> Determine the XPath expression. If it is true, execute the content in the ontology, otherwise skip the ontology
<x:forEach> Iterate over nodes in an XML document
<x:choose> The parent tag of <x:when> and <x:otherwise>
<x:when> The sub-tag of <x:choose> is used for conditional judgment
<x:otherwise> The sub-tag of <x:choose> is executed when <x:when> is judged to be false
<x:transform> Apply XSL transformations to XML documents
<x:param> Used together with <x:transform> to set XSL style sheets

JSTL functions

JSTL contains a series of standard functions, most of which are general 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" %>
##fn:substringBefore() Returns the subset of the string before the specified substringfn:toLowerCase() Convert the characters in the string to lowercasefn:toUpperCase() Convert the characters in the string to uppercasefn:trim() Remove leading whitespace
Function Description
fn:contains() Test 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() Skip characters that can be used as XML tags
fn:indexOf() Returns the position where the specified string appears in the input string
fn:join() Combine the elements in the array into a string and output
fn:length() Return the string length
#fn:replace() Replaces the specified position in the input string with the specified string and returns
fn:split() Separate the string with the specified delimiter and then form a substring array and return
fn:startsWith() Tests whether the input string starts with the specified prefix
fn:substring() Return a subset of the string
#fn:substringAfter() Returns the subset of the string after the specified substring
#