The JavaServer Pages Standard Tag Library
The JavaServer Pages Standard Tag Library
The JavaServer Pages Standard Tag Library
Core Tags
Formatting tags
SQL tags
XML tags
JSTL Functions
Download the binary distribution from Apache Standard Taglib and unpack the
compressed file.
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.
Core Tags:
The core group of tags are the most frequently used JSTL tags. Following is
the syntax to include JSTL Core library in your JSP:
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
Description
<c:out >
<c:set >
<c:remove >
<c:catch>
<c:if>
<c:choose>
<c:when>
<c:otherwise >
<c:import>
<c:forEach >
<c:forTokens>
<c:param>
<c:redirect >
<c:url>
Formatting tags:
The JSTL formatting tags are used to format and display text, the date, the
time, and numbers for internationalized Web sites. Following is the syntax
to include Formatting library in your JSP:
<%@ taglib prefix="fmt"
uri="http://java.sun.com/jsp/jstl/fmt" %>
Description
<fmt:formatNumber>
<fmt:parseNumber>
<fmt:formatDate>
<fmt:parseDate>
time
<fmt:bundle>
<fmt:setLocale>
<fmt:setBundle>
<fmt:timeZone>
<fmt:setTimeZone>
<fmt:message>
<fmt:requestEncoding>
SQL tags:
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" %>
Description
<sql:setDataSource>
<sql:query>
<sql:update>
<sql:param>
<sql:dateParam>
<sql:transaction >
XML tags:
The JSTL XML tags provide a JSP-centric way of creating and manipulating
XML documents. Following is the syntax to include JSTL XML library in your
JSP.
The JSTL XML tag library has custom tags for interacting with XML data.
This includes parsing XML, transforming XML data, and flow control based
on XPath expressions.
<%@ taglib prefix="x"
uri="http://java.sun.com/jsp/jstl/xml" %>
Before you proceed with the examples, you would need to copy following
two XML and XPath related libraries into your <Tomcat Installation
Directory>\lib:
Description
<x:out>
<x:parse>
<x:set >
<x:if >
<x:forEach>
<x:choose>
<x:when >
<x:otherwise >
<x:transform >
<x:param >
JSTL Functions:
Description
fn:contains()
fn:containsIgnoreCase()
fn:endsWith()
fn:escapeXml()
fn:indexOf()
fn:join()
fn:length()
fn:replace()
fn:split()
fn:startsWith()
fn:substring()
fn:substringAfter()
fn:substringBefore()
fn:toLowerCase()
fn:toUpperCase()
fn:trim()
Attribute:
The <c:out> tag has following attributes:
Attribute
Description
Required
Default
value
Information to output
Yes
None
default
No
body
escapeXml
No
true
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>
<c:out> Tag Example</title>
</head>
<body>
<c:out value="${'<tag> , &'}"/>
</body>
</html>
The <c:set> tag is JSTL-friendly version of the setProperty action. The tag
is helpful because it evaluates an expression and uses the results to set a
value of a JavaBean or a java.util.Map object.
Attribute:
The <c:set> tag has following attributes:
Attribute
Description
Required
Default
value
Information to save
No
body
target
No
None
property
Property to modify
No
None
var
No
None
scope
No
Page
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:set> Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:out value="${salary}"/>
</body>
</html>
Attribute:
The <c:remove> tag has following attributes:
Attribute
Description
Required
Default
var
Yes
None
scope
No
All scopes
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:remove> Tag Example</title>
</head>
<body>
Attribute:
The <c:catch> tag has following attributes:
Attribute
Description
Required
Default
var
No
None
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
</body>
</html>
Attribute:
The <c:if> tag has following attributes:
Attribute
Description
Required
Default
test
Condition to evaluate
Yes
None
var
No
None
scope
No
page
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:if> Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<c:if test="${salary > 2000}">
<p>My salary is: <c:out value="${salary}"/><p>
</c:if>
</body>
</html>
Attribute:
Description
Required
Default
test
Condition to evaluate
Yes
None
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:choose> Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<p>Your salary is : <c:out value="${salary}"/></p>
<c:choose>
<c:when test="${salary <= 0}">
Salary is very low to survive.
</c:when>
<c:when test="${salary > 1000}">
Salary is very good.
</c:when>
<c:otherwise>
No comment sir...
</c:otherwise>
</c:choose>
</body>
</html>
Attribute:
Description
Required
Default
test
Condition to evaluate
Yes
None
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:choose> Tag Example</title>
</head>
<body>
<c:set var="salary" scope="session" value="${2000*2}"/>
<p>Your salary is : <c:out value="${salary}"/></p>
<c:choose>
<c:when test="${salary <= 0}">
Salary is very low to survive.
</c:when>
<c:when test="${salary > 1000}">
Salary is very good.
</c:when>
<c:otherwise>
No comment sir...
</c:otherwise>
</c:choose>
</body>
</html>
Attribute:
The <c:import> tag has following attributes:
Attribute
Description
Require
d
Default
url
Yes
None
context
No
Current
application
charEncoding
No
ISO-8859-1
var
No
Print to page
scope
No
Page
varReader
No
None
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:import> Tag Example</title>
</head>
<body>
<c:import var="data" url="http://www.tutorialspoint.com"/>
<c:out value="${data}"/>
</body>
</html>
Above
example
would
fetch
complete
content
from
tutorialspoint.com/index.htm and would store in variable data which will be
printed eventually. Try it yourself.
Attribute:
The <c:forEach> tag has following attributes:
Attribute
Description
Required
Default
items
No
None
begin
No
end
No
Last
element
step
No
var
No
None
varStatus
No
None
Description
Required
Default
delims
Yes
None
<head>
<title><c:forTokens> Tag Example</title>
</head>
<body>
<c:forTokens items="Zara,nuha,roshy" delims="," var="name">
<c:out value="${name}"/><p>
</c:forTokens>
</body>
</html>
Attribute:
The <c:param> tag has following attributes:
Attribute
Description
Required
Default
name
Yes
None
value
No
Body
URL
Example:
If you need to pass parameters to a <c:import> tag, use the <c:url> tag to
create the URL first as shown below:
<c:url value="/index.jsp" var="myURL">
<c:param name="trackingId" value="1234"/>
<c:param name="reportType" value="summary"/>
</c:url>
<c:import url="${myURL}"/>
Attribute:
The <c:redirect> tag has following attributes:
Attribute
Description
Required
Default
url
Yes
None
context
No
Current application
Example:
If you need to pass parameters to a <c:import> tag, use the <c:url> tag to
create the URL first as shown below:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:redirect> Tag Example</title>
</head>
<body>
<c:redirect url="http://www.photofuntoos.com"/>
</body>
</html>
Attribute:
The <c:url> tag has following attributes:
Attribute
Description
Required
Default
value
Base URL
Yes
None
context
No
Current application
var
No
Print to page
scope
No
Page
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:url> Tag Example</title>
</head>
<body>
<a href="<c:url value="/jsp/index.htm"/>">TEST</a>
</body>
</html>
Attribute:
Description
Required
Default
value
Yes
None
type
NUMBER, CURRENCY, or
PERCENT
No
Number
pattern
No
None
currencyCode
No
From the
default locale
currencySymbol
No
From the
default locale
groupingUsed
No
true
maxIntegerDigits
No
None
minIntegerDigits
No
None
maxFractionDigits
No
None
minFractionDigits
No
None
var
No
Print to page
formatted number
scope
No
page
If the type attribute is percent or number, then you can use several numberformatting attributes. The maxIntegerDigits and minIntegerDigits attributes
allow you to specify the size of the nonfractional portion of the number. If the
actual number exceeds maxIntegerDigits, then the number is truncated.
Attributes are also provided to allow you to determine how many decimal places
should be used. The minFractionalDigits and maxFractionalDigits attributes allow
you to specify the number of decimal places. If the number exceeds the
maximum number of fractional digits, the number will be rounded.
You may select to use the pattern attribute. This attribute lets you include
special characters that specify how you would like your number encoded.
Following table shows these codes.
Symbol
Description
Represents a digit.
Separates formats.
'
Example:
<html>
<head>
<title>JSTL fmt:formatNumber Tag</title>
</head>
<body>
<h3>Number Format:</h3>
<c:set var="balance" value="120000.2309" />
<p>Formatted Number (1): <fmt:formatNumber value="${balance}"
type="currency"/></p>
<p>Formatted Number (2): <fmt:formatNumber type="number"
maxIntegerDigits="3" value="${balance}" /></p>
<p>Formatted Number (3): <fmt:formatNumber type="number"
maxFractionDigits="3" value="${balance}" /></p>
<p>Formatted Number (4): <fmt:formatNumber type="number"
groupingUsed="false" value="${balance}" /></p>
<p>Formatted Number (5): <fmt:formatNumber type="percent"
maxIntegerDigits="3" value="${balance}" /></p>
<p>Formatted Number (6): <fmt:formatNumber type="percent"
minFractionDigits="10" value="${balance}" /></p>
<p>Formatted Number (7): <fmt:formatNumber type="percent"
maxIntegerDigits="3" value="${balance}" /></p>
<p>Formatted Number (8): <fmt:formatNumber type="number"
pattern="###.###E0" value="${balance}" /></p>
<p>Currency in USA :
<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="${balance}" type="currency"/></p>
</body>
</html>
Number Format:
Formatted Number (1): 120,000.23
Formatted Number (2): 000.231
Formatted Number (3): 120,000.231
Formatted Number (4): 120000.231
Formatted Number (5): 023%
Formatted Number (6): 12,000,023.0900000000%
Formatted Number (7): 023%
Formatted Number (8): 120E3
Currency in USA : $120,000.23
Attribute:
The <fmt:parseNumber> tag has following attributes:
Attribute
Description
Required
Default
value
No
Body
type
No
number
parseLocale
No
Default
locale
integerOnly
No
false
pattern
No
None
timeZone
No
Default time
zone
var
No
Print to page
scope
No
page
A pattern attribute is provided that works just like the pattern attribute for
the <fmt:formatNumber> tag. However, in the case of parsing, the pattern
attribute tells the parser what format to expect.
Example:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>JSTL fmt:parseNumber Tag</title>
</head>
<body>
<h3>Number Parsing:</h3>
<c:set var="balance" value="1250003.350" />
</body>
</html>
Attribute:
The <fmt:formatDate> tag has following attributes:
Attribute
Description
Required
Default
value
Yes
None
type
No
date
dateStyle
No
default
timeStyle
No
default
pattern
No
None
timeZone
No
Default time
zone
var
No
Print to page
scope
No
page
The pattern attribute to specify even more precise handling of the date:
Code
Purpose
Sample
AD
The year
2002
The month
April & 04
20
12
The minute
45
The second
52
The millisecond
970
Tuesday
180
27
PM
24
'
''
Example:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>JSTL fmt:dateNumber Tag</title>
</head>
<body>
<h3>Number Format:</h3>
<c:set var="now" value="<%=new java.util.Date()%>" />
dateStyle="short" timeStyle="short"
value="${now}" /></p>
<p>Formatted Date (5): <fmt:formatDate type="both"
dateStyle="medium" timeStyle="medium"
value="${now}" /></p>
<p>Formatted Date (6): <fmt:formatDate type="both"
dateStyle="long" timeStyle="long"
value="${now}" /></p>
<p>Formatted Date (7): <fmt:formatDate pattern="yyyy-MM-dd"
value="${now}" /></p>
</body>
</html>
Attribute:
The <fmt:parseDate> tag has following attributes:
Attribute
Description
Require
d
Default
value
No
Body
type
No
date
dateStyle
No
Default
timeStyle
No
Default
parseLocale
No
Default locale
pattern
No
None
timeZone
No
Default time
zone
var
No
Print to page
scope
No
page
A pattern attribute is provided that works just like the pattern attribute for
the <fmt:formatDate> tag. However, in the case of parsing, the pattern
attribute tells the parser what format to expect.
Example:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>JSTL fmt:parseDate Tag</title>
</head>
<body>
<h3>Date Parsing:</h3>
<c:set var="now" value="20-10-2010" />
</body>
</html>
Attribute:
The <sql:setDataSource> tag has following attributes:
Attribute
Description
Required
Default
driver
No
None
url
No
None
user
Database username
No
None
password
Database password
No
None
password
Database password
No
None
dataSource
No
None
var
No
Set
default
scope
No
Page
Example:
Consider the following information about your MySQL database setup:
All the above parameters would vary based on your MySQL or any other
database setup. Keeping above parameters in mind, following is a simple
example to use setDataSource tag:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
</body>
</html>
Attribute:
The <sql:query> tag has following attributes:
Attribute
Description
Require
d
Default
sql
No
Body
dataSource
No
Default
database
maxRows
No
Unlimited
startRow
No
var
No
Set default
scope
No
Page
Example:
To start with basic concept, let us create a simple table Employees table in
TEST database and create few records in that table as follows:
Step 1:
Open a Command Prompt and change to the installation directory as
follows:
C:\>
C:\>cd Program Files\MySQL\bin
C:\Program Files\MySQL\bin>
Step 2:
Login to database as follows
C:\Program Files\MySQL\bin>mysql -u root -p
Enter password: ********
mysql>
Step 3:
Create the table Employee in TEST database as follows:
mysql> use TEST;
mysql> create table Employees
(
id int not null,
age int not null,
first varchar (255),
last varchar (255)
);
Query OK, 0 rows affected (0.08 sec)
mysql>
mysql>
Now let us write a JSP which will make use of <sql:query> to execute a
SQL SELECT statement as follows:
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<html>
<head>
<title>JSTL sql:query Tag</title>
</head>
<body>
<td><c:out value="${row.last}"/></td>
<td><c:out value="${row.age}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
Now try to access above JSP, which should display the following result:
Emp ID
First Name
Last Name
Age
100
Zara
Ali
18
101
Mahnaz
Fatma
25
102
Zaid
Khan
30
103
Sumit
Mittal
28
Attribute:
The <sql:update> tag has following attributes:
Attribute
Description
Required
Default
sql
No
Body
dataSource
No
Default
the default)
database
var
No
None
scope
No
Page
Example:
To start with basic concept, let us create a simple table Employees table in
TEST database and create few records in that table as follows:
Step 1:
Open a Command Prompt and change to the installation directory as
follows:
C:\>
C:\>cd Program Files\MySQL\bin
C:\Program Files\MySQL\bin>
Step 2:
Login to database as follows
C:\Program Files\MySQL\bin>mysql -u root -p
Enter password: ********
mysql>
Step 3:
Create the table Employee in TEST database as follows:
mysql>
Now let us write a JSP which will make use of <sql:update> to execute a
SQL INSERT statement to create one record in the table as follows:
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<html>
<head>
<title>JSTL sql:update Tag</title>
</head>
<body>
<td><c:out value="${row.age}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
Now try to access above JSP, which should display the following result:
Emp ID
First Name
Last Name
Age
100
Zara
Ali
18
101
Mahnaz
Fatma
25
102
Zaid
Khan
30
103
Sumit
Mittal
28
104
Nula
Ali
Similar way, you can try SQL UPDATE and DELETE statements on the same
table.
Attribute:
The <sql:param> tag has following attributes:
Attribute
Description
Required
Default
value
No
Body
Example:
To start with basic concept, let us create a simple table Employees table in
TEST database and create few records in that table as follows:
Step 1:
Open a Command Prompt and change to the installation directory as
follows:
C:\>
C:\>cd Program Files\MySQL\bin
C:\Program Files\MySQL\bin>
Step 2:
Login to database as follows
C:\Program Files\MySQL\bin>mysql -u root -p
Enter password: ********
mysql>
Step 3:
Create the table Employee in TEST database as follows:
mysql> use TEST;
mysql> create table Employees
(
id int not null,
age int not null,
first varchar (255),
mysql>
Now let us write a JSP which will make use of <sql:update> to execute a
SQL DELETE statement to delete one record with id = 103 from the table as
follows:
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<html>
<head>
</c:forEach>
</table>
</body>
</html>
Now try to access above JSP, which should display the following result:
Emp ID
First Name
Last Name
Age
100
Zara
Ali
18
101
Mahnaz
Fatma
25
102
Zaid
Khan
30
You can try <sql:param> with SQL UPDATE and SELECT statements as well
in the same way as I have used it with DELETE statement.
Attribute:
The <sql:dateParam> tag has following attributes:
Attribute
Description
Require
d
Default
value
No
Body
type
No
TIMESTAMP
Example:
To start with basic concept, let us create a simple table Students table in
TEST database and create few records in that table as follows:
Step 1:
Open a Command Prompt and change to the installation directory as
follows:
C:\>
C:\>cd Program Files\MySQL\bin
C:\Program Files\MySQL\bin>
Step 2:
Login to database as follows
C:\Program Files\MySQL\bin>mysql -u root -p
Enter password: ********
mysql>
Step 3:
Create the table Employee in TEST database as follows:
mysql> use TEST;
mysql> create table Students
(
id int not null,
first varchar (255),
mysql>
Now let us write a JSP which will make use of <sql:update> along with
<sql:param> and <sql:dataParam> to execute a SQL UPDATE statement to
update date of birth for Zara:
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<html>
<head>
<title>JSTL sql:dataParam Tag</title>
</head>
<body>
<%
Date DoB = new Date("2001/12/16");
int studentId = 100;
%>
<th>Last Name</th>
<th>DoB</th>
</tr>
<c:forEach var="row" items="${result.rows}">
<tr>
<td><c:out value="${row.id}"/></td>
<td><c:out value="${row.first}"/></td>
<td><c:out value="${row.last}"/></td>
<td><c:out value="${row.dob}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
Now try to access above JSP, which should display the following result after
updating dob from 2002/05/16 to 2001/12/16 for the record with ID=100:
Emp ID
First Name
Last Name
DoB
100
Zara
Ali
2001-12-16
101
Mahnaz
Fatma
1978-11-28
102
Zaid
Khan
1980-10-10
103
Sumit
Mittal
1971-05-08
Attribute:
The <sql:transaction> tag has following attributes:
Attribute
Description
Require
d
Default
dataSource
No
Default
database
isolation
No
Databases
default
Example:
To start with basic concept, let us create a simple table Students table in
TEST database and create few records in that table as follows:
Step 1:
Open a Command Prompt and change to the installation directory as
follows:
C:\>
C:\>cd Program Files\MySQL\bin
C:\Program Files\MySQL\bin>
Step 2:
Login to database as follows
Step 3:
Create the table Employee in TEST database as follows:
mysql> use TEST;
mysql> create table Students
(
id int not null,
first varchar (255),
last varchar (255),
dob date
);
Query OK, 0 rows affected (0.08 sec)
mysql>
mysql>
Now let us write a JSP which will make use of <sql:update> along with
<sql:transaction> to execute a SQL UPDATE statement. Here code inside
<sql:transaction> either would be exeucted completely or not at all:
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<%@ page import="java.util.Date,java.text.*" %>
<html>
<head>
<title>JSTL sql:transaction Tag</title>
</head>
<body>
<%
Date DoB = new Date("2001/12/16");
int studentId = 100;
%>
<sql:transaction dataSource="${snapshot}">
<sql:update var="count">
UPDATE Students SET last = 'Ali' WHERE Id = 102
</sql:update>
<sql:update var="count">
UPDATE Students SET last = 'Shah' WHERE Id = 103
</sql:update>
<sql:update var="count">
INSERT INTO Students
VALUES (104,'Nuha', 'Ali', '2010/05/26');
</sql:update>
</sql:transaction>
</body>
</html>
Now try to access above JSP, which should display the following result:
Emp ID
First Name
Last Name
DoB
100
Zara
Ali
2001-12-16
101
Mahnaz
Fatma
1978-11-28
102
Zaid
Ali
1980-10-10
103
Sumit
Shah
1971-05-08
104
Nuha
Ali
2010-05-26