0% found this document useful (0 votes)
72 views60 pages

The JavaServer Pages Standard Tag Library

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 60

The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful

JSP tags which encapsulates 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 existing
custom tags with JSTL tags.
The JSTL tags can be classified, according to their functions, into following
JSTL tag library groups that can be used when creating a JSP page:

Core Tags

Formatting tags

SQL tags

XML tags

JSTL Functions

Install JSTL Library:


If you are using Apache Tomcat container then follow the following two
simple steps:

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" %>

There are following Core JSTL Tags:


Tag

Description

<c:out >

Like <%= ... >, but for expressions.

<c:set >

Sets the result of an expression evaluation in a 'scope'

<c:remove >

Removes a scoped variable (from a particular scope, if


specified).

<c:catch>

Catches any Throwable that occurs in its body and


optionally exposes it.

<c:if>

Simple conditional tag which evalutes its body if the


supplied condition is true.

<c:choose>

Simple conditional tag that establishes a context for


mutually exclusive conditional operations, marked by
<when> and <otherwise>

<c:when>

Subtag of <choose> that includes its body if its condition


evalutes to 'true'.

<c:otherwise >

Subtag of <choose> that follows <when> tags and runs


only if all of the prior conditions evaluated to 'false'.

<c:import>

Retrieves an absolute or relative URL and exposes its


contents to either the page, a String in 'var', or a Reader
in 'varReader'.

<c:forEach >

The basic iteration tag, accepting many different


collection types and supporting subsetting and other
functionality .

<c:forTokens>

Iterates over tokens, separated by the supplied


delimeters.

<c:param>

Adds a parameter to a containing 'import' tag's URL.

<c:redirect >

Redirects to a new URL.

<c:url>

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 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" %>

Following is the list of Formatting JSTL Tags:


Tag

Description

<fmt:formatNumber>

To render numerical value with specific precision


or format.

<fmt:parseNumber>

Parses the string representation of a number,


currency, or percentage.

<fmt:formatDate>

Formats a date and/or time using the supplied


styles and pattern

<fmt:parseDate>

Parses the string representation of a date and/or

time

<fmt:bundle>

Loads a resource bundle to be used by its tag


body.

<fmt:setLocale>

Stores the given locale in the locale configuration


variable.

<fmt:setBundle>

Loads a resource bundle and stores it in the


named scoped variable or the bundle configuration
variable.

<fmt:timeZone>

Specifies the time zone for any time formatting or


parsing actions nested in its body.

<fmt:setTimeZone>

Stores the given time zone in the time zone


configuration variable

<fmt:message>

To display an internationalized message.

<fmt:requestEncoding>

Sets the request character encoding

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" %>

Following is the list of SQL JSTL Tags:


Tag

Description

<sql:setDataSource>

Creates a simple DataSource suitable only for


prototyping

<sql:query>

Executes the SQL query defined in its body or


through the sql attribute.

<sql:update>

Executes the SQL update defined in its body or


through the sql attribute.

<sql:param>

Sets a parameter in an SQL statement to the


specified value.

<sql:dateParam>

Sets a parameter in an SQL statement to the


specified java.util.Date value.

<sql:transaction >

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
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:

XercesImpl.jar: Download it fromhttp://www.apache.org/dist/xerces/j/

xalan.jar: Download it from http://xml.apache.org/xalan-j/index.html

Following is the list of XML JSTL Tags:


Tag

Description

<x:out>

Like <%= ... >, but for XPath expressions.

<x:parse>

Use to parse XML data specified either via an attribute or


in the tag body.

<x:set >

Sets a variable to the value of an XPath expression.

<x:if >

Evaluates a test XPath expression and if it is true, it


processes its body. If the test condition is false, the body
is ignored.

<x:forEach>

To loop over nodes in an XML document.

<x:choose>

Simple conditional tag that establishes a context for


mutually exclusive conditional operations, marked by
<when> and <otherwise>

<x:when >

Subtag of <choose> that includes its body if its


expression evalutes to 'true'

<x:otherwise >

