How To Install JSTL Library
How To Install JSTL 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/cor
e" %>
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
Require
d
Defaul
t
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
Require Defaul
d
t
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>
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
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>
<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>
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
Require
d
Default
test
Condition to evaluate
Yes
None
var
None
scope
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>
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
case statements, the <c:choose> tag has <c:when> tags. A a switch
statement has default clause to specify a default action and similar way
<c:choose> has <c:otherwise> as default clause.
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>
Your salary is :
4000
Salary is very
good.
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
case statements, the <c:choose> tag has <c:when> tags. A a switch
statement has default clause to specify a default action and similar way
<c:choose> has <c:otherwise> as default clause.
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>
Your salary is :
4000
Salary is very
good.
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
case statements, the <c:choose> tag has <c:when> tags. A a switch
statement has default clause to specify a default action and similar way
<c:choose> has <c:otherwise> as default clause.
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>
Your salary is :
4000
Salary is very
good.
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
case statements, the <c:choose> tag has <c:when> tags. A a switch
statement has default clause to specify a default action and similar way
<c:choose> has <c:otherwise> as default clause.
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>
Your salary is :
4000
Salary is very
good.
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
Requir
Default
ed
url
Yes
None
context
No
Current
application
charEncodi
Character set to use for imported data
ng
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>
Attribute:
The <c:forEach> tag has following attributes:
Attribute
Description
Requir
ed
Default
items
No
None
begin
No
end
No
Last
element
step
No
var
No
None
varStatus
No
None
Attribute
Description
Required
Default
delims
Yes
None
1
2
3
4
5
Attribute:
The <c:forEach> tag has following attributes:
Attribute
Description
Requir
ed
Default
items
No
None
begin
No
end
No
Last
element
step
No
var
No
None
varStatus
No
None
Description
Required
Default
delims
Yes
None
1
2
3
4
5
nuha
roshy
Attribute:
The <c:param> tag has following attributes:
Attribute
Description
Required Default
name
Yes
None
value
No
Body
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
Requir
ed
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>
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:
Attribute:
The <fmt:parseNumber> tag has following attributes:
Attribute
Description
Requir
Default
ed
value
No
Body
type
No
number
parseLocal
e
No
Default
locale
integerOnl
y
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" />
<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>
Attribute:
The <fmt:formatDate> tag has following attributes:
Attribute
Description
Requir
ed
Default
value
Yes
None
type
No
date
dateStyle
No
default
timeStyle
No
default
pattern
No
None
timeZone
No
Default time
zone
var
Print to page
scope
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" %>
Date Format:
Attribute:
The <fmt:parseDate> tag has following attributes:
Attribute
Description
Requir
ed
Default
value
No
Body
type
No
date
dateStyle
No
Default
timeStyle
No
Default
parseLocal
e
No
Default locale
pattern
No
None
timeZone
No
Default time
zone
var
scope
No
Print to page
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>
The <fmt:bundle> tag will make the specified bundle available to all
<fmt:message> tags that occur between the bounding <fmt:bundle> and
</fmt:bundle> tags. This saves you the extra step of having to specify the
resource bundle for each of your <fmt:message> tags.
For example, the following two <fmt:bundle> blocks would produce the
same output:
<fmt:bundle basename="com.tutorialspoint.Example">
<fmt:message key="count.one"/>
</fmt:bundle>
<fmt:bundle basename="com.tutorialspoint.Example"
prefix="count.">
<fmt:message key="title"/>
</fmt:bundle>
Attribute:
The <fmt:bundle> tag has following attributes:
Attribute
Description
Requir Defa
ed
ult
basename
Yes
None
prefix
None
Example:
Resource bundles contain locale-specific objects. Resource bundles contain
key/value pairs. When your program needs a locale-specific resource, you
keep all the keys common to all the locale but you can have translated
values specific to locale. Resource bundles helps in providing content specific
to locale.
A Java resource bundle file contains a series of key-to-string mappings. The
method that we focus on involves creating compiled Java classes that extend
the java.util.ListResourceBundle class. You must compile these class files and
make them available to the classpath of your Web application.
Let us define a default resource bundle as follows:
package com.tutorialspoint;
import java.util.ListResourceBundle;
public class Example_En extends
ListResourceBundle {
public Object[][] getContents() {
return contents;
}
static final Object[][] contents = {
{"count.one", "One"},
{"count.two", "Two"},
{"count.three", "Three"},
};
}
The <fmt:timeZone> tag is used to specify the time zone that all tags within
its body will use.
Attribute:
The <fmt:timeZone> tag has following attributes:
Attribute
Description
Required
Default
value
Yes
None
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"
prefix="fmt" %>
<html>
<head>
<title>JSTL fmt:timeZone Tag</title>
</head>
<body>
<c:set var="now" value="<%=new java.util.Date()
%>" />
<table border="1" width="100%">
<tr>
<td width="100%" colspan="2" bgcolor="#0000FF">
<p align="center">
<b>
<font color="#FFFFFF" size="4">Formatting:
<fmt:formatDate value="${now}" type="both"
timeStyle="long" dateStyle="long" />
</font>
</b>
</p>
</td>
</tr>
<c:forEach var="zone"
items="<%=java.util.TimeZone.getAvailableIDs()
%>">
<tr>
<td width="51%">
<c:out value="${zone}" />
</td>
<td width="49%">
<fmt:timeZone value="${zone}">
<fmt:formatDate value="${now}" timeZone="${zn}"
type="both" />
</fmt:timeZone>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
The <fmt:setTimeZone> tag is used to copy a time zone object into the
specified scoped variable.
Attribute:
The <fmt:setTimeZone> tag has following attributes:
Attribute
Description
Requir Default
ed
value
Yes
None
var
No
Replace
default
scope
No
Page
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"
prefix="fmt" %>
<html>
<head>
<title>JSTL fmt:setTimeZone Tag</title>
</head>
<body>
<c:set var="now" value="<%=new java.util.Date()
%>" />
<p>Date in Current Zone: <fmt:formatDate value="$
{now}"
type="both" timeStyle="long" dateStyle="long"
/></p>
<p>Change Time Zone to GMT-8</p>
<fmt:setTimeZone value="GMT-8" />
<p>Date in Changed Zone: <fmt:formatDate
value="${now}"
type="both" timeStyle="long" dateStyle="long"
/></p>
</body>
</html>
Attribute:
The <fmt:message> tag has following attributes:
Attribute
Description
Require
Default
d
key
No
Body
bundle
No
Default
bundle
var
No
Print to page
scope
No
Page
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"
prefix="fmt" %>
<html>
<head>
<title>JSTL fmt:message Tag</title>
</head>
<body>
<fmt:setLocale value="en"/>
<fmt:setBundle basename="com.tutorialspoint.Example"
var="lang"/>
<fmt:message key="count.one" bundle="$
{lang}"/><br/>
<fmt:message key="count.two" bundle="$
{lang}"/><br/>
<fmt:message key="count.three" bundle="$
{lang}"/><br/>
</body>
</html>
Attribute:
The <sql:setDataSource> tag has following attributes:
Attribute
Description
Require
d
Default
driver
No
None
url
No
None
user
Database username
No
None
password
Database password
No
None
password
Database password
No
None
No
None
var
Set default
scope
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" %>
<%@ 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>
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
Requir
Default
ed
sql
No
Body
dataSource
No
Default
database
maxRows
No
Unlimited
startRow
No
var
No
Set default
scope
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>
Now let us write a JSP which will make use of <sql:query> to execute a SQL
SELECT statement as follows:
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
Requir
Default
ed
sql
No
Body
dataSource
No
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:
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
Ag
e
100
Zara
Ali
18
101
Mahnaz
Fatma
25
102
Zaid
Khan
30
103
Sumit
Mittal
28
104
Nula
Ali
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:
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
Ag
e
100
Zara
Ali
18
101
Mahnaz
Fatma
25
102
Zaid
Khan
30
Attribute:
The <sql:dateParam> tag has following attributes:
Attribute
Description
Requir
Default
ed
value
No
Body
type
No
TIMESTAM
P
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>
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>
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-1216
101
Mahnaz
Fatma
1978-1128
102
Zaid
Khan
1980-1010
103
Sumit
Mittal
1971-0508
Attribute:
The <sql:transaction> tag has following attributes:
Attribute
Description
Requir
Default
ed
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
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),
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
First
Last
DoB
ID
Name
Name
100
Zara
Ali
2001-1216
101
Mahnaz
Fatma
1978-1128
102
Zaid
Ali
1980-1010
103
Sumit
Shah
1971-0508
104
Nuha
Ali
2010-0526
Attribute:
The <fmt:formatNumber> tag has following attributes:
Attribute
Description
Requir
Default
ed
value
Yes
None
type
No
Number
pattern
No
None
currencyCode
No
currencySymb
Currency symbol (for type="currency") No
ol
groupingUsed
No
true
maxIntegerDi
gits
No
None
No
None
No
None
minFractionDi
gits
None
var
No
Print to page
scope
No
page
If the type attribute is percent or number, then you can use several
number-formatting 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:
<%@ 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>