JSTL
JSTL
***
*JSTL(
JSPSt
andar
dTagLi
brar
y)*
***
*
Quest
ion:
-Whati
swr
ongi
nusi
ngJSPScr
ipt
letTags?
Ans:
-
usi
ngscri
ptl
etcodeproducespagesourcecodet
hatcontainsamixofHTMLtagsand
Javast
atements.Ther
eareseveralr
easonswhythi
smi xingofpr
ogrammingst
ylesi
s
notopt
imal
.
Thepr
imar
yreasonwhyy
oushoul
dn'
tmi
xscr
ipt
letandt
ag-
basedcodei
sther
eadabi
l
ity
i
ssue,
whi
chappl
i
esbot
htohumansandcomput
ers.
Addi
ti
onal
l
y,scr
ipt
letcodecanbemoredi
ffi
cul
tforaHTMLpr ogr
ammert opr
oduce.A
HTMLprogrammermustlearnt
hesy
ntaxofJav
at opr
oducescri
ptl
etcode.
Exampl
e:-
<html>
<head>
<ti
tl
e>Countt
o10i
nJSPscriptl
et</t
it
le>
</head>
<body >
<%
for
(i
nti
=1;i
<=10;i
++)
{%>
<%=i%><br/
>
<%
}
%>
</body>
</ht
ml >
Quest
ion:
-HowJSTLFi
xesJSPScr
ipt
let
'sShor
tcomi
ngs.
Ans:
-
JSTLal
l
owsthehumanprogr
ammert
olookatapr
ogr
am t
hatconsi
stsent
ir
elyof
HTMLandHTML-
li
ket
ags.
Wi
thJSTL,t
heHTMLpr
ogr
ammeri
sal
readypr
ogr
ammi
ngi
naf
ami
l
iart
ag-
based
sy
ntax.
Consi
dert
hefoll
owingexampl
e,whi
chshowshowt
ocountf
rom 1t
o10usi
ngJSTL
rat
hert
hanscr
iptl
etcode.
<%@ tagl
iburi
="ht
tp:
//j
ava.
sun.com/j
stl
/core"pref
ix="
c"%>
<ht
ml >
<head>
<t
it
le>Countt
o10Exampl e(usingJSTL)</
tit
le>
</head>
<body>
<c:f
orEachvar
="i
"begin="
1"end="
10"st
ep="
1">
<c:
outval
ue="
${i
}"/>
<br/ >
</c:
forEach>
</body >
</html>
Wheny ouexaminetheprecedi
ngsourcecode,y
oucanseet hatt
heJSPpageconsi
sts
enti
rel
yoftags.ThecodemakesuseofHTMLt agssuch<head>and<br
>.Theuseof
tagsisnotconfi
nedjusttoHTMLt ags;t
hiscodealsomakesuseofJSTLt agssuch
as<c:f
orEach>and<c:
out>.
JSTLhassupportf
orcommon,str
uct
ural
taskssuchasit
erat
ionandcondi
ti
onals,
tags
formanipul
ati
ng XMLdocuments,i
nter
nati
onali
zat
iontags,and SQLtags.Italso
prov
idesafr
amewor kf
ori
ntegr
ati
ngt
heexisti
ngcust
om tagswit
htheJSTLtags.
TheJSPStandar
dTagLi
brar
y(JSTL)r
epr
esent
sasetoft
agst
osi
mpl
i
fyt
heJSP
devel
opment.
Adv
ant
ageofJSTL
1.FastDev
elopmentJSTLprov
idesmanyt agst
hatsi
mpl i
fytheJSP.
2.CodeReusabi
li
tyWecanuset heJSTLtagsonvari
ouspages.
3.Noneedtousescri
ptl
ettagI
tav oi
dstheuseofscr
ipt
lettag.
Quest
ion:
-Di
sadv
ant
agesOfJSTL?
Ans:
-Fol
l
owi
ngar
esomedi
sadv
ant
agesofJSTL.
1) Overhead
Both JSP scri
ptl
etand JSTLcodearet r
ansl
ated t
o aJavaser
vletforexecut
ion.
BecauseJSPscr i
ptl
etcodeisr
eal
lyj
ustJavaembeddedinJSPf
il
es,ther
eisrel
ati
vely
l
itt
leov er
headinthescri
ptl
etcodethatisgener
ated.Thisi
snott hecasewithtag
l
ibrar
iessuchasJSTL.Lar
geJSPf i
l
esthatmakeuseofJSTLwi l
lgener
ateagreatdeal
ofoverheadcode.Thi
smeanst hatmoreserver-
processi
ngpowerisrequi
redtorun
JSTL-basedsi
test
hanpureJSPscri
ptl
etsi
tes.
Anotherdi
sadvant
ageoft hisover
headist
hatthesizeofaJav aclassi sli
mited.Lar ge
complexJSPpagest hatmakeextensi
veuseofJSTLandot hertagli
br ar
iescanqui ckly
exceedthi
sclass-
sizeli
mitat
ion.I
fyour
unint
ot hi
sli
mitat
ion,y
ouwi llneedtospl i
ty our
JSPpageintomulti
plefil
es.
2) NotasExt ensi
veasJSPScr i
ptl
et
JSPscr i
ptl
etcodei sr
eal
lyjustJavacodethati
sinser
tedi
ntot
heservl
ett hattheWeb
servergenerat
es,soJSPscr ipt
letcodecanmakeuseofnear l
yanycl asst hatJav
a
i
ncludes.Thisgiv
esgreatfl
exibi
l
ityi
ntermsofwhatyoucanact
ual
lyi
ncludei nthi
sJSP
codeofy ourpage.
JSTLisnotsoflexi
ble.JSTLcodei
sanewpr ogrammi nglanguaget
hati
snotnearl
yas
evol
vedasJavai t
sel
f.Thismeansthati
fyouaretocreateapureJSTLimpl
ementati
on,
youdonothav eaccesst othenumberofclassesthatyouwoul di
fyouwereusinga
JSPscri
ptl
et.
Alt
hought hi
sisal imit
ation,itshouldnothi nderyout oomuch.JSP pagesshould
usual
lycontai
nonlypresentat
ion-
levelcode.AlthoughJSPscript
let
sgiveyouaccesst
o
theenti
reJDK,doy oureall
ywantt obeopeni ngsocketconnect
ionsfr
om yourJSP
pages?Suchthingsareusuall
yleftt
oJav aBeans.
I
fapr ogr
ammeri salr
eadyfamil
iarwi
thJava,JSPscr i
ptl
etisnotterr
ibl
yhardtolearn.
Thiscanmaket heequival
entJSTLcodelookl argerandmor ecompl ex.Aft
erall
,all
JSTLcodeisvali
dXML.Thisalonemeansextratypingforyourpr
ogrammer sbecauseit
takesmorekeyst
rokestopr
oduceanXMLt agthandoesi tsscri
ptl
etcounter
part
.
Quest
ion:
-JSTLTagLi
brar
ies.
Ans:
-
Cl
assi
fi
cat
ionofTheJSTLTags
TheJSTLtagscanbeclassi
fi
ed,accor
dingtothei
rfunct
ions,i
ntot
hef
oll
owi
ngJSTL
tagl
i
brar
ygroupst
hatcanbeusedwhencr eat
ingaJSPpage−
Cor
eTags
For
mat
ti
ngt
ags
SQLt
ags
XMLt
ags
JSTLFunct
ions
Cor
eTag
<c:
out
>
The<c:
out>tagdi spl
aystheresultofanexpressi
on.Thisisal
mostsi
mil
artothe
way<%= %>wor ks.The dif
ference her
ei st hat<c:
out
>tag l
etsyou use t
he
si
mpler"
."notation t o access pr oper
ti
es. For exampl e, to access
cust
omer.address.str
eet
,
uset
het
ag<c:
outv
alue="
cust
omer
.addr
ess.
str
eet
"/>.
The<c:out
>tagcanaut
omat
ical
l
yescapeXMLt
agssot
heyar
en'
tev
aluat
edasact
ual
tags.
At
tri
but
e
The<c:
out
>taghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
Val
ue I
nfor
mat
iont
oout
put Yes None
Def
aul
t Fal
l
backi
nfor
mat
iont
oout
put No body
escapeXml Tr
ue i
fthe t
ag shoul
d escape speci
al XML No t
rue
char
act
ers
<c:
set>
The<c:set>tagisJSTL-f
ri
endlyver
sionoft
hesetPr
opert
yacti
on.Thetagishel
pful
becauseiteval
uatesanexpressi
onandusesther
esult
stosetavalueofaJav
aBean
orajava.ut
il
.Mapobject
.
At
tri
but
e
The<c:
set
>taghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
Val
ue I
nfor
mat
iont
osav
e No body
Name of t
he v
ari
abl
e whose pr
oper
ty shoul
d be
Tar
get No None
modi
fi
ed
pr
oper
ty Pr
oper
tyt
omodi
fy No None
Var Nameoft
hev
ari
abl
etost
orei
nfor
mat
ion No None
Scope Scopeofv
ari
abl
etost
orei
nfor
mat
ion No Page
I
ftar
geti
sspeci
fi
ed,
proper
tymustal
sobespeci
fi
ed.
Exampl
eofout&sett
ag
<%@pagecontentType="t
ext
/html
"pageEncoding="
UTF-8"
%>
<%@ t
agl
iburi
=" htt
p://j
ava.
sun.
com/j
sp/jst
l/
core"pref
ix="c"%>
<ht
ml >
<head>
<tit
le>out&setTagExampl
e</
ti
tl
e>
</head>
<body>
<c:setv
ar="sal
ary"scope="sessi
on"v
alue="
${2000*
2}"
/>
<c:outval
ue="${
salary}
"/>
<jsp:useBeanid="b1"cl
ass="p1.JavaBean"></j
sp:useBean>
<c:settarget="
${b1}"pr
opert
y ="mob"value="
123"/ >
<jsp:getProper
tyname="b1"pr oper
ty="mob"></
jsp:get
Proper
ty>
</body>
</
html>
POJOf
il
e(st
oret
hisf
il
eint
o‘Sour
sePackages’
fol
der)
packagep1;
publi
cclassJavaBean
{
pri
vateintmob;
publi
cintget
Mob( ){
r
eturnmob;
}
publi
cv oi
dsetMob(i
ntmob){
t
his.mob=mob;
}
}
<c:
remov
e>
The<c:remov e>tagr
emov esavari
abl
efr
om eit
heraspecif
iedscopeort
hef i
rstscope
wheret hev ariabl
eisfound(ifnoscopeisspecif
ied)
.Thi
sact i
onisnotpar t
icul
arl
y
helpf
ul,buti tcanaidi nensuri
ngthataJSPcl eansupanyscopedr esourcesitis
responsibl
ef or.
At
tri
but
e
The<c:
remov
e>t
aghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
v
ar Nameoft
hev
ari
abl
etor
emov
e Yes None
scope Scopeoft
hev
ari
abl
etor
emov
e No Al
lscopes
Exampl
e
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
cor
e"pr
efi
x="
c"%>
<ht
ml >
<head>
<tit
le>r
emov
eTagExampl
e</
ti
tl
e>
</head>
<body>
<c:setvar="
salar
y"scope=" sessi
on"v
alue="${2*4000}"
/>
<p>BeforeRemoveVal ue:<c:outval
ue="${sal
ary}
"/></
p>
<c:
removevar="salary"/>
<p>AfterRemoveValue: <c:
outv al
ue="${
salar
y}"
/></p>
</body>
</
html>
<c:
for
Each>
Thesetagsexistasagoodal ternat
ivetoembeddi
ngaJavafor
,while,
ordo-whilel
oop
vi
aascr i
ptlet
.The<c:forEach>tagisacommonl yusedt
agbecauseitit
eratesovera
col
lect
ionofobjects.The<c:forTokens>tagi
susedtobreakast
ringint
ot okensand
i
terat
ethrougheachoft hetokens.
At
tri
but
e
The<c:
for
Each>t
aghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
i
tems I
nfor
mat
iont
oloopov
er No None
El
ementtost
artwi
th(
0=f
ir
sti
tem,1=second
begi
n No 0
i
tem,
..
.)
El
ementt
oendwi
th(
0=f
ir
sti
tem,
1=secondi
tem, Last
end No
..
.) el
ement
st
ep Pr
ocessev
eryst
epi
tems No 1
v
ar Nameoft
hev
ari
abl
etoexposet
hecur
renti
tem No None
v
arSt
atus Nameoft
hev
ari
abl
etoexposet
hel
oopst
atus No None
Exampl
efor<c:
for
Each>
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
cor
e"pr
efi
x="
c"%>
<ht
ml >
<head>
<tit
le>f
orEachTagEx
ampl
e</
ti
tl
e>
</head>
<body >
<c:forEachvar="i
"begi
n=" 1"end="
5">
It
em <c:outval
ue="${
i}"
/><br>
</c:forEach>
</body >
</
html>
Theabov
ecodewi
l
lgener
atet
hef
oll
owi
ngr
esul
t−
I
tem 1
I
tem 2
I
tem 3
I
tem 4
I
tem 5
Exampl
efor
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
cor
e"pr
efi
x="
c"%>
<ht
ml >
<head>
<tit
le>f
orEachTagEx
ampl
e</
ti
tl
e>
</head>
<body >
<c:forEachit
ems="Ramesh,
Sati
sh,
Rahul
"var="
name"
>
<c:outval
ue="${
name}"
/><br
>
</c: f
orEach>
</body >
</
html>
Theabov
ecodewi
l
lgener
atet
hef
oll
owi
ngr
esul
t−
Ramesh
Sati
sh
Rahul
<c:
for
Tokens>
The<c:f
orTokens>taghassi
milarat
tri
butesasthatofthe<c:
for
Each>tagexceptone
addi
ti
onalatt
ri
butedeli
mswhichspecif
iesshar
acterst
ouseasdel i
mit
ers.
At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
Del
i
ms Char
act
erst
ouseasdel
i
mit
ers Yes None
Exampl
efor<c:
for
Tokens>
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
cor
e"pr
efi
x="
c"%>
<ht
ml >
<head>
<tit
le>f
orTokensTagExampl
e</
ti
tl
e>
</head>
<body >
<c:forTokensit
ems=" Ramesh;
Sat
ish;
Rahul
"del
i
ms="
;"v
ar="
name"
>
<c:outvalue="${
name}"
/><p>
</c:forTokens>
</body >
</
html>
Theabov
ecodewi
l
lgener
atet
hef
oll
owi
ngr
esul
t−
Ramesh
Sati
sh
Rahul
<c:
cat
ch>
The<c:cat
ch>tag catches anyThrowabl
ethatoccursinit
s body and opt
ional
l
y
exposesit
.Iti
susedforerr
orhandli
ngandtodealmoregr
acef
ull
ywit
htheprobl
em.
At
tri
but
e
The<c:
cat
ch>t
aghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
re Def
aul
d t
The name of the v
ari
abl
e to hol
d the
v
ar j
ava.l
ang.
Thr
owabl
eift
hrownbyel
ementsin No None
thebody.
Exampl
e
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
cor
e"pr
efi
x="
c"%>
<ht
ml >
<head>
<tit
le>cat
chTagExampl
e</
ti
tl
e>
</head>
<body>
<c:catchvar="catchExcepti
on">
<%i ntx=10/0;%>
</c:catch>
Theexceptionis: ${
catchExcept
ion}<br
>
</body>
</
html>
Theabov
ecodewi
l
lgener
atet
hef
oll
owi
ngr
esul
t−
Theexcept
ioni
s:j
ava.
lang.
Ari
thmat
icExcept
ion:
/byzer
o
<c:
if
>
The<c:
if
>tag ev
aluat
es an expr
essi
on and di
spl
aysi
ts bodycont
entonl
yift
he
expr
essi
oneval
uatest
otr
ue.
At
tri
but
e
The<c:
if
>taghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
t
est Condi
ti
ont
oev
aluat
e Yes None
Nameoft
hev
ari
abl
etost
oret
hecondi
ti
on'
s
v
ar No None
r
esul
t
scope Scopeoft
hev
ari
abl
etost
oret
hecondi
ti
on'
s No page
resul
t
Exampl
e
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
cor
e"pr
efi
x="
c"%>
<ht
ml >
<head>
<tit
le>ifTagExampl
e</
ti
tl
e>
</head>
<body>
<c:setvar="salary
"scope="sessi
on"v
alue=" ${
2000*
2}"
/>
<c:i
ftest="${salar
y>2000}">
<p>Mysalaryis:<c:
outval
ue=" ${
sal
ary}
"/><p>
</c:i
f>
</body>
</
html>
Theabov
ecodewi
l
lgener
atet
hef
oll
owi
ngr
esul
t−
Mysal
aryi
s:4000
<c:
choose>
<c:
when>
<c:
other
wise>
The<c:choose>workslikeaJav aswitchstat
ementinthatitl
etsyouchoosebetween
a number of al ter
nativ
es. Wher e t heswi
tchstatement hascasest at
ements,
the<c:
choose>t ag has<c: when>tags. Just as a swi t
ch st at
ement has
thedefaultcl
ause t
o specif
ya def aultacti
on,
<c:choose>has<c:ot
herwise>asthe
defaul
tclause.
At
tri
but
e
The<c:
choose>t
agdoesnothav
eanyat
tr
ibut
e.
The<c:
when>t
aghasoneat
tri
but
eswhi
chi
sli
stedbel
ow.
The<c:
other
wise>t
agdoesnothav
eanyat
tr
ibut
e.
The<c:
when>t
aghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
t
est Condi
ti
ont
oev
aluat
e Yes None
Exampl
e
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
cor
e"pr
efi
x="
c"%>
<ht
ml >
<head>
<tit
le>choose,
when&ot
her
wiseTagsExampl
e</
ti
tl
e>
</head>
<body>
<c:setvar="sal
ary
"scope="sessi
on"v
alue=" ${2000*
2}"
/>
<p>Yoursalaryi
s:<c:
outval
ue=" ${
sal
ary}
"/></p>
<c:choose>
<c:whentest="${
sal
ary<=0}"
>
Sal ar
yisver
ylowtosurv
ive.
</c:when>
<c:whentest="${
sal
ary>1000}
">
Sal ar
yisver
ygood.
</c:when>
<c:otherwise>
Nocommentsi r
..
.
</c:otherwise>
</
c:choose>
</body
>
</
html>
Theabov
ecodewi
l
lgener
atet
hef
oll
owi
ngr
esul
t−
Yoursalar
yis:4000
Sal
aryisverygood.
<c:
impor
t>
The<c:
import
>tagprovi
desallf
unct
ional
i
tiesoft
he<i
ncl
ude>act
ionbutal
soal
l
ows
fort
hei
nclusi
onofabsol
uteURLs.
Forex
ample,usi
ngtheimpor
ttagal
l
owsf
ort
hei
ncl
usi
onofcont
entf
rom adi
ff
erent
Websi
teoranFTPserver
.
At
tri
but
e
The<c:
impor
t>t
aghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
re Def
aul
t
d
ur
l URLtor
etr
iev
eandi
mpor
tint
othe
Yes None
page
/fol
l
owedbyt henameofal
ocal Curr
ent
cont
ext No
webappl
i
cat
ion appl
icat
ion
char
Encodi
n Char
act
ersett
ousef
ori
mpor
ted
No I
SO-
8859-
1
g dat
a
Name of the v
ari
abl
eto st
ore
v
ar No Pr
intt
opage
i
mpor
tedtext
Scopeoft
hevar
iabl
eusedt
ost
ore
scope No Page
i
mportedt
ext
Exampl
e
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
cor
e"pr
efi
x="
c"%>
<ht
ml >
<head>
<tit
le>i
mpor
tTagExampl
e</
ti
tl
e>
</head>
<body>
<c:i
mpor tvar="data"url
="ht
tp:
//www.
googl
e.co.
in"
/>
<c:outvalue="${data}
"/>
</body>
</
html>
<c:
redi
rect
>
The<c:
redi
rect
>tagredi
rect
st hebrowsert
oanalt
ernat
eURLbyf aci
li
tat
ingautomati
c
URLrewri
ti
ng,i
tsuppor
tscontext-
rel
ati
veURLs,
andital
sosuppor
tsthe<c:par
am>t ag.
At
tri
but
e
The<c:
redi
rect
>taghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
re Def
aul
t
d
ur
l URLt
oredi
rectt
heuser
'sbr
owsert
o Yes None
/fol
l
owedbythenameofal
ocalweb Curr
ent
cont
ext No
appl
icat
ion appl
icat
ion
Exampl
e
I
fyouneedtopassparamet
erst
oa<c:
impor
t>t
ag,uset
he<c:
url
>tagt
ocr
eat
ethe
URLf
ir
stasshownbel
ow−
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
cor
e"pr
efi
x="
c"%>
<ht
ml >
<head>
<tit
le>r
edi
rectTagEx
ampl
e</
ti
tl
e>
</head>
<body
>
<c:redi
rectur
l="
htt
ps:
//www.
googl
e.co.
in"
/>
</body>
</
html>
<c:
url
>
The<c:ur
l>tagformatsaURLint
oast ri
ngandst or
esiti
ntoav ar
iabl
e.Thist
ag
aut
omat i
cal
lyperf
ormsURLrewr
it
ingwhennecessar
y.Thev
arat
tr
ibut
especif
iest
he
var
iabl
ethatwil
lcont
aint
hef
ormatt
edURL.
TheJSTLur lt ag is just an alt
ernat
ive method of wr i
ti
ng the callto
theresponse.
encodeURL()met
hod.Theonlyrealadvant
agetheurlt
agpr ov
idesi
s
properURLencoding,
incl
udi
nganypar
ametersspeci
fi
edbychi
l
drenpar
am tag.
At
tri
but
e
The<c:
url
>taghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
re Def
aul
t
d
Val
ue BaseURL Yes None
/fol
l
owedbythenameofal
ocalweb Curr
ent
cont
ext No
appl
icat
ion appl
icat
ion
Nameoft hevar
iabl
etoexposet
he
v
ar No Pr
intt
opage
pr
ocessedURL
Scopeoft hevar
iabl
etoexposet
he
scope No Page
processedURL
<c:
par
am>
The<c:
par
am>t agallowsproperURLrequestparamet
ert
obespeci
fi
edwi
thURLand
al
sodoesthenecessaryURLencodingr
equired.
Wit
hina<c:par
am>tag,thenameat t
ri
but
eindi
cat
est
hepar
amet
ername,andt
he
val
ueat
tri
butei
ndi
cat
estheparamet
erval
ue−
At
tri
but
e
The<c:
par
am>t
aghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
Nameoft
her
equestpar
amet
ert
oseti
nthe
name Yes None
URL
Val
ueoft
her
equestpar
amet
ert
oseti
nthe
v
alue No Body
URL
Exampl
e
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
cor
e"pr
efi
x="
c"%>
<ht
ml >
<head>
<tit
le>ur
l&par
am TagExampl
e</
ti
tl
e>
</head>
<body
>
<c:url
value="/i
ndex.
jsp"var="my URL">
<c:
par
am name=" t
racki
ngId"value="1234"
/>
<c:
par
am name=" r
eport
Ty pe"value="summary
"/>
</c:ur
l>
<c:outvalue="
${myURL} "
/>
</
body
>
</
html
>
SqlTag
<sql
:set
Dat
aSour
ce>
The<sql:
setDataSource>t
agset
st hedat
asourceconf
igur
ati
onv
ari
abl
eorsav
esthe
dat
a-sour
cei nf
ormat i
oninascopedvari
abl
ethatcanbeusedasinputt
otheot
her
JSTLdatabaseactions.
At
tri
but
e
The<sql
:set
Dat
aSour
ce>t
aghast
hef
oll
owi
ngat
tr
ibut
es−
At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
dr
iver Nameoft
heJDBCdr i
vercl
asst
obe
No None
regi
ster
ed
ur
l JDBCURLf
ort
hedat
abaseconnect
ion No None
user Dat
abaseuser
name No None
passwor Dat
abasepasswor
d
No None
d
Nameoft
hev
ari
abletorepr
esentt
he Set
v
ar No
database defaul
t
Scopeoft
hev
ari
abl
etorepr
esentt
he
scope No Page
dat
abase
Exampl
e
Consi
dert
hef
oll
owi
ngi
nfor
mat
ionabouty
ourMy
SQLdat
abaseset
up−
Wear
eusi
ngJDBCMy
SQLdr
iver
.
Wear
egoi
ngt
oconnectt
oTESTdat
abaseonl
ocal
machi
ne.
Wewoul
duseuser
_idandmy
passwor
dtoaccessTESTdat
abase.
Al
ltheaboveparameter
swi l
lvar
ybasedonyourMySQLoranyotherdat
abaseset
up.
Consi
der
ingtheaboveparameter
s,f
oll
owi
ngexampl
eusestheset
DataSourcet
ag−
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
cor
e"pr
efi
x="c"%>
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
sql
"pr
efi
x="sql
"%>
<ht
ml >
<head>
<tit
le>JSTLsql
:
set
Dat
aSour
ceTag</
ti
tl
e>
</head>
<body>
<sql:
setDataSourcevar="snapshot
"dr
iver="
com.
mysql
.j
dbc.
Dri
ver
"
url="
jdbc:mysql:
//l
ocal
host/TYI
T"
user="root"password=" "
/>
</body
>
</
html>
<sql
:updat
e>
The<sql
:updat
e>tag execut
es an SQL st
atementthatdoes notr
etur
n dat
a;f
or
exampl
e,SQLINSERT,UPDATE,orDELETEstatement
s.
At
tri
but
e
The<sql
:updat
e>t
aghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
re Def
aul
t
d
sql SQLcommandtoexecute(
shoul
dnot Body
No
ret
urnaResult
Set
)
dat
aSour Dat
abaseconnect
iontouse(
over
ri
des Default
No
ce t
hedefault
) database
Nameoft
hevar
iabl
etostor
ethecount
v
ar No None
ofaf
fect
edrows
scope Scopeoft
hev
ari
abl
etost
oret
hecount No Page
ofaf
fect
edr
ows
Exampl
e
Letusnowwrit
eaJSPwhichwi
l
lmakeuseofthe<sql
:updat
e>t
agt
oexecut
eanSQL
INSERTst
atementt
ocr
eat
eonerecor
dint
het
ableasfoll
ows−
<%@ pageimpor
t=" j
ava.i
o.*,j
ava.
uti
l.
*,
java.sql.
*"%>
<%@ pageimpor
t=" j
avax.servl
et.
htt
p.*,
javax.ser
v l
et.*
"%>
<%@ tagl
i
buri="ht
tp:/
/java.sun.
com/jsp/jstl
/core"prefi
x="c"
%>
<%@ tagl
i
buri="ht
tp:/
/java.sun.
com/jsp/jstl
/sql
"pr efi
x="sql
"%>
<ht
ml >
<head>
<tit
le>JSTLsql
:
updat
eTag</
ti
tl
e>
</head>
<body>
<sql:
setDataSourcevar="snapshot
"dr
iver="
com.
mysql
.j
dbc.
Dri
ver
"
url="
jdbc:mysql:
//l
ocal
host/TYI
T"
user="root"password=" "
/>
<sql:updatedataSource="${snapshot
}"var="count
">
INSERTI NTOst udentVALUES( 101,
'Deepak'
,'
Panvel
'
);
</sql:update>
</body>
</
html >
<sql
:quer
y>
The<sql
:query
>tagexecut
esanSQLSELECTst
atementandsav
est
her
esul
tina
scopedv
ariabl
e.
At
tri
but
e
The<sql
:quer
y>t
aghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
re Def
aul
t
d
dat
aSour Dat
abaseconnect
iont
ouse No Def
aul
t
ce (
over
ri
dest
hedef
aul
t) dat
abase
maxRows Maxi
mum numberofresult
sto Unl
i
mit
ed
No
st
orei
nthevar
iable
st
art
Row Numberoft
her
owintheresul
t 0
No
atwhi
cht
ost
artr
ecor
ding
Nameoft hevari
ableto Setdef
aul
t
Var No
repr
esentthedatabase
Scopeofvari
abl
etoexposet
he
Scope No Page
r
esul
tfrom t
hedat
abase
Exampl
e
<%@ pageimpor
t=" j
ava.i
o.*,j
ava.
uti
l.
*,
java.sql.
*"%>
<%@ pageimpor
t=" j
avax.servl
et.
htt
p.*,
javax.ser
v l
et.*
"%>
<%@ tagl
i
buri="ht
tp:/
/java.sun.
com/jsp/jstl
/core"prefi
x="c"
%>
<%@ tagl
i
buri="ht
tp:/
/java.sun.
com/jsp/jstl
/sql
"pr efi
x="sql
"%>
<ht
ml >
<head>
<tit
le>JSTLsql
:
quer
yTag</
ti
tl
e>
</head>
<body>
<sql:
setDataSourcevar="snapshot
"dr
iver="
com.
mysql
.j
dbc.
Dri
ver
"
url="
jdbc:mysql:
//l
ocal
host/TYI
T"
user="root"password=" "
/>
<sql
:quer
ydataSource="${
snapshot
}"v
ar="
resul
t"
>
SELECT*from student
;
</
sql:
query
>
<t
ableborder="1"al
i
gn="cent
er"
>
<tr
>
<th>RollNo</t
h>
<th>StudentName</
th>
<th>Address</
th>
</
tr>
<c:
for
Eachv
ar="
row"i
tems="
${r
esul
t.
rows}
">
<tr>
<td><c:outv
alue="
${r
ow.
roll
_no}"
/></t
d>
<td><c:outv
alue="
${r
ow.
stud_name}"/></t
d>
<td><c:outv
alue="
${r
ow.
stud_add}"
/></td>
</tr>
</c:
forEach>
</table>
</body>
</
html>
<sql
:par
am>
The<sql:
param>tag used as a nest ed acti
on f or the<sql:
query
>tag and
the<sql
:updat
e>tagt
osuppl
yav al
ueforavaluepl
aceholder
.Ifanul
lval
ueispr
ovi
ded,
theval
ueissettoSQLNULLfort
heplacehol
der.
At
tri
but
e
The<sql
:par
am>t
aghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
Val
ue Val
ueoft
hepar
amet
ert
oset No Body
Exampl
e
<%@ pageimpor
t=" j
ava.i
o.*,j
ava.
uti
l.
*,
java.sql.
*"%>
<%@ pageimpor
t=" j
avax.servl
et.
htt
p.*,
javax.ser
v l
et.*
"%>
<%@ tagl
i
buri="ht
tp:/
/java.sun.
com/jsp/jstl
/core"prefi
x="c"
%>
<%@ tagl
i
buri="ht
tp:/
/java.sun.
com/jsp/jstl
/sql
"pr efi
x="sql
"%>
<ht
ml >
<head>
<tit
le>JSTLsql
:
par
am Tag</
ti
tl
e>
</head>
<body>
<sql:
set
DataSourcevar="snapshot
"dr
iver="
com.
mysql
.j
dbc.
Dri
ver
"
url="j
dbc:mysql:
//l
ocal
host
/TYIT"
user="root"password=""/
>
<c:
setv
ar="
empI
d"v
alue="
103"
/>
<sql
:
updat
edat
aSour
ce="
${snapshot
}"v
ar="
count
">
DELETEFROM st udentWHERErol
l
_no=?
<sql:par
am value="${empI
d}"/
>
</sql:
update>
<c:outval
ue="DataDelet
ed.
"/>
</body
>
</
html>
<sql
:dat
ePar
am>
The<sql:
dateParam>tag i
s used as a nested acti
on f or t
he<sql:
query>and
the<sql
:update>tagt
osupplyadat
eandti
mev al
ueforav al
ueplacehol
der.I
fanull
val
ueisprovided,t
heval
uei
ssett
oSQLNULLf orthepl
aceholder
.
At
tri
but
e
The<sql
:dat
ePar
am>t
aghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
re Def
aul
t
d
Val
ueofthedateparametert
o Body
Val
ue No
set(j
ava.
uti
l.
Date)
t
ype DATE(dat
eonly)
,TI
ME(ti
me TI
MESTAMP
onl
y),
orTIMESTAMP(dat
eand No
t
ime)
Exampl
e
<%@ pageimpor
t=" j
ava.i
o.*,j
av a.
uti
l.
*,
java.sql.
*"%>
<%@ pageimpor
t=" j
avax.servlet.
htt
p.*,
javax.ser
v l
et.*
"%>
<%@ pageimpor
t=" j
ava.uti
l.Date,
java.
text.*
"%>
<%@ tagl
i
buri="ht
tp:/
/java.sun.com/ j
sp/jstl
/core"prefi
x="c"
%>
<%@ tagl
i
buri="ht
tp:/
/java.sun.com/ j
sp/jstl
/sql
"pr efi
x="sql
"%>
<ht
ml >
<head>
<tit
le>JSTLsql
:
dat
aPar
am Tag</
ti
tl
e>
</head>
<body>
<sql:
set
DataSourcevar="snapshot
"dri
ver="com.mysql
.j
dbc.
Dri
ver
"
ur
l="j
dbc:mysql:
//l
ocal
host/TYI
T"user="r
oot"passwor
d=""/>
<%
Dat
eDoB=newDat e(
"2020/
07/
15"
);
i
ntstudent
Id=100;
%>
<sql:
updatedataSource="${snapshot
}"var="
count
">
UPDATESt udentsSETdob=?WHEREI d=?
<sql:dat
eParam val
ue="<%=DoB%>"t ype="DATE"/>
<sql:par
am value="<%=studentI
d%>"/>
</sql
:update>
</body
>
</
html>
<sql
:t
ransact
ion>
The<sql:
transacti
on>tagisusedtogroupt he<sql:
query
>and<sql:
updat
e>tagsint
o
tr
ansacti
ons.Youcanputasmany<sql :query>and<sql:
update>t
agsasstatement
s
i
nsidethe<sql:t
ransact
ion>t
agtocreat
easi ngl
etransact
ion.
Itensur
esthatthedatabasemodifi
cati
onsperf
ormedbythenest
edacti
onsar
eei
ther
commi t
tedorrol
ledbackifanexcept
ionist
hrownbyanynest
edacti
on.
At
tri
but
e
The<sql
:t
ransact
ion>t
aghast
hef
oll
owi
ngat
tri
but
es−
At
tri
but
e Descr
ipt
ion Requi
re Def
aul
t
d
dat
aSour Dat
abaseconnect
iontouse Defaul
t
No
ce (
overr
idest
hedefault
) database
i
sol
ati
on Transact
ionisolat
ion Dat
abase’
(READ_COMMI TTED, sdefaul
t
READ_ UNCOMMI TTED, No
REPEATABLE_ READ, or
SERI
ALIZABLE)
Exampl
e
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
cor
e"pr
efi
x="c"
%>
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
sql
"pr
efi
x="sql
"%>
<ht
ml >
<head>
<tit
le>JSTLsql
:
transact
ionTag</
ti
tl
e>
</head>
<body>
<sql:
setDataSourcev ar="snapshot"dri
ver="com.my sql
.j
dbc.
Dri
ver
"
url="j
dbc: mysql:
//l
ocalhost/
TYIT"user=" r
oot"password=""/
>
<sql:
transacti
ondat aSource="${snapshot}
">
<sql:
updat e>
i
nsertintostudentvalues(
105,'
Rahul'
,'
Panvel
')
;
</sql:
updat e>
<sql:updat e>
insertintostudentval
ues(
105,'
Prav
in'
,
'NewPanv
el'
);
</sql:update>
</sql:tr
ansaction>
<c:outv alue="
Transact
ionCompleted"
/>
Si
mpleRegi str
ati
onExampl e
Regi
str
ati
on
Rol
lNo:
Name:
Addr
ess:
<html>
<head>
<ti
tl
e>TODOsuppl
yat i
tl
e</
ti
t l
e>
<metachar
set
="UTF-8"
>
<metaname="v
iewport
"content="
widt
h=dev
ice-
widt
h,i
nit
ial
-scal
e=1.
0">
</head>
<body>
<form act i
on="data_sav e.
jsp"met hod=" post "
>
<tablebor der="1"ali
gn=" center"cell
spaci ng="5"cell
padding="10"
>
<tr><thcolspan=" 2">Registrati
on</ th></tr
>
<tr><td>RollNo: </td><td><inputtype=" number "name=" t
1"></t
d></
tr>
<tr><td>Name: </td><td><inputtype=" text"name="t2"></
td></t
r>
<tr><td>Addr ess:</td><td><textareaname=" t3"rows="4"
cols="
20" ></textar
ea></ t
d></ tr
>
<tr><td></td><td><inputt y
pe=" submi t"value="SAVE"></t
d></tr
>
</table>
</form>
</body>
</html>
dat
a_sav
e.j
sp
<ht
ml >
<head>
<tit
le>JSTLsql
:
set
Dat
aSour
ceTag</
ti
tl
e>
</head>
<body>
<sql:setDataSourcev ar=" snapshot"dr
iver="
com.
mysql
.j
dbc.
Dri
ver
"
url="jdbc:mysql:/
/localhost/TYI
T"
user=" root"passwor d=" "
/>
<sql:updatedat aSource=" ${snapshot}
">
insertintostudentv al
ues( ?,
?,
?);
<sql:param value="${param. t
1}"
/>
<sql:param value="${param. t
2}"
/>
<sql:param value="${param. t
3}"
/>
</sql:
update>
<c:outval
ue="Dat
aSav
ed.
"/
>
<sql:
querydat
aSource=" ${
snapshot
}"v
ar="
resul
t"
>
SELECT*from student;
</sql
:query
>
<t
ableborder="1"al
i
gn="cent
er"
>
<tr
>
<th>RollNo</t
h>
<th>StudentName</
th>
<th>Address</
th>
</
tr>
<c:
forEachvar="r
ow"i
tems="${resul
t.
rows} "
>
<tr>
<td><c:outval
ue="
${row.
roll
_no}"
/></t
d>
<td><c:outval
ue="
${row.
stud_name}"/></t
d>
<td><c:outval
ue="
${row.
stud_add}"
/></td>
</tr>
</c:
forEach>
</tabl
e>
</body>
</
html>
XMLt
ags
TheJSTLXMLt agspr
ovi
deaJSP-centri
cwayofcreat
ingandmanipul
ati
ngtheXML
document
s.Fol
l
owingi
sthesy
ntaxtoincl
udet
heJSTLXMLl i
brar
yinyourJSP.
TheJSTLXMLt agli
brar
yhascustom t
agsf
ori
nteract
ingwit
ht heXMLdat a.Thi
s
i
ncludesparsi
ngtheXML,tr
ansf
ormingt
heXMLdata,andthefl
ow cont
rolbasedon
theXPathexpr
essi
ons.
<%@ t
agl
ibpr
efi
x="
x"ur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/xml
"%>
Befor
eyoupr
oceedwiththeexamples,y
ouwillneedtocopythefol
lowi
ngt
woXML
andXPat
hrel
atedl
i
brar
iesint
oyour<TomcatI
nstall
ati
onDi
rect
ory>\
li
b−
Xer
cesI
mpl
.j
ar−Downl
oadi
tfr
om ht
tps:
//www.
apache.
org/
dist
/xer
ces/
j/
xal
an.
jar−Downl
oadi
tfr
om ht
tps:
//xml
.apache.
org/
xal
an-
j/
index.
html
Fol
l
owi
ngi
sthel
i
stofXMLJSTLTags−
S.
No Tag&Descr
ipt
ion At
tri
but
e
.
<x:
out
> At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
Li
ke <%= .
..>,butf
orXPat
h sel
ect XPathexpr
essionto
ev
aluateasastri
ng,oft
en Yes None
ex
pressi
ons. usingXPathvari
ables
1
escapeXm Tr
ueifthetagshoul
d
l escapespecialXML No t
rue
char
acters
2 <x:
par
se> At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
Used to parse t
he XML dat
a v
ar
Av
ari
abl
ethatcont
ainst
he
No None
par
sedXMLdat a
speci
fi
edeitherviaanat
tri
but
e
orint
hetagbody .
xml Textofthedocumentto Body
No
parse(Str
ingorReader
)
doc XMLdocumentt
obepar
sed No Page
Scopeofthevar
iable
scope No Page
specif
iedi
nthevarattr
ibut
e
<x:
set> At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
Set
sav ar
iabl
et othev
alueof v
ar
Avari
ablet
hati
ssett
othe
Yes
Body
anXPat
hexpressi
on. v
alueoftheXPat
hexpr
essi
on
3 sel
ect TheXPat
hexpressi
ont
obe
No None
eval
uated
Scopeofthevar
iable
scope No Page
specif
iedi
nthevarattr
ibut
e
<x:
i
f> At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
processesitsbody .I
fthetest
4 conditi
oni sfal
se,thebodyi s v
ar
Nameofthevari
abletost
ore
No None
t
hecondi
tion'
sresul
t
i
gnor ed.
Scopeofthevar
iable
scope No Page
specif
iedi
nthevarattr
ibut
e
<x:
for
Each> At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
Toloopovernodesi
nanXML sel
ect TheXPat
hexpressi
ont
obe
Yes None
5 document
. eval
uated
Nameofthevari
abletost
ore
v
ar No None
t
hecondi
tion'
sresul
t
Scopeofthevar
iable
scope No Page
specif
iedi
nthevarattr
ibut
e
<x:
choose> NO
Simple condi
tional tag that
6 establi
shes a cont ext for
mut ual
lyexcl
usive condi
ti
onal
operati
ons, marked
by<when>and<ot herwise>ta
gs.
<x:
when> At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
<x:
other
wise> NO
<x:
tr
ansf
orm> At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
Appl
i
esanXSLtransf
ormat
ion doc Sour
ceXMLdocumentfor
No Body
onaXMLdocument t
heXSLTt
ransf
ormat
ion
docSy
stemI URIoftheori
ginal
XML
No None
d document
xsl
t XSLTstyl
esheetprov
idi
ng
9 t
ransformati
on Yes None
i
nstructi
ons
xsl
tSy
stemI
d URIoft
heorigi
nal
XSLT
No None
document
r
esul
t Resultobj
ectt
oacceptt
he Pri
ntt
o
No
transfor
mati
on'
sresul
t page
v
ar Var
iabl
ethati
ssett
othe No Pr
intt
o
t
ransfor
medXML page
document
Scopeofthevariabl
eto
scope exposethe No None
transf
ormati
on'sresul
t
<x:
par
am > At
tri
but
e Descr
ipt
ion Requi
red Def
aul
t
Used al
ong wi
th name Nameoft
heXSLTpar
amet
er
Yes
Body
thetransf
ormtag t o set a t
oset
10 parameter i n t he XSLT
styl
esheet Val
ue
Val
ueoft
heXSLTpar
amet
er
No None
t
oset
EXAMPLE:
-
<%@ t
agl
i
bpr
efi
x="
c"ur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/cor
e"%>
<%@ t
agl
i
bpr
efi
x="
x"ur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
xml "%>
<ht
ml >
<head>
<tit
le>JSTLxml
Tagsexampl
e</
ti
tl
e>
</head>
<body>
<h3>BooksI
nfo:
</h3>
<c:
setvar="xmltext
">
<books>
<book>
<name>Enterpr
iseJava</name>
<author
>Omkar </aut
hor
>
<pri
ce>300</pr
ice>
</book>
<book>
<name>Asp. NetwithC#</
name>
<author
>Nikhil
</aut
hor>
<pri
ce>200</pri
ce>
</book>
</books>
</
c:set
>
<x:
par
sexml
="${
xml
text
}"v
ar="
out
put
"/>
<x:f
orEachselect="$output
/books/
book/name"v
ar="
it
em"
>
BookName: <x:outsel
ect="$i
tem"/><br
>
</x:
forEach>
<br>
<h3>Bookdet ail
s</h3>
<x:f
orEachselect=" $output/books/ book"v ar=" i
tem">
BookName: <x:outselect=" $item/ name"/ ><br>
BookAut hor:<x:outselect=" $item/ author"/><br>
BookPr i
ce:<x:outselect=" $item/ price"/><br><br>
</x:
forEach>
</body>
</
html>
JSTLFunct
ionTags
TheJSTLfunct
ionprovi
desanumberofstandar
dfunct
ions,
mostofthesefunct
ions
arecommonstringmanipul
ati
onf
unct
ions.Thesynt
axusedfori
ncl
udingJSTLfuncti
on
l
ibrar
yiny
ourJSPi s:
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
funct
ions"pr
efi
x="
fn"%>
JSTLFunct
ionTagsLi
st
JSTLFunct
ions Sy
ntax Descr
ipt
ion
f
n:cont
ains(
) boolean Iti
susedt otesti
faninputst
ri
ng
contains(St
ring containi
ngthespeci
fi
edsubstri
ngi
na
i
nput stri
ng,Str
ing program.
substring)
fn:
cont
ainsI eCas bool
gnor ean Iti
susedt ot
estifaninputst
ring
e() contains(St
ring contai
nsthespecif
iedsubstr
ingasa
i
nput stri
ng,Str
ing caseinsensi
ti
veway .
substring)
f
n:endsWi
th(
) boolean I
tisusedtotesti
faninputst
ri
ngends
endsWi t
h(St
ri
ng wi
ththespecif
iedsuf
fix.
i
nput ,
Stri
ng
substri
ng)
f
n:escapeXml
() Str
ing I
tescapesthechar
act
erst
hatwoul
d
escapeXml
(St
ri
ng bei
nterpr
etedasXMLmarkup.
i
nput)
f
n:i
ndexOf
() i
nti
ndexOf(
Stri
ng Itret
urnsanindexwi
thi
nastri
ngof
i
nput
,Str
ingsearch) fir
stoccurr
enceofaspeci
fi
ed
substri
ng.
f
n:t
ri
m() St
ri
ngtr
im(
Str
ing I
tremov
esthebl
ankspacesf
rom bot
h
i
nput
) t
heendsofast
ri
ng.
f
n:st
art
sWi
th(
) boolean Iti
susedforchecki
ngwhet
hert
he
star
tsWith(
Str
ing giv
enstri
ngisstar
tedwi
thapar
ti
cul
ar
i
nput ,
Stri
ng str
ingval
ue.
substri
ng)
f
n:spl
i
t() St
ri
ng[
]spl
it
(St
ri
ng Itspl
i
tsthestr
ingi
ntoanar
rayof
i
nput
,St
ri
ngdeli
mit
) substr
ings.
f
n:t
oLower
Case(
) Stri
ng Itconv
ert
sal
lthecharact
ersofa
toLower
Case(
Str
ing stri
ngtol
owercase.
i
nput)
f
n:t
oUpper
Case(
) Stri
ng Itconv
ert
sallthechar
act
ersofa
toUpper
Case(
Str
ing stri
ngtouppercase.
i
nput)
f
n:subst
ri
ng(
) Str
ing Itr
eturnsthesubsetofastr
ing
subst
ring(
Str
ing accordingtot
hegivenstar
tandend
i
nput,i
ntstar
t,i
nt posit
ion.
end)
f
n:subst
ri
ngAf
ter
() Str
ing Itr
eturnsthesubsetofst
ri
ngaf
tera
subst
ringAf
ter
(St
ri
ng specif
icsubstr
ing.
i
nput
,St
ri
ngsub)
f
n:subst
ri
ngBef
or ) St
e( r
ing Itr
eturnsthesubsetofst
ri
ngbef
orea
substr
ingBef
ore(
Str
in specif
icsubstr
ing.
ginput,St
ri
ngsub)
f
n:l
engt
h() i
nt I
tretur
nsthenumberofchar
acter
s
l
engt
h(j
ava.
lang.
Obj
e i
nsideastri
ng,ort
henumberofit
ems
ct
) i
nacol l
ect
ion.
f
n:r
epl
ace(
) boolean Itr
eplacesal
ltheoccurr
enceofa
repl
ace(St
ri
nginput
, str
ingwithanotherst
ri
ngsequence.
Stri
ngfi
nd,Str
ing
repl
acewit
h)
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
core"pref
ix="c"%>
<%@ t
agl
i
bur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
functi
ons"pr
efix="
fn"%>
<ht
ml >
<head>
<tit
le>Usi
ngJSTLFunct
ions</
ti
tl
e>
</head>
<body >
<c:setv ar=" stri
ng1"v alue=" Thisisf i
rstStr
ing."
/>
<c:setv ar=" stri
ng2"v alue=" ${fn:
subst ri
ngBefore(
str
ing1,'
fi
rst'
)}
"/>
<p>bef ore: ${string2}</ p>
after: ${fn:
subst ringAfter (st
ri
ng1, '
fi
rst'
)}<br
>
Upper Case: ${fn:toUpper Case(str
ing1)}<br>
Lower Case: ${fn:toLower Case(stri
ng1) }
<br>
length : ${fn:
lengt h(string1)}<br>
escapeXml :${fn:escapeXml (
"Thisis<abc>exampl e</abc>"
)}<br
>
wihoutescapeXml :${"Thi si
s<abc>exampl e</abc>"
}<br>
tri
m: ${fn:t
rim(" Thi sis JSTL " )
}<br>
split:${fn:spli
t("ThisisJSTL" ,"")
}
</body>
</
html>
For
mat
ti
ngTags
TheJSTLfor
mattingtagsareusedt
of ormatanddispl
ayt
ext,t
hedat
e,t
heti
me,and
number
sforint
ernati
onal
izedWebsi
tes.Fol
lowi
ngisthesy
ntaxtoi
ncl
udeFor
matti
ng
l
ibr
aryi
nyourJSP−
<%@ t
agl
i
bpr
efi
x="
fmt
"ur
i="
htt
p:/
/jav
a.sun.
com/
jsp/
jst
l/
fmt
"%>
For
mat
ti
ngTags Descr
ipt
ions
f
mt:
par
seNumbe Iti
susedt oParsesthe At
tri
but
e Descr
ipt
ion Requi
re Def
aul
r str
ingr epresent
ati
onofa d t
currency,percent
ageor
number . Numer
icval
uetor
ead Body
Val
ue No
(
parse)
t
ype NUMBER,CURRENCY,
or numbe
No
PERCENT r
par
seLoca Local
etousewhen Defaul
le parsi
ngt
henumber No t
local
e
Whet hertoparsetoan
i
ntegerOnl
i
nteger(t
rue)orfl
oating- No f
alse
y
pointnumber( f
alse)
pat
ter
n Cust
om par
singpat
ter
n No None
t
imeZone Timezoneofthe Defaul
displ
ayeddat
e No ttime
zone
Nameofthev ar
iabl
eto Pri
nt
v
ar stor
etheparsednumber No to
page
Scopeofthevari
ablet
o
scope st
oret
hef or
matted No page
number
fmt:
for
mat
Numb Iti
susedt oformatt
he At
tri
but
e Descr
ipt
ion Requi
re Def
aul
t
er numericalval
uewith d
speci
ficfor
mator
preci
sion. Numer
icv
aluet
o
Val
ue Yes None
di
spl
ay
t
ype NUMBER, Numbe
CURRENCY,or No r
PERCENT
Speci
fyacustom
pat
ter
n f
ormatt
ingpatt
ern No None
fort
heoutput.
Curr
encycode(f
or Fr
om
type="cur
rency
") t
he
cur
rency
Code No
defaul
t
locale
Fr
om
Cur
rencysymbol
t
he
cur
rency
Symbol (f
ortype= No
defaul
t
"
currency
")
locale
Whethertogroup
gr
oupi
ngUsed numbers(TRUEor No t
rue
FALSE)
Maxi
mum number
maxI
nteger
Digi
t
ofi
ntegerdi
git
sto No None
s
pri
nt
Mini
mum number
mi
nInt
eger
Digi
ts ofi
ntegerdi
git
sto No None
pri
nt
Maximum number
maxFr
acti
onDi
gi
off
racti
onaldi
git
s No None
ts
topri
nt
Mini
mum number
mi
nFr
acti
onDi
git
offr
acti
onaldi
git
s No None
s
topri
nt
Nameoft he
v
ariabletostore Pri
ntt
o
v
ar No
t
hef ormatted page
number
Scopeoft he
v
ariabletostor
e
scope No page
t
hef ormatt
ed
number
f
mt:
par
seDat
e I
tparsesthest
ri
ng At
tri
but
e Descr
ipt
ion Requi
re Def
aul
repr
esent
ati
onofati
me d t
anddate.
Val
ue Dat
eval
uet
oread(
par
se) No Body
t
ype DATE,
TIME,
orBOTH No dat
e
FULL,LONG,MEDIUM, Def
aul
dat
eSt
yle No
SHORT,orDEFAULT t
FULL,LONG,MEDIUM, Def
aul
t
imeSt
yle No
SHORT,orDEFAULT t
Localetousewhen Defaul
par
seLoca
parsingt
hedate No t
le
local
e
pat
ter
n Cust
om par
singpat
ter
n No None
Ti
mezoneofthepar
sed Defaul
t
imeZone date No ttime
zone
Nameofthev
ari
abl
eto Pri
nt
v
ar st
oret
hepar
seddat
e No to
page
Scopeofthev
ari
abl
eto
scope No page
st
orethef
ormatt
eddat
e
f
mt:
for
mat
Dat
e I
tformatsthet
imeand/or At
tri
but
e Descr
ipt
ion Requi
re Def
aul
dat
eusingthesuppli
ed d t
pat
ternandsty
les.
Val
ue Dat
eval
uet
odi
spl
ay Yes None
t
ype DATE,
TIME,
orBOTH No dat
e
dat
eSt
yl FULL,LONG,MEDIUM,
No def
aul
t
e SHORT,orDEFAULT
t
imeSt
yl FULL,LONG,MEDIUM,
No def
aul
t
e SHORT,orDEFAULT
pat
ter
n Cust
om f
ormat
ti
ngpat
ter
n No None
Ti
mezoneoft
hedi
spl
ayed Defaul
t
t
imeZon
dat
e No ti
me
e
zone
v
ar Nameoft
hevar
iabl
eto No Pri
ntt
o
st
oret
hef
ormat
teddat
e page
Scopeofthev
ari
abl
eto
scope No page
st
orethef
ormatt
eddat
e
f
mt:
bundl
e I
tisusedf orcr
eati
ngthe Attr
ibut
e Descr
ipt
ion Requi
re Def
aul
ResourceBundl
eobjects d t
whichwillbeusedbytheir
tagbody. Speci
fi
esthebasenameof
basena
t
her
esourcebundlet
hati
s Yes None
me
tobeloaded.
Val
uetoprependt
oeach
Pr
efi
x keynamein No None
<fmt:
message>subt
ags
f
mt:
set
Bundl
e Itl
oadstheresource At
tri
but
e Descr
ipt
ion Requi
re Def
aul
bundleandstoresiti
na d t
bundleconf
igurat
ion
vari
ableort
henamed Basenameoft heresource
scopedvari
able. basena bundl
efamil
ytoexposeas
Yes None
me ascopedoraconfigurat
ion
var
iable
Nameofthev
ari
abl
eto Repl
ac
v
ar st
oret
henewbundl
e No e
def
ault
Scopeofthev
ari
abl
eto
scope No Page
stor
ethenewbundl
e
f
mt:
ti
meZone I
tspecifi
esaparsi
ngact
ion At
tri
but
e Descri
pti
on Requi
re Def
aul
nestedinit
sbodyort
he d t
ti
mezonef oranyt
ime
for
mat t
ing. Ti
mezonetoapplyt
othe
Val
ue Yes None
body
f
mt:
set
TimeZon Itst
orestheti
mezone At
tri
but
e Descr
ipt
ion Requi
re Def
aul
e i
nsideat i
mezone d t
confi
gurati
onvar
iabl
e.
Timezonetoexposeasa
Val
ue scopedorconf
igur
ati
on Yes None
var
iabl
e
Nameofthev
ari
abl
eto Repl
ac
v
ar st
oret
henewti
mezone No e
def
ault
Scopeofthev
ari
abl
eto
scope No Page
st
orethenewti
mezone
f
mt:
message I
tdisplayan At
tri
but
e Descr
ipt
ion Requi
re Def
aul
i
nternati
onal
izedmessage. d t
key Messagekeyt
oret
ri
eve No Body
bundl
e Resour
cebundl
etouse Defaul
No t
bundle
Nameofthevari
ableto Pri
nt
v
ar st
oret
helocal
ized No to
message page
Scopeofthevari
ableto
scope stor
ethelocal
ized No Page
message
Exampl
e
<%@ t
agl
i
bpr
efi
x="
c"uri
="ht
tp:
//j
ava.sun.com/jsp/j
stl
/core"
%>
<%@ t
agl
i
bpr
efi
x="
fmt"uri
="ht
tp:
//j
ava.sun.com/jsp/j
stl
/fmt"
%>
<html>
<head>
<ti
tl
e>fmt:
for
mat
NumberTag</
tit
le>
</head>
<body>
<h3>Formatti
ngofNumber :
</h3>
<c:
setvar="
Amount"value="9850.
14115"/>
<p>FormattedNumber-1:
<fmt:
formatNumbervalue="${Amount
}"ty
pe="curr
ency"/></
p>
<p>Formatt
edNumber -
2:
<fmt:
formatNumbertype="number"gr
oupingUsed="t
rue"val
ue="
${Amount
}"/
></
p>
<p>Formatt
edNumber -
3:
<fmt:f
ormatNumbert
ype="number
"maxIntegerDigi
ts="3"value="${Amount}
"/></p>
<p>Formatt
edNumber-4:
<fmt:f
ormatNumbert
ype="number
"maxFractionDigi
ts="6"value="${Amount}
"/></p>
<p>Formatt
edNumber-5:
<fmt:f
ormatNumbert
ype="per
cent
"maxIntegerDi
gits="4"value="${Amount}
"/></p>
<p>Formatt
edNumber-6:
<fmt:f
ormatNumbert
ype="number
"pat
tern="###.###$"v alue="${Amount}
"/></p>
</body>
</html>
Out
put
:
For
mat
ti
ngofNumber:
For
mat
tedNumber
-1:
$9,850.
14
For
mat
tedNumber
-2:
9,850.
141
For
mat
tedNumber
-3:
850.141
For
mat
tedNumber
-4:
9,850.
14115
For
mat
tedNumber
-5:
5,014%
For
mat
tedNumber
-6:
9850.141$
Exampl
e
<%@ t
agl
i
bpr
efi
x="
c"uri
="ht
tp:
//j
ava.sun.com/jsp/j
stl
/core"%>
<%@ t
agl
i
bpr
efi
x="
fmt"uri
="ht
tp:
//j
ava.sun.com/jsp/j
stl
/fmt"%>
<html >
<head>
<tit
le>fmt:
parseDateTag</
tit
le>
</head>
<body >
<h3>Par sedDate:
</h3>
<c:setvar="dat
e"val
ue="12-
08-2016"/
>
<fmt:
parseDateval
ue="${
date}"var="
parsedDat
e"pat
ter
n="
dd-
MM-
yyy
y"/
>
<p><c:
outvalue="
${par
sedDate}"/></p>
</
body
>
</
html
>
Out
put
:
Par
sedDat
e:
Fr
iAug1600:
00:
00I
ST2019
Exampl
eof<f
mt:
for
mat
Dat
e>t
ag:
<%@ t agli
bpr efi
x="c"uri
="http:
//java.sun.com/ j
sp/j
stl
/core"%>
<%@ t agli
bpr efi
x="fmt"uri
="http://j
ava.sun.com/ j
sp/j
stl
/fmt "
%>
<html >
<head>
<ti
tle>fmt:formatDate</ti
tl
e>
</head>
<body >
<h2>Di f
ferentFormat softheDat e</h2>
<c:setvar="Date"value="<%=newj ava.uti
l
.Date()%>"/>
<p>
Format tedTime:
<fmt :
format Datetype="ti
me"v alue="${Dat e}
"/>
</p>
<p>
Format tedDat e:
<fmt :
format Datetype="date"value="${Dat e}
"/>
</p>
<p>
Format tedDat eandTi me:
<fmt :
format Datetype="both"value="${Dat e}
"/ >
</p>
<p>
Format tedDat eandTi meinshor tstyl
e:
<fmt :
format Datetype="both"dateSt yl
e="short"ti
meStyle="short
"
value="${Date}"/>
</p>
<p>
Format tedDat eandTi meinmedi um st yl
e:
<fmt :
format Datetype="both"dateSt yl
e="medi um"timeStyle="medi
um"
value="${Date}"/>
</p>
<p>
Format tedDat eandTi meinl ongst yl
e:
<fmt :
format Datetype="both"dateSt yl
e="long"timeSty
le="l
ong"
value="${Date}"/>
</p>
</
body
>
</
html
>
Out
put
:
Dif
fer
entFor
matsoft heDate
Formatt
edTi
me: 4:20:50PM
Formatt
edDate:Aug16, 2019
Formatt
edDateandTi me:Aug16,
20194:
20:
50PM
For
mat
tedDat
eandTi
mei
nshortst
yle:8/
16/194:20PM
For
mat
tedDat
eandTi
mei
nmedium sty
le:Aug16,20194:20:50PM
For
mat
tedDat
eandTi
mei
nlongsty
le:August16,
20194:20:50PM IST