Subtag of <choose> that follows <when> tags and runs


only if all of the prior conditions evaluated to 'false'

<x:transform >

Applies an XSL transformation on a XML document

<x:param >

Use 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 library in your JSP:
<%@ taglib prefix="fn"
uri="http://java.sun.com/jsp/jstl/functions" %>

Following is the list of JSTL Functions:


Function

Description

fn:contains()

Tests if an input string contains the specified


substring.

fn:containsIgnoreCase()

Tests if an input string contains the specified


substring in a case insensitive way.

fn:endsWith()

Tests if an input string ends with the specified


suffix.

fn:escapeXml()

Escapes characters that could be interpreted as


XML markup.

fn:indexOf()

Returns the index withing a string of the first


occurrence of a specified substring.

fn:join()

Joins all elements of an array into a string.

fn:length()

Returns the number of items in a collection, or


the number of characters in a string.

fn:replace()

Returns a string resulting from replacing in an


input string all occurrences with a given string.

fn:split()

Splits a string into an array of substrings.

fn:startsWith()

Tests if an input string starts with the specified


prefix.

fn:substring()

Returns a subset of a string.

fn:substringAfter()

Returns a subset of a string following a specific


substring.

fn:substringBefore()

Returns a subset of a string before a specific


substring.

fn:toLowerCase()

Converts all of the characters of a string to lower


case.

fn:toUpperCase()

Converts all of the characters of a string to upper


case.

fn:trim()

Removes white spaces from both ends of a string

JSTL Core <c:out> Tag


The <c:out> tag displays the result of an expression, similar to the way <
%= %> works with a difference that <c:out> tag lets you use the simpler
"."
notation
to
access
properties.
For
example,
to
access
customer.address.street
just
use
tag
is
<c:out
value="customer.address.street"/>.
The <c:out> tag can automatically escape XML tags so they aren't
evaluated as actual tags.

Attribute:
The <c:out> tag has following attributes:
Attribute

Description

Required

Default

value

Information to output

Yes

None

default

Fallback information to output

No

body

escapeXml

True if the tag should escape special XML


characters

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>

This would produce following result:


<tag> , &

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

Name of the variable whose property should be


modified

No

None

property

Property to modify

No

None

var

Name of the variable to store information

No

None

scope

Scope of variable to store information

No

Page

If target is specified, property must also be specified.

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>

This would produce following result:


4000

JSTL Core <c:remove> Tag


The <c:remove> tag removes a variable from either a specified scope or
the first scope where the variable is found (if no scope is specified). This
action is not normally particularly helpful, but it can aid in ensuring that a
JSP cleans up any scoped resources it is responsible for.

Attribute:
The <c:remove> tag has following attributes:
Attribute

Description

Required

Default

var

Name of the variable to remove

Yes

None

scope

Scope of the variable to remove

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>

<c:set var="salary" scope="session" value="${2000*2}"/>


<p>Before Remove Value: <c:out value="${salary}"/></p>
<c:remove var="salary"/>
<p>After Remove Value: <c:out value="${salary}"/></p>
</body>
</html>

This would produce following result:


Before Remove Value: 4000
After Remove Value:

JSTL Core <c:catch> Tag


The <c:catch> tag catches any Throwable that occurs in its body and
optionally exposes it. Simply it is used for error handling and to deal more
gracefully with the problem.

Attribute:
The <c:catch> tag has following attributes:
Attribute

Description

Required

Default

var

The name of the variable to hold the


java.lang.Throwable if thrown by elements in
the body.

No

None

Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>

<title><c:catch> Tag Example</title>


</head>
<body>

<c:catch var ="catchException">


<% int x = 5/0;%>
</c:catch>

<c:if test = "${catchException != null}">


<p>The exception is : ${catchException} <br />
There is an exception: ${catchException.message}</p>
</c:if>

</body>
</html>

This would produce following result:


The exception is : java.lang.ArithmaticException: / by zero
There is an exception: / by zero

JSTL Core <c:if> Tag


The <c:if> tag evaluates an expression and displays its body content only if
the expression evaluates to true.

