- Basic JSP Tutorial
- JSP - Home
- JSP - Overview
- JSP - Environment Setup
- JSP - Architecture
- JSP - Lifecycle
- JSP - Syntax
- JSP - Directives
- JSP - Actions
- JSP - Implicit Objects
- JSP - Client Request
- JSP - Server Response
- JSP - Http Status Codes
- JSP - Form Processing
- JSP - Writing Filters
- JSP - Cookies Handling
- JSP - Session Tracking
- JSP - File Uploading
- JSP - Handling Date
- JSP - Page Redirect
- JSP - Hits Counter
- JSP - Auto Refresh
- JSP - Sending Email
- Advanced JSP Tutorials
- JSP - Standard Tag Library
- JSP - Database Access
- JSP - XML Data
- JSP - Java Beans
- JSP - Custom Tags
- JSP - Expression Language
- JSP - Exception Handling
- JSP - Debugging
- JSP - Security
- JSP - Internationalization
- JSP Useful Resources
- JSP - Questions and Answers
- JSP - Quick Guide
- JSP - Useful Resources
- JSP - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 functionality common to many JSP applications.
JSTL has support for common, structural tasks such as iteration and conditionals, tags for manipulating XML documents, internationalization 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 library. If you are using the Apache Tomcat container, then follow these two steps −
Step 1 − Download the binary distribution from Apache Standard Taglib and unpack the compressed file.
Step 2 − To use the Standard Taglib from its Jakarta Taglibs distribution, simply copy the JAR files in the distribution's 'lib' directory to your application's webapps\ROOT\WEB-INF\lib directory.
To use any of the libraries, you must include a <taglib> directive at the top of each JSP that uses the library.
Classification of The JSTL Tags
The JSTL tags can be classified, according to their functions, into the following JSTL tag library groups that can be used when creating a JSP page −
The core group of tags are the most commonly used JSTL tags. Following is the syntax to include the JSTL Core library in your JSP −
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
Following table lists out the core JSTL Tags −
|S.No.||Tag & Description|
Like <%= ... >, but for expressions.
Sets the result of an expression evaluation in a 'scope'
Removes a scoped variable (from a particular scope, if specified).
Catches any Throwable that occurs in its body and optionally exposes it.
Simple conditional tag which evalutes its body if the supplied condition is true.
Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>.
Subtag of <choose> that includes its body if its condition evalutes to 'true'.
Subtag of <choose> that follows the <when> tags and runs only if all of the prior conditions evaluated to 'false'.
Retrieves an absolute or relative URL and exposes its contents to either the page, a String in 'var', or a Reader in 'varReader'.
The basic iteration tag, accepting many different collection types and supporting subsetting and other functionality .
Iterates over tokens, separated by the supplied delimeters.
Adds a parameter to a containing 'import' tag's URL.
Redirects to a new URL.
Creates a URL with optional query parameters
The JSTL formatting tags are used to format and display text, the date, the time, and numbers for internationalized Websites. Following is the syntax to include Formatting library in your JSP −
<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
Following table lists out the Formatting JSTL Tags −
|S.No.||Tag & Description|
To render numerical value with specific precision or format.
Parses the string representation of a number, currency, or percentage.
Formats a date and/or time using the supplied styles and pattern.
Parses the string representation of a date and/or time
Loads a resource bundle to be used by its tag body.
Stores the given locale in the locale configuration variable.
Loads a resource bundle and stores it in the named scoped variable or the bundle configuration variable.
Specifies the time zone for any time formatting or parsing actions nested in its body.
Stores the given time zone in the time zone configuration variable
Displays an internationalized message.
Sets the request character encoding
The JSTL SQL tag library provides tags for interacting with relational databases (RDBMSs) such as Oracle, mySQL, or Microsoft SQL Server.
Following is the syntax to include JSTL SQL library in your JSP −
<%@ taglib prefix = "sql" uri = "http://java.sun.com/jsp/jstl/sql" %>
Following table lists out the SQL JSTL Tags −
|S.No.||Tag & Description|
Creates a simple DataSource suitable only for prototyping
Executes the SQL query defined in its body or through the sql attribute.
Executes the SQL update defined in its body or through the sql attribute.
Sets a parameter in an SQL statement to the specified value.
Sets a parameter in an SQL statement to the specified java.util.Date value.
Provides nested database action elements with a shared Connection, set up to execute all statements as one transaction.
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 library in your JSP.
The JSTL XML tag library 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.
<%@ taglib 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 libraries into your <Tomcat Installation Directory>\lib −
XercesImpl.jar − Download it from https://www.apache.org/dist/xerces/j/
xalan.jar − Download it from https://xml.apache.org/xalan-j/index.html
Following is the list of XML JSTL Tags −
|S.No.||Tag & Description|
Like <%= ... >, but for XPath expressions.
Used to parse the XML data specified either via an attribute or in the tag body.
Sets a variable to the value of an XPath expression.
Evaluates a test XPath expression and if it is true, it processes its body. If the test condition is false, the body is ignored.
To loop over nodes in an XML document.
Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise> tags.
Subtag of <choose> that includes its body if its expression evalutes to 'true'.
Subtag of <choose> that follows the <when> tags and runs only if all of the prior conditions evaluates to 'false'.
Applies an XSL transformation on a XML document
Used along with the transform tag to set a parameter in the XSLT stylesheet
JSTL includes a number of standard functions, most of which are common string manipulation functions. Following is the syntax to include JSTL Functions library in your JSP −
<%@ taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" %>
Following table lists out the various JSTL Functions −
|S.No.||Function & Description|
Tests if an input string contains the specified substring.
Tests if an input string contains the specified substring in a case insensitive way.
Tests if an input string ends with the specified suffix.
Escapes characters that can be interpreted as XML markup.
Returns the index withing a string of the first occurrence of a specified substring.
Joins all elements of an array into a string.
Returns the number of items in a collection, or the number of characters in a string.
Returns a string resulting from replacing in an input string all occurrences with a given string.
Splits a string into an array of substrings.
Tests if an input string starts with the specified prefix.
Returns a subset of a string.
Returns a subset of a string following a specific substring.
Returns a subset of a string before a specific substring.
Converts all of the characters of a string to lower case.
Converts all of the characters of a string to upper case.
Removes white spaces from both ends of a string.