Easy Tutorial
❮ Jstl Function Containsignorecase Jsp Database Access ❯

JSP Standard Tag Library (JSTL)

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

JSTL supports common, structural tasks such as iteration, conditional execution, XML document manipulation, internationalization tags, and SQL tags. In addition to these, it provides a framework for using custom tags integrated with JSTL.

Based on the functionalities provided by JSTL tags, they can be categorized into five types:


JSTL Library Installation

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

Download the binary package (jakarta-taglibs-standard-current.zip) from the Apache Standard Tag Library.

Download the jakarta-taglibs-standard-1.1.2.zip package and extract it. Copy the two jar files, standard.jar and jstl.jar, from jakarta-taglibs-standard-1.1.2/lib/ to /WEB-INF/lib/.

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

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

<?xml version="1.0" encoding="UTF-8"?>
&lt;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>
```xml
<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 in the header of each 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, similar to <%= ... >
<c:set> Used to save data
<c:remove> Used to delete data
<c:catch> Used to handle exceptions that generate error conditions and store error messages
<c:if> Functions like the if statement in regular programming
<c:choose> Serves as the parent tag for <c:when> and <c:otherwise>
<c:when> A child tag of <c:choose>, used to check if a condition is true
<c:otherwise> A child tag of <c:choose>, follows <c:when> and executes when <c:when> is false
<c:import> Retrieves content from an absolute or relative URL and exposes it to the page
<c:forEach> Basic iteration tag, accepts multiple collection types
<c:forTokens> Iterates over tokens separated by a specified delimiter
<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, 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" %>
Tag Description
<fmt:formatNumber> Formats a number with specified formatting or precision
<fmt:parseNumber> Parses a string representing a number, currency, or percentage
<fmt:formatDate> Formats date and time with 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> Bind Resource
<fmt:timeZone> Specify Time Zone
<fmt:setTimeZone> Specify Time Zone
<fmt:message> Display Resource Configuration Information
<fmt:requestEncoding> Set Character Encoding for Request

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> Specify Data Source
<sql:query> Execute SQL Query Statement
<sql:update> Execute SQL Update Statement
<sql:param> Set Parameter in SQL Statement to Specified Value
<sql:dateParam> Set Date Parameter in SQL Statement to Specified java.util.Date Object Value
<sql:transaction> Provide 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 XML tags, you must copy the relevant XML and XPath packages to your <Tomcat installation directory>\lib:

Download link: http://www.apache.org/dist/xerces/j/

Download link: http://xml.apache.org/xalan-j/index.html

Tag Description
<x:out> Similar to <%= ... %>, but used for XPath expressions
<x:parse> Parse XML Data
<x:set> Set XPath Expression
<x:if> Evaluate XPath Expression, if true, execute the content within, otherwise skip
<x:forEach> Iterate Over Nodes in XML Document
<x:choose> Parent tag for <x:when> and <x:otherwise>
<x:when> Child tag of <x:choose>, used for conditional judgment
<x:otherwise> Child tag of <x:choose>, executed when <x:when> is false
<x:transform> Apply XSL Transformation to XML Document

This is a Chinese to English translation. Please provide the English translation for this text. Do not provide any explanations or text apart from the translation.


JSTL Functions

JSTL includes a series of standard functions, mostly 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" %>
Function Description
fn:contains() Tests if the input string contains the specified substring
fn:containsIgnoreCase() Tests if the input string contains the specified substring, case insensitive
fn:endsWith() Tests if the input string ends with the specified suffix
fn:escapeXml() Skips characters that can be used as XML tags
fn:indexOf() Returns the position of the specified substring in the input string
fn:join() Combines the elements of an array into a string and outputs it
fn:length() Returns the length of the string
fn:replace() Replaces the specified position in the input string with the specified string and returns it
fn:split() Splits the string with the specified delimiter and returns a substring array
fn:startsWith() Tests if the input string starts with the specified prefix
fn:substring() Returns a subset of the string
fn:substringAfter() Returns a subset of the string after the specified substring
fn:substringBefore() Returns a 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 characters
❮ Jstl Function Containsignorecase Jsp Database Access ❯