Attribute:
The <c:if> tag has following attributes:
Attribute

Description

Required

Default

test

Condition to evaluate

Yes

None

var

Name of the variable to store the condition's


result

No

None

scope

Scope of the variable to store the condition's


result

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>

This would produce following result:


My salary is: 4000

JSTL Core <c:choose>, <c:when>, <c:otherwise> Tag


The <c:choose> works like a Java switch statement in that it lets you
choose between a number of alternatives. Where the switch statement
has casestatements, the <c:choose> tag has <c:when> tags. A a switch
statement hasdefault clause to specify a default action and similar way
<c:choose> has <c:otherwise> as default clause.

Attribute:

The <c:choose> tag does not have any attribute.

The <c:when> tag has one attributes which is listed below.

The <c:otherwise> tag does not have any attribute.

The <c:when> tag has following attributes:


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>

This would produce following result:


Your salary is : 4000
Salary is very good.

JSTL Core <c:choose>, <c:when>, <c:otherwise> Tag


The <c:choose> works like a Java switch statement in that it lets you
choose between a number of alternatives. Where the switch statement
has casestatements, the <c:choose> tag has <c:when> tags. A a switch
statement hasdefault clause to specify a default action and similar way
<c:choose> has <c:otherwise> as default clause.

Attribute:

The <c:choose> tag does not have any attribute.

The <c:when> tag has one attributes which is listed below.

The <c:otherwise> tag does not have any attribute.

The <c:when> tag has following attributes:


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>

This would produce following result:


Your salary is : 4000
Salary is very good.

JSTL Core <c:import> Tag


The <c:import> tag provides all of the functionality of the <include> action
but also allows for inclusion of absolute URLs.
For example, using the import tag allows for inclusion of content from a
different Web site or an FTP server.

Attribute:
The <c:import> tag has following attributes:

Attribute

Description

Require
d

Default

url

URL to retrieve and import into the


page

Yes

None

context

/ followed by the name of a local web


application

No

Current
application

charEncoding

Character set to use for imported


data

No

ISO-8859-1

var

Name of the variable to store


imported text

No

Print to page

scope

Scope of the variable used to store


imported text

No

Page

varReader

Name of an alternate variable to


expose java.io.Reader

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.

JSTL Core <c:forEach>, <c:forTokens> Tag


These tags exist as a good alternative to embedding a Java for,
while, or do-while loop via a scriptlet. The <c:forEach> tag is the more
commonly used tag because it iterates over a collection of objects. The
<c:forTokens> tag is used to break a string into tokens and iterate through
each of the tokens.

Attribute:
The <c:forEach> tag has following attributes:
Attribute

Description

Required

Default

items

Information to loop over

No

None

begin

Element to start with (0 = first item, 1 =


second item, ...)

No

end

Element to end with (0 = first item, 1 =


second item, ...)

No

Last
element

step

Process every step items

No

var

Name of the variable to expose the current


item

No

None

varStatus

Name of the variable to expose the loop


status

No

None

The <c:forTokens> tag has similar attributes as <c:forEach> except one


additional attribute delims which specifies sharacters to use as delimiters.
Attribute

Description

Required

Default

delims

Characters to use as delimiters

Yes

None

Example for <c:forEach>:


<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:forEach> Tag Example</title>
</head>
<body>
<c:forEach var="i" begin="1" end="5">
Item <c:out value="${i}"/><p>
</c:forEach>
</body>
</html>

This would produce following result:


Item 1
Item 2
Item 3
Item 4
Item 5

Example for <c:forTokens>:


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

<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>

This would produce following result:


Zara
nuha
roshy

JSTL Core <c:param> Tag


The <c:param> tag allows proper URL request parameter to be specified
with URL and it does any necessary URL encoding required.
Within a <c:param> tag, the name attribute indicates the parameter name,
and the value attribute indicates the parameter value:

Attribute:
The <c:param> tag has following attributes:
Attribute

Description

Required

Default

name

Name of the request parameter to set in the


URL

Yes

None

value

Value of the request parameter to set in the

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}"/>

