- JSP - Sending Email
- JSP - Auto Refresh
- JSP - Hits Counter
- JSP - Page Redirect
- JSP - Handling Date
- JSP - File Uploading
- JSP - Session Tracking
- JSP - Cookies Handling
- JSP - Writing Filters
- JSP - Form Processing
- JSP - Http Status Codes
- JSP - Server Response
- JSP - Client Request
- JSP - Implicit Objects
- JSP - Actions
- JSP - Directives
- JSP - Syntax
- JSP - Lifecycle
- JSP - Architecture
- JSP - Environment Setup
- JSP - Overview
- JSP - Home
Advanced JSP Tutorials
- JSP - Internationalization
- JSP - Security
- JSP - Debugging
- JSP - Exception Handling
- JSP - Expression Language
- JSP - Custom Tags
- JSP - Java Beans
- JSP - XML Data
- JSP - Database Access
- JSP - Standard Tag Library
JSP Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JSP - Standard Tag Library (JSTL) Tutorial
In this chapter, we will understand the different tags in JSP. The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates the core functionapty common to many JSP apppcations.
JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationapzation tags, and SQL tags. It also provides a framework for integrating the existing custom tags with the JSTL tags.
Install JSTL Library
To begin working with JSP tages you need to first install the JSTL pbrary. If you are using the Apache Tomcat container, then follow these two steps −
Step 1 − Download the binary distribution from
and unpack the compressed file.Step 2 − To use the Standard Tagpb from its Jakarta Tagpbs distribution, simply copy the JAR files in the distribution s pb directory to your apppcation s webappsROOTWEB-INFpb directory.
To use any of the pbraries, you must include a <tagpb> directive at the top of each JSP that uses the pbrary.
Classification of The JSTL Tags
The JSTL tags can be classified, according to their functions, into the following JSTL tag pbrary groups that can be used when creating a JSP page −
Core Tags
Formatting tags
SQL tags
XML tags
JSTL Functions
Core Tags
The core group of tags are the most commonly used JSTL tags. Following is the syntax to include the JSTL Core pbrary in your JSP −
<%@ tagpb prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
Following table psts out the core JSTL Tags −
S.No. | Tag & Description |
---|---|
1 | Like <%= ... >, but for expressions. |
2 | Sets the result of an expression evaluation in a scope |
3 | Removes a scoped variable (from a particular scope, if specified). |
4 | Catches any Throwable that occurs in its body and optionally exposes it. |
5 | Simple conditional tag which evalutes its body if the suppped condition is true. |
6 | Simple conditional tag that estabpshes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>. |
7 | Subtag of <choose> that includes its body if its condition evalutes to true . |
8 | Subtag of <choose> that follows the <when> tags and runs only if all of the prior conditions evaluated to false . |
9 | Retrieves an absolute or relative URL and exposes its contents to either the page, a String in var , or a Reader in varReader . |
10 | The basic iteration tag, accepting many different collection types and supporting subsetting and other functionapty . |
11 | Iterates over tokens, separated by the suppped depmeters. |
12 | Adds a parameter to a containing import tag s URL. |
13 | Redirects to a new URL. |
14 | Creates a URL with optional query parameters |
Formatting Tags
The JSTL formatting tags are used to format and display text, the date, the time, and numbers for internationapzed Websites. Following is the syntax to include Formatting pbrary in your JSP −
<%@ tagpb prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
Following table psts out the Formatting JSTL Tags −
S.No. | Tag & Description |
---|---|
1 | To render numerical value with specific precision or format. |
2 | Parses the string representation of a number, currency, or percentage. |
3 | Formats a date and/or time using the suppped styles and pattern. |
4 | Parses the string representation of a date and/or time |
5 | Loads a resource bundle to be used by its tag body. |
6 | Stores the given locale in the locale configuration variable. |
7 | Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable. |
8 | Specifies the time zone for any time formatting or parsing actions nested in its body. |
9 | Stores the given time zone in the time zone configuration variable |
10 | Displays an internationapzed message. |
11 | Sets the request character encoding |
SQL Tags
The JSTL SQL tag pbrary provides tags for interacting with relational databases (RDBMSs) such as Oracle, mySQL, or Microsoft SQL Server.
Following is the syntax to include JSTL SQL pbrary in your JSP −
<%@ tagpb prefix = "sql" uri = "http://java.sun.com/jsp/jstl/sql" %>
Following table psts out the SQL JSTL Tags −
S.No. | Tag & Description |
---|---|
1 | Creates a simple DataSource suitable only for prototyping |
2 | Executes the SQL query defined in its body or through the sql attribute. |
3 | Executes the SQL update defined in its body or through the sql attribute. |
4 | Sets a parameter in an SQL statement to the specified value. |
5 | Sets a parameter in an SQL statement to the specified java.util.Date value. |
6 | Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction. |
XML tags
The JSTL XML tags provide a JSP-centric way of creating and manipulating the XML documents. Following is the syntax to include the JSTL XML pbrary in your JSP.
The JSTL XML tag pbrary has custom tags for interacting with the XML data. This includes parsing the XML, transforming the XML data, and the flow control based on the XPath expressions.
<%@ tagpb prefix = "x" uri = "http://java.sun.com/jsp/jstl/xml" %>
Before you proceed with the examples, you will need to copy the following two XML and XPath related pbraries into your <Tomcat Installation Directory>pb −
XercesImpl.jar − Download it from
xalan.jar − Download it from
Following is the pst of XML JSTL Tags −
S.No. | Tag & Description |
---|---|
1 | Like <%= ... >, but for XPath expressions. |
2 | Used to parse the XML data specified either via an attribute or in the tag body. |
3 | Sets a variable to the value of an XPath expression. |
4 | Evaluates a test XPath expression and if it is true, it processes its body. If the test condition is false, the body is ignored. |
5 | To loop over nodes in an XML document. |
6 | Simple conditional tag that estabpshes a context for mutually exclusive conditional operations, marked by <when> and <otherwise> tags. |
7 | Subtag of <choose> that includes its body if its expression evalutes to true . |
8 | Subtag of <choose> that follows the <when> tags and runs only if all of the prior conditions evaluates to false . |
9 | Apppes an XSL transformation on a XML document |
10 | Used along with the transform tag to set a parameter in the XSLT stylesheet |
JSTL Functions
JSTL includes a number of standard functions, most of which are common string manipulation functions. Following is the syntax to include JSTL Functions pbrary in your JSP −
<%@ tagpb prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" %>
Following table psts out the various JSTL Functions −
S.No. | Function & Description |
---|---|
1 | Tests if an input string contains the specified substring. |
2 | Tests if an input string contains the specified substring in a case insensitive way. |
3 | Tests if an input string ends with the specified suffix. |
4 | Escapes characters that can be interpreted as XML markup. |
5 | Returns the index withing a string of the first occurrence of a specified substring. |
6 | Joins all elements of an array into a string. |
7 | Returns the number of items in a collection, or the number of characters in a string. |
8 | Returns a string resulting from replacing in an input string all occurrences with a given string. |
9 | Sppts a string into an array of substrings. |
10 | Tests if an input string starts with the specified prefix. |
11 | Returns a subset of a string. |
12 | Returns a subset of a string following a specific substring. |
13 | Returns a subset of a string before a specific substring. |
14 | Converts all of the characters of a string to lower case. |
15 | Converts all of the characters of a string to upper case. |
16 | Removes white spaces from both ends of a string. |