Above request would pass URL as below - Try it yourself.


"/index.jsp?trackingId=1234;reportType=summary"

JSTL Core <c:redirect> Tag


The <c:redirect> tag redirects the browser to an alternate URL by providing
automatically URL rewriting, it supports context-relative URLs, and it
supports the <c:param> tag.

Attribute:
The <c:redirect> tag has following attributes:
Attribute

Description

Required

Default

url

URL to redirect the user's browser to

Yes

None

context

/ followed by the name of a local


web application

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>

Above example would redirect request to http://www.photofuntoos.com Try it yourself.

JSTL Core <c:url> Tag


The <c:url> tag formats a URL into a string and stores it into a variable.
This tag automatically performs URL rewriting when necessary. The var
attribute specifies the variable that will contain the formatted URL.
The JSTL url tag is just an alternative method of writing the call to the
response.encodeURL() method. The only real advantage the url tag
provides is proper URL encoding, including any parameters specified by
children paramtag.

Attribute:
The <c:url> tag has following attributes:
Attribute

Description

Required

Default

value

Base URL

Yes

None

context

/ followed by the name of a local


web application

No

Current application

var

Name of the variable to expose the


processed URL

No

Print to page

scope

Scope of the variable to expose the


processed URL

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>

This would produce following result:


TEST

JSTL Core <fmt:formatNumber> Tag


The <fmt:formatNumber> tag is used to format numbers, percentages, and
currencies.

Attribute:

The <fmt:formatNumber> tag has following attributes:


Attribute

Description

Required

Default

value

Numeric value to display

Yes

None

type

NUMBER, CURRENCY, or
PERCENT

No

Number

pattern

Specify a custom formatting


pattern for the output.

No

None

currencyCode

Currency code (for


type="currency")

No

From the
default locale

currencySymbol

Currency symbol (for


type="currency")

No

From the
default locale

groupingUsed

Whether to group numbers


(TRUE or FALSE)

No

true

maxIntegerDigits

Maximum number of integer


digits to print

No

None

minIntegerDigits

Minimum number of integer


digits to print

No

None

maxFractionDigits

Maximum number of fractional


digits to print

No

None

minFractionDigits

Minimum number of fractional


digits to print

No

None

var

Name of the variable to store the

No

Print to page

formatted number

scope

Scope of the variable to store


the formatted number

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.

Grouping can be used to insert commas between thousands groups. Grouping is


specified by setting the groupingIsUsed attribute to either true or false. When
using grouping with minIntegerDigits, you must be careful to get your intended
result.

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.

Represents in exponential form.

Represents a digit; displays 0 as absent.

Serves as a placeholder for a decimal separator.

Serves as a placeholder for a grouping separator.

Separates formats.

Used as the default negative prefix.

Multiplies by 100 and displays as a percentage.

Multiplies by 1000 and displays as per mille.

Represents the currency sign; replaced by actional currency symbol.

Indicates that any other characters can be used in the prefix or


suffix.

'

Used to quote special characters in a prefix or suffix.

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: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>

This would produce following result:

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

JSTL Core <fmt:parseNumber> Tag


The <fmt:parseNumber> tag is used to parse numbers, percentages, and
currencies.

Attribute:
The <fmt:parseNumber> tag has following attributes:
Attribute

Description

Required

Default

value

Numeric value to read (parse)

No

Body

type

NUMBER, CURRENCY, or PERCENT

No

number

parseLocale

Locale to use when parsing the number

No

Default

locale

integerOnly

Whether to parse to an integer (true) or


floating-point number (false)

No

false

pattern

Custom parsing pattern

No

None

timeZone

Time zone of the displayed date

No

Default time
zone

var

Name of the variable to store the parsed


number

No

Print to page

scope

Scope of the variable to store the


formatted number

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" />

<fmt:parseNumber var="i" type="number" value="${balance}" />


<p>Parsed Number (1) : <c:out value="${i}" /></p>
<fmt:parseNumber var="i" integerOnly="true"
type="number" value="${balance}" />
<p>Parsed Number (2) : <c:out value="${i}" /></p>

</body>
</html>

This would produce following result:


Number Parsing:
Parsed Number (1) : 1250003.35
Parsed Number (2) : 1250003

JSTL Core <fmt:formatDate> Tag


The <fmt:formatDate> tag is used to format dates in a variety of ways

Attribute:
The <fmt:formatDate> tag has following attributes:
Attribute

Description

Required

Default

value

Date value to display

Yes

None

type

DATE, TIME, or BOTH

No

date

dateStyle

FULL, LONG, MEDIUM, SHORT, or


DEFAULT

No

default

timeStyle

FULL, LONG, MEDIUM, SHORT, or


DEFAULT

No

default

pattern

Custom formatting pattern

No

None

timeZone

Time zone of the displayed date

No

Default time
zone

var

Name of the variable to store the


formatted date

No

Print to page

scope

Scope of the variable to store the


formatted date

No

page

The pattern attribute to specify even more precise handling of the date:
Code

Purpose

Sample

The era designator

AD

The year

2002

The month

April & 04

The day of the month

20

The hour(12-hour time)

12

The hour(24-hour time)

The minute

45

The second

52

The millisecond

970

The day of the week

Tuesday

The day of the year

180

The day of the week in the month

2 (2nd Wed in month)

The week in the year

27

The week in the month

The a.m./p.m. indicator

PM

The hour(12-hour time)

24

The hour(24-hour time)

The time zone

Central Standard Time

'

The escape for text

''

The single quote

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()%>" />

<p>Formatted Date (1): <fmt:formatDate type="time"


value="${now}" /></p>
<p>Formatted Date (2): <fmt:formatDate type="date"
value="${now}" /></p>
<p>Formatted Date (3): <fmt:formatDate type="both"
value="${now}" /></p>
<p>Formatted Date (4): <fmt:formatDate type="both"

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>

This would produce following result:


Date Format:
Formatted Date (1): 14:27:18
Formatted Date (2): 23-Sep-2010
Formatted Date (3): 23-Sep-2010 14:27:18
Formatted Date (4): 23/09/10 14:27
Formatted Date (5): 23-Sep-2010 14:27:18
Formatted Date (6): 23 September 2010 14:27:18 GST
Formatted Date (7): 2010-09-23

JSTL Core <fmt:parseDate> Tag


The <fmt:parseDate> tag is used to parse dates.

Attribute:
The <fmt:parseDate> tag has following attributes:
Attribute

Description

Require
d

Default

value

Date value to read (parse)

No

Body

type

DATE, TIME, or BOTH

No

date

dateStyle

FULL, LONG, MEDIUM, SHORT, or


DEFAULT

No

Default

timeStyle

FULL, LONG, MEDIUM, SHORT, or


DEFAULT

No

Default

parseLocale

Locale to use when parsing the date

No

Default locale

pattern

Custom parsing pattern

No

None

timeZone

Time zone of the parsed date

No

Default time
zone

var

Name of the variable to store the


parsed date

No

Print to page

scope

Scope of the variable to store the


formatted date

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" />

<fmt:parseDate value="${now}" var="parsedEmpDate"


pattern="dd-MM-yyyy" />
<p>Parsed Date: <c:out value="${parsedEmpDate}" /></p>

</body>
</html>

This would produce following result:


Date Parsing:
Parsed Date: Wed Oct 20 00:00:00 GST 2010

JSTL SQL <sql:setDataSource> Tag


The <sql:setDataSource> tag sets the data source configuration variable or
saves the data-source information in a scoped variable that can be used as
input to the other JSTL database actions.

Attribute:
The <sql:setDataSource> tag has following attributes:

Attribute

Description

Required

Default

driver

Name of the JDBC driver class to be


registered

No

None

url

JDBC URL for the database connection

No

None

user

Database username

No

None

password

Database password

No

None

password

Database password

No

None

dataSource

Database prepared in advance

No

None

var

Name of the variable to represent the


database

No

Set
default

scope

Scope of the variable to represent the


database

No

Page

Example:
Consider the following information about your MySQL database setup:

We are using JDBC MySQL driver.

We are going to connect to TEST database on local machine.

We would use user_id and mypassword to access TEST database.

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" %>

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


<html>
<head>
<title>JSTL sql:setDataSource Tag</title>
</head>
<body>

<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"


url="jdbc:mysql://localhost/TEST"
user="user_id" password="mypassword"/>

<sql:query dataSource="${snapshot}" sql="..." var="result" />

</body>
</html>

JSTL SQL <sql:query> Tag


The <sql:query> tag executes an SQL SELECT statement and saves the
result in a scoped variable.

Attribute:
The <sql:query> tag has following attributes:
Attribute

Description

Require
d

Default

sql

SQL command to execute (should return


a ResultSet)

No

Body

dataSource

Database connection to use (overrides


the default)

No

Default
database

maxRows

Maximum number of results to store in


the variable

No

Unlimited

startRow

Number of the row in the result at which


to start recording

No

var

Name of the variable to represent the


database

No

Set default

scope

Scope of variable to expose the result


from the database

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>

Create Data Records


Finally you create few records in Employee table as follows:
mysql> INSERT INTO Employees VALUES (100, 18, 'Zara', 'Ali');
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO Employees VALUES (101, 25, 'Mahnaz', 'Fatma');


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Employees VALUES (102, 30, 'Zaid', 'Khan');


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Employees VALUES (103, 28, 'Sumit', 'Mittal');


Query OK, 1 row affected (0.00 sec)

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>

<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"


url="jdbc:mysql://localhost/TEST"
user="root" password="pass123"/>

<sql:query dataSource="${snapshot}" var="result">


SELECT * from Employees;
</sql:query>

<table border="1" width="100%">


<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</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.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

JSTL SQL <sql:update> Tag


The <sql:update> tag executes an SQL statement that does not return
data, for example SQL INSERT, UPDATE, or DELETE statements.

Attribute:
The <sql:update> tag has following attributes:
Attribute

Description

Required

Default

sql

SQL command to execute (should not


return a ResultSet)

No

Body

dataSource

Database connection to use (overrides

No

Default

the default)

database

var

Name of the variable to store the count


of affected rows

No

None

scope

Scope of the variable to store the count


of affected rows

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>

Create Data Records


Finally you create few records in Employee table as follows:
mysql> INSERT INTO Employees VALUES (100, 18, 'Zara', 'Ali');
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO Employees VALUES (101, 25, 'Mahnaz', 'Fatma');


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Employees VALUES (102, 30, 'Zaid', 'Khan');


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Employees VALUES (103, 28, 'Sumit', 'Mittal');


Query OK, 1 row affected (0.00 sec)

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.*" %>

<%@ 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:update Tag</title>
</head>
<body>

<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"


url="jdbc:mysql://localhost/TEST"
user="root" password="pass123"/>

<sql:update dataSource="${snapshot}" var="count">


INSERT INTO Employees VALUES (104, 2, 'Nuha', 'Ali');
</sql:update>

<sql:query dataSource="${snapshot}" var="result">


SELECT * from Employees;
</sql:query>

<table border="1" width="100%">


<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</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.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.

JSTL SQL <sql:param> Tag


The <sql:param> tag used as a nested action for <sql:query> and
<sql:update> to supply a value for a value placeholder. If a null value is
provided, the value is set to SQL NULL for the placeholder.

Attribute:
The <sql:param> tag has following attributes:
Attribute

Description

Required

Default

value

Value of the parameter to set

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),

last varchar (255)


);
Query OK, 0 rows affected (0.08 sec)
mysql>

Create Data Records


Finally you create few records in Employee table as follows:
mysql> INSERT INTO Employees VALUES (100, 18, 'Zara', 'Ali');
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO Employees VALUES (101, 25, 'Mahnaz', 'Fatma');


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Employees VALUES (102, 30, 'Zaid', 'Khan');


Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Employees VALUES (103, 28, 'Sumit', 'Mittal');


Query OK, 1 row affected (0.00 sec)

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>

<title>JSTL sql:param Tag</title>


</head>
<body>

<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"


url="jdbc:mysql://localhost/TEST"
user="root" password="pass123"/>

<c:set var="empId" value="103"/>

<sql:update dataSource="${snapshot}" var="count">


DELETE FROM Employees WHERE Id = ?
<sql:param value="${empId}" />
</sql:update>

<sql:query dataSource="${snapshot}" var="result">


SELECT * from Employees;
</sql:query>

<table border="1" width="100%">


<tr>
<th>Emp ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</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.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

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.

JSTL SQL <sql:dateParam> Tag

The <sql:dateParam> tag is used as a nested action for <sql:query> and


<sql:update> supply a date and time value for a value placeholder. If a null
value is provided, the value is set to SQL NULL for the placeholder.

Attribute:
The <sql:dateParam> tag has following attributes:
Attribute

Description

Require
d

Default

value

Value of the date parameter to set


(java.util.Date)

No

Body

type

DATE (date only), TIME (time only), or


TIMESTAMP (date and time)

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),

last varchar (255),


dob date
);
Query OK, 0 rows affected (0.08 sec)
mysql>

Create Data Records


Finally you create few records in Employee table as follows:
mysql> INSERT INTO Students
VALUES (100, 'Zara', 'Ali', '2002/05/16');
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO Students


VALUES (101, 'Mahnaz', 'Fatma', '1978/11/28');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Students


VALUES (102, 'Zaid', 'Khan', '1980/10/10');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Students


VALUES (103, 'Sumit', 'Mittal', '1971/05/08');
Query OK, 1 row affected (0.00 sec)

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.*" %>

<%@ page import="java.util.Date,java.text.*" %>


<%@ 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:dataParam Tag</title>
</head>
<body>

<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"


url="jdbc:mysql://localhost/TEST"
user="root" password="pass123"/>

<%
Date DoB = new Date("2001/12/16");
int studentId = 100;
%>

<sql:update dataSource="${snapshot}" var="count">


UPDATE Students SET dob = ? WHERE Id = ?
<sql:dateParam value="<%=DoB%>" type="DATE" />
<sql:param value="<%=studentId%>" />
</sql:update>

<sql:query dataSource="${snapshot}" var="result">


SELECT * from Students;
</sql:query>

<table border="1" width="100%">


<tr>
<th>Emp ID</th>
<th>First Name</th>

<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

JSTL SQL <sql:transaction> Ta


The <sql:transaction> tag is used to group <sql:query> and <sql:update>
into transactions. You can put as many <sql:query> and <sql:update> as
statements inside <sql:transaction> to make them a single transaction.

It ensures that the database modifications performed by the nested actions


are either committed or rolled back if an exception is thrown by any nested
action.

Attribute:
The <sql:transaction> tag has following attributes:
Attribute

Description

Require
d

Default

dataSource

Database connection to use (overrides the


default)

No

Default
database

isolation

Transaction isolation (READ_COMMITTED,


READ_UNCOMMITTED,
REPEATABLE_READ, or SERIALIZABLE)

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

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),
last varchar (255),
dob date
);
Query OK, 0 rows affected (0.08 sec)
mysql>

Create Data Records


Finally you create few records in Employee table as follows:
mysql> INSERT INTO Students
VALUES (100, 'Zara', 'Ali', '2002/05/16');
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO Students


VALUES (101, 'Mahnaz', 'Fatma', '1978/11/28');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Students


VALUES (102, 'Zaid', 'Khan', '1980/10/10');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Students


VALUES (103, 'Sumit', 'Mittal', '1971/05/08');
Query OK, 1 row affected (0.00 sec)

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.*" %>

<%@ 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:transaction Tag</title>
</head>
<body>

<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"


url="jdbc:mysql://localhost/TEST"
user="root" password="cohondob"/>

<%
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>

<sql:query dataSource="${snapshot}" var="result">


SELECT * from Students;
</sql:query>

<table border="1" width="100%">


<tr>
<th>Emp ID</th>
<th>First Name</th>
<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:
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

You might also like