Action and Directives
Action and Directives
** JSP–Di
rect
ives *
**
Thesedi
rect
ivesprov
idedir
ect
ionsandinstr
uct
ionst
othecont
ainer
,tel
l
ingi
thow t
o
handl
ecert
ainaspect
softheJSPprocessi
ng.
AJSPdir
ecti
veaf
fect
stheov
eral
lst
ruct
ureoft
heser
vletcl
ass.I
tusual
l
yhast
he
f
oll
owi
ngfor
m−
<%@ di
rect
iveat
tri
but
e="
val
ue"%>
Dir
ect
ivescanhav
eanumberofat
tri
but
eswhi
chy
oucanl
i
stdownaskey
-val
uepai
rs
andseparat
edbywhi
tespace.
Theblanksbet
weenthe@ symbolandthedi
rect
ivename,andbet
weent
hel
ast
at
tri
but
eandthecl
osi
ng%>,
areopt
ional
.
Ther
ear
ethr
eet
ypesofdi
rect
ivet
ag−
S.
No. Di
rect
ive&Descr
ipt
ion
<%@ page.
..%>
1
Defi
nes page-
dependentatt
ri
butes,such as scr
ipt
ing l
anguage,er
ror
page,andbuff
eri
ngrequi
rement
s.
<%@ i
ncl
ude.
..%>
2
I
ncl
udesaf
il
edur
ingt
het
ransl
ati
onphase.
<%@ t
agl
ib.
..%>
3
Decl
aresat
agl
i
brar
y,cont
aini
ngcust
om act
ions,
usedi
nthepage
JSP-ThepageDi
rect
ive
Thepagedirect
iveisusedtoprovidei
nst
ruct
ionstot
hecontai
ner.Thesei
nst
ruct
ions
pert
aintothecurrentJSPpage.Youmaycodepagedi r
ect
ivesanywherei
nyourJSP
page.Byconvent
ion,pagedi
rect
ivesar
ecodedatthet
opoftheJSPpage.
Fol
l
owi
ngi
sthebasi
csy
ntaxoft
hepagedi
rect
ive−
<%@ pageat
tri
but
e="
val
ue"%>
Youcanwr
it
etheXMLequi
val
entoft
heabov
esy
ntaxasf
oll
ows−
<j
sp:
dir
ect
ive.
pageat
tri
but
e="
val
ue"/
>
At
tri
but
es
Fol
l
owi
ngt
abl
eli
stsoutt
heat
tri
but
esassoci
atedwi
tht
hepagedi
rect
ive−
S.
No. At
tri
but
e&Pur
pose
1 buf
fer
Speci
fi
esabuf
fer
ingmodel
fort
heout
putst
ream.
aut
oFl
ush
2
Cont
rol
sthebehav
ioroft
heser
vletout
putbuf
fer
.
cont
ent
Type
3
Def
inest
hechar
act
erencodi
ngscheme.
er
ror
Page
4 Defi
nestheURLofanot
herJSPt
hatr
epor
tsonJav
auncheckedr
unt
ime
except
ions.
i
sEr
ror
Page
5 Indicat
esifthisJSP pagei
saURLspeci
fi
edbyanot
herJSP page'
s
errorPageat
tri
but
e.
ext
ends
6
Speci
fi
esasuper
classt
hatt
hegener
atedser
vletmustext
end.
i
mpor
t
7 Speci
fi
esal i
stofpackagesorclassesforusei
ntheJSPast
heJav
a
i
mportstat
ementdoesforJav
aclasses.
i
nfo
8 Def
ines a st ri
ng t hat can be accessed wi
th t
he
ser
vlet
'sget
Ser
vlet
Inf
o()met
hod.
9 i
sThr
eadSaf
e
Def
inest
het
hreadi
ngmodel
fort
hegener
atedser
vlet
.
l
anguage
10
Def
inest
hepr
ogr
ammi
ngl
anguageusedi
ntheJSPpage.
sessi
on
11
Speci
fi
eswhet
herornott
heJSPpagepar
ti
cipat
esi
nHTTPsessi
ons
i
sELI
gnor
ed
12 Speci
fi
eswhet
herornott
heELexpr
essi
onwi
thi
ntheJSPpagewi
l
lbe
i
gnored.
i
sScr
ipt
ingEnabl
ed
13
Det
ermi
nesi
fthescr
ipt
ingel
ement
sar
eal
l
owedf
oruse.
Thebuf
ferAt
tri
but
e
Thebuf f
erat
tri
bute speci
fi
es t
he buf
fer
ing char
act
eri
sti
cs f
ort
he ser
verout
put
responseobj
ect.
Youmaycodeav alueof"none"tospeci
fynobuffer
ingsothatt
heserv
letoutputis
i
mmedi at
elydir
ect
edtotheresponseobj
ectoryoumaycodeamaximum buff
ersizein
ki
lobytes,whichdi
rect
stheservl
ettowri
tetothebuff
erbef
orewri
ti
ngt
other esponse
object
.
Todir
ectt
heser
vlett
owr
it
etheout
putdi
rect
lyt
other
esponseout
putobj
ect
,uset
he
fol
l
owing−
<%@ pagebuf
fer="
none"%>
Usethefol
l
owingtodi
rectt
heser
vlett
owr
it
etheout
putt
oabuf
ferofsi
zenotl
ess
than8ki
l
obytes−
<%@ pagebuf
fer="
8kb"%>
Theaut
oFl
ushAt
tri
but
e
TheautoFlushatt
ri
bute speci
fi
es whether buf
fer
ed output shoul
d be fl
ushed
aut
omat i
call
ywhent hebufferi
sfi
ll
ed,orwhetheranexcepti
onshouldber
aisedto
i
ndicat
ethebufferover
fl
ow.
Avalueoftr
ue(def
aul
t)i
ndi
cat
esaut
omat
icbuf
ferf
lushi
ngandav
alueoff
alset
hrows
anexcept
ion.
Thefol
lowi
ngdirect
ivecausest
heser
vlett
othr
ow anexcept
ionwhent
heser
vlet
's
out
putbuff
eri
sfull−
<%@ pageaut
oFl
ush="
fal
se"%>
Thi
sdi
rect
ivecausest
heser
vlett
ofl
usht
heout
putbuf
ferwhenf
ull
−
<%@ pageaut
oFl
ush="
tr
ue"%>
Usual
l
y,t
hebuf
ferandt
heaut
oFl
ushat
tri
but
esar
ecodedonasi
ngl
epagedi
rect
iveas
fol
l
ows−
<%@ pagebuf
fer="
16kb"aut
ofl
ush="
tr
ue"%>
Thecont
ent
TypeAt
tri
but
e
Thecont entTypeatt
ri
buteset
sthecharact
erencodi
ngf ortheJSPpageandforthe
generatedresponsepage.Thedef
aul
tcontentt
ypeistext/
html,whi
chi
sthest
andard
contenttypeforHTMLpages.
I
fyouwantt
owr
it
eoutXMLf
rom y
ourJSP,
uset
hef
oll
owi
ngpagedi
rect
ive−
<%@ pagecont
ent
Type="
text
/xml
"%>
Thef
oll
owi
ngst
atementdi
rect
sthebr
owsert
orendert
hegener
atedpageasHTML−
<%@ pagecont
ent
Type="
text
/ht
ml"%>
Thef
oll
owi
ngdi
rect
iveset
sthecont
entt
ypeasaMi
crosof
tWor
ddocument−
<%@ pagecont
ent
Type="
appl
i
cat
ion/
mswor
d"%>
Youcanalsospecif
yt hecharacterencodingfortheresponse.Forexampl
e,i
fyou
wantedt
ospeci
fythattheresul
ti
ngpaget hati
sretur
nedtothebrowserusesI
SOLati
n
1,y
oucanusethefoll
owingpagedi r
ect
ive−
<%@ pagecont
ent
Type="
text
/ht
ml:
char
set
=ISO-
8859-
1"%>
Theer
ror
PageAt
tri
but
e
Theerror
Pageatt
ri
butetell
stheJSPengi
newhichpagetodispl
ayi
fthereisaner
ror
whil
ethecurr
entpageruns.Thev
alueoft
heer
rorPageat
tr
ibut
eisar
elat
iveURL.
Thefol
l
owi
ngdi
rect
ivedi
spl
aysMy
Err
orPage.
jspwhenal
luncaughtexcept
ionsar
e
thr
own−
<%@ pageer
ror
Page="
MyEr
ror
Page.
jsp"%>
Thei
sEr
ror
PageAt
tri
but
e
TheisEr
rorPageat
tri
but
eindi
cat
est
hatt
hecur
rentJSPcanbeusedast
heer
rorpage
foranot
herJSP.
Thev
alueofi
sEr
ror
Pagei
sei
thert
rueorf
alse.Thedef
aul
tval
ueoft
hei
sEr
ror
Page
at
tri
but
eisf
alse.
Forexample,t
hehandleEr
ror
.j
spset
sthei
sEr
ror
Pageopt
iont
otr
uebecausei
tis
supposedt
ohandl
eerrors−
<%@ pagei
sEr
ror
Page="
tr
ue"%>
Theext
endsAt
tri
but
e
Theext
endsat
tri
but
especi
fi
esasuper
classt
hatt
hegener
atedser
vletmustext
end.
Forexample,t
hefoll
owingdi
rect
ivedi
rect
st heJSPtransl
atort
ogener
atet
heser
vlet
suchthatt
heserv
l endssomePackage.
etext SomeClass−
<%@ pageext
ends="
somePackage.
SomeCl
ass"%>
Thei
mpor
tAt
tri
but
e
Theimpor
tat
tri
buteserv
esthesamef unct
ionas,andbehav
esli
ke,theJavai
mport
st
atement
.Thev al
ueforthei
mportopti
oni st
henameoft hepackageyouwantt
o
i
mport.
Toi
mpor
tjav
a.sql
.*
,uset
hef
oll
owi
ngpagedi
rect
ive−
<%@ pagei
mpor
t="
jav
a.sql
.*
"%>
Toi
mpor
tmul
ti
plepackages,
youcanspeci
fyt
hem separ
atedbycommaasf
oll
ows−
<%@ pagei
mpor
t="
jav
a.sql
.*
,j
ava.
uti
l
.*"%>
By default
, a cont ai
ner automatical
l
y i
mpor
tsj
ava.
lang.
*, j
avax.
ser
vlet
.*,
j
avax.
ser
vlet
.j
sp.
*,andj
avax.
ser
vlet
.ht
tp.
*.
Thei
nfoAt
tr
ibut
e
Theinf
oattr
ibut
elet
syoupr
ovi
deadescr
ipt
ionoft
heJSP.Thef
oll
owi
ngi
sacodi
ng
example−
<%@ pagei
nfo="
Thi
sJSPPageWr
it
tenByZARA"%>
Thei
sThr
eadSaf
eAt
tri
but
e
Thel
anguageAt
tri
but
e
Thelanguageat
tri
but
eindi
cat
est
hepr
ogr
ammi
ngl
anguageusedi
nscr
ipt
ingt
heJSP
page.
Forexample,becauseyouusual
l
yuseJav
aast
hescr
ipt
ingl
anguage,y
ourl
anguage
opt
ionlooksli
kethis−
<%@ pagel
anguage="
jav
a"%>
Thesessi
onAt
tri
but
e
Thesessi
onattr
ibut
ei ndi
cateswhet
herornott heJSPpageusesHTTPsessions.A
val
ueoft r
uemeanst hatt heJSPpagehasaccesst oabuil
ti
nsessi
onobj
ectanda
val
ueoffalsemeansthattheJSPpagecannotaccesst
hebui
lt
insessi
onobj
ect
.
Fol
l
owi ng di
rect
iveal
l
owst heJSP pageto useanyofthebui
lt
inobjectsessi
on
methodssuchassession.
getCr
eat
ionTi
me(
)orsessi
on.
get
Last
AccessTi
me()−
<%@ pagesessi
on="
tr
ue"%>
Thei
sELI
gnor
edAt
tri
but
e
TheisELI
gnoredattr
ibutegi
vesyout
heabil
i
tytodisabl
etheev
aluat
ionofExpr
essi
on
Language(EL)expr
essionswhi
chhasbeeni
ntr
oducedinJSP2.
0.
Thedefaul
tvalueoftheatt
ri
buteistr
ue,meaningthatexpr
essi
ons,${
..
.}
,ar
eevaluat
ed
asdict
atedbytheJSPspecifi
cati
on.I
ftheatt
ribut
eissettofal
se,
thenexpr
essi
onsare
noteval
uatedbutrat
hertr
eatedasstati
ctext
.
Fol
l
owi
ngdi
rect
iveset
sanexpr
essi
onnott
obeev
aluat
ed−
<%@ pagei
sELI
gnor
ed="
fal
se"%>
Thei
sScr
ipt
ingEnabl
edAt
tri
but
e
TheisScr
ipt
ingEnabl
edat
tri
but
edet
ermi
nesi
fthescr
ipt
ingel
ement
sar
eal
l
owedf
or
use.
Thedefaul
tv alue ( t
rue)enables scriptl
ets,expressi
ons,and decl
arat
ions.I
fthe
att
ri
but
e'svaluei ssettof alse,atranslat
ion-
ti
meer r
orwil
lberai
sediftheJSPuses
anyscr
iptl
ets,expressi
ons( non-EL)
,ordeclarat
ions.
Theatt
ri
but
e’sval
uecanbesett ofal
sei
fyouwantt
orest
ri
ctt
heusageofscr
ipt
let
s,
expr
essi
ons(non-
EL)
,ordecl
arat
ions−
<%@ pagei
sScr
ipt
ingEnabl
ed="
fal
se"%>
Thei
ncl
udeDi
rect
ive
Theincludedir
ect
iveis used t
oi ncl
ude a f
il
e duri
ng the tr
anslat
ion phase.This
dir
ecti
vet el
l
sthecont
ainertomergethecont
entofotherexter
nalfi
leswit
ht hecur
rent
JSPdur ingthetr
ansl
ationphase.Youmaycodet heincludedir
ecti
vesany wher
ei n
yourJSPpage.
Thegener
alusagef
orm oft
hisdi
rect
ivei
sasf
oll
ows−
<%@ i
ncl
udef
il
e="
rel
ati
veur
l">
Thef i
lenamei ntheincl
udedir
ectiv
ei sact
ual
l
yarel
ati
veURL.Ifyoujustspeci
fya
fi
lenamewi thnoassociatedpath,theJSPcompi
l
erassumesthatthefil
eisinthe
samedi rect
oryasyourJSP.
Youcanwr
it
etheXMLequi
val
entoft
heabov
esy
ntaxasf
oll
ows−
<j
sp:
dir
ect
ive.
incl
udef
il
e="
rel
ati
veur
l"/
>
Exampl
e:-
Fi
lename:I
ncl
ude_
dir
ect
ive.
jsp
<%@pagecont
ent
Type="
text
/ht
ml"pageEncodi
ng="
UTF-
8"%>
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<met
aht
tp-
equi
v="
Cont
ent
-Ty
pe"cont
ent
="t
ext
/ht
ml;
char
set
=UTF-
8">
<t
it
le>JSPPage</
ti
tl
e>
</
head>
<body
>
<h1>Hel
l
oWor
ld!
</h1>
Today
'sdat
e:
<%@i
ncl
udef
il
e="
next
.j
sp"%>
</
body
>
</
html
>
Fi
lename:next
.j
sp
<%@pagecont
ent
Type="
text
/ht
ml"pageEncodi
ng="
UTF-
8"i
mpor
t="
jav
a.ut
il
.Dat
e"%>
<!
DOCTYPEht
ml>
<ht
ml>
<head>
<met
aht
tp-
equi
v="
Cont
ent
-Ty
pe"cont
ent
="t
ext
/ht
ml;
char
set
=UTF-
8">
<t
it
le>JSPPage</
ti
tl
e>
</
head>
<body
>
<%
Dat
ed=newDat
e()
;
out
.pr
int
ln(
d);
%>
</
body
>
</
html
>
Thet
agl
i
bDi
rect
ive
TheJavaSer
verPagesAPIal
lowy
outodef
inecust
om JSPtagsthatl
ookli
keHTMLor
XMLt agsandat aglibr
aryisasetofuser-
def
inedtagsthatimplementcust
om
behav
ior.
Thetagl
ibdi
rect
ivedecl
arest
hatyourJSPpageusesasetofcust
om tags,i
dent
if
ies
thel
ocati
onoftheli
brar
y,andprov
idesmeansfori
dent
if
yingt
hecust
om tagsinyour
JSPpage.
Thet
agl
i
bdi
rect
ivef
oll
owst
hesy
ntaxgi
venbel
ow−
<%@ t
agl
i
bur
i="
uri
"pr
efi
x="
pref
ixOf
Tag">
Her
e,theur
iattr
ibuteval
ueresolv
est oal ocat
ion t
hecont ai
nerunderst
andsand
t
heprefi
xat
tri
buteinf
ormsacontai
nerwhatbit
sofmar kupar
ecustom act
ions.
Youcanwr
it
etheXMLequi
val
entoft
heabov
esy
ntaxasf
oll
ows−
<j
sp:
dir
ect
ive.
tagl
i
bur
i="
uri
"pr
efi
x="
pref
ixOf
Tag"/
>
Exampl
e:-
<%@pagecontent
Type="text
/ht
ml "pageEncodi
ng="UTF-
8"%>
<%@ t
agl
iburi="htt
p:/
/java.
sun.com/ j
sp/j
stl
/cor
e"pref
ix="c"%>
<ht
ml >
<head>
<tit
le>c:
outTagExampl
e</
ti
tl
e>
</head>
<body>
<c:
outv
alue="
${2*
2000}
"></
c:out
>
</body
>
</
html>
*
** JSP-Act
ion *
**
Theseacti
onsuseconst ruct
si nXMLsyntaxtocontrolt
hebehavi
oroftheserv
let
engi
ne.Youcandy namicall
yinser
tafi
l
e,reuseJavaBeanscomponent
s,f
orwar
dt he
usert
oanotherpage,orgenerat
eHTMLfortheJav
aplugin.
Ther
eisonl
yonesy
ntaxf
ort
heAct
ionel
ement
,asi
tconf
ormst
otheXMLst
andar
d−
<j
sp:
act
ion_
nameat
tri
but
e="
val
ue"/
>
Acti
onelement
sar ebasi
cal
l
ypr
edef
inedf
unct
ions.Thef
oll
owi
ngt
abl
eli
stsoutt
he
avai
labl
eJSPacti
ons−
S.
No. Sy
ntax&Pur
pose
1 j
sp:
incl
ude
I
ncl
udesaf
il
eatt
het
imet
hepagei
srequest
ed.
j
sp:
useBean
2
Fi
ndsori
nst
ant
iat
esaJav
aBean.
j
sp:
set
Proper
ty
3
Set
sthepr
oper
tyofaJav
aBean.
j
sp:
get
Proper
ty
4
I
nser
tst
hepr
oper
tyofaJav
aBeani
ntot
heout
put
.
j
sp:
for
war
d
5
For
war
dst
her
equest
ert
oanewpage.
j
sp:
plugi
n
6 Generat
esbrowser-
speci
fi
ccodet
hatmakesanOBJECTorEMBEDt
ag
fort
heJavaplugi
n.
j
sp:
element
7
Def
inesXMLel
ement
sdy
nami
cal
l
y.
j
sp:
att
ri
but
e
8
Def
inesdy
nami
cal
l
y-def
inedXMLel
ement
'
sat
tri
but
e.
j
sp:
body
9
Def
inesdy
nami
cal
l
y-def
inedXMLel
ement
'
sbody
.
j
sp:
text
10
Usedt
owr
it
etempl
atet
exti
nJSPpagesanddocument
s.
CommonAt
tri
but
es
Therearetwoatt
ri
but
est
hatar
ecommont
oal
lAct
ionel
ement
s:t
hei
dat
tr
ibut
eand
thescopeatt
ri
but
e.
I
dat
tr
ibut
e
Thei datt
ri
buteuniquel
yident
if
iestheActi
onelement,andal l
owstheact
iontobe
ref
erencedinsi
detheJSPpage.I ft
heActi
oncreatesaninstanceofanobj
ect
,thei
d
val
uecanbeusedt orefer
encei
tthrought
heimpli
citobj
ectPageCont
ext
.
Scopeat
tri
but
e
Thisattr
ibut
ei denti
fiestheli
fecycleoftheActi
onelement
.Theidat t
ribut
eandt he
scopeattri
butearedi rect
lyr
elated,asthescopeatt
ri
but
edetermi
nest heli
fespanof
theobjectassociatedwi tht
hei d.Thescopeat t
ri
butehasfourpossi
bl eval
ues:(
a)
page,(
b)request,(c)sessi
on,and(d)appli
cat
ion.
The<j
sp:
i
ncl
ude>Act
ion
Thisact
ionl
etsy
oui
nser
tfi
l
esi
ntot
hepagebei
nggener
ated.Thesy
ntaxl
ooksl
i
ke
thi
s−
<j
sp:
i
ncl
udepage="
rel
ati
veURL"f
lush="
tr
ue"/
>
Unl
iket
heincl
udedir
ecti
ve,whichi
nsert
sthef
il
eatthet
imetheJSPpageist
ransl
ated
i
ntoaserv
let
,thi
sact
ioninser
tsthefi
l
eatthet
imethepagei
srequest
ed.
Fol
l
owi
ngt
abl
eli
stsoutt
heat
tri
but
esassoci
atedwi
tht
hei
ncl
udeact
ion−
S.
No. At
tri
but
e&Descr
ipt
ion
1 page
Ther
elat
iveURLoft
hepaget
obei
ncl
uded.
f
lush
2 Thebooleanatt
ri
butedeter
mineswhet
hert
hei
ncl
udedr
esour
cehasi
ts
buf
ferf
lushedbefor
eiti
sincl
uded.
Exampl
e
Letusdef
inet
hef
oll
owi
ngt
wof
il
es(
a)dat
e.j
spand(
b)mai
n.j
spasf
oll
ows−
Fol
l
owi
ngi
sthecont
entoft
hedat
e.j
spf
il
e−
<p>Today
'sdat
e:<%=(
newj
ava.
uti
l
.Dat
e()
).
toLocal
eSt
ri
ng(
)%></
p>
Fol
l
owi
ngi
sthecont
entoft
hemai
n.j
spf
il
e−
<ht
ml >
<head>
<tit
le>Thei
ncl
udeAct
ionExampl
e</
ti
tl
e>
</head>
<body>
<center>
<h2>Theincl
udeact
ionExample</h2>
<j
sp:i
ncludepage="dat
e.j
sp"f
lush="true"/
>
</center
>
</body>
</
html>
Letusnowkeepallt
hesefi
lesi
nther
ootdi
rect
oryandt
ryt
oaccessmai
n.j
sp.Youwi
l
l
recei
vet
hefol
lowi
ngoutput−
Thei
ncl
udeact
ionExampl
e
Today
'sdat
e:12-
Sep-
201014:
54:
22
The<j
sp:
useBean>Act
ion
TheuseBeanacti
onisquitever
sat
il
e.I
tfi
rstsear
chesf oranexist
ingobj
ectuti
li
zi
ng
theidandscopevar
iabl
es.I
fanobj
ecti
snotfound,i
tthentri
estocreat
ethespeci
fi
ed
object
.
Thesi
mpl
estwayt
oloadabeani
sasf
oll
ows−
<j
sp:
useBeani
d="
name"cl
ass="
package.
class"/
>
Onceabeancl
assisl
oaded,y
oucanusejsp:
set
Proper
tyandj
sp:
get
Proper
tyact
ions
t
omodif
yandret
ri
evet
hebeanproper
ti
es.
Fol
l
owi
ngt
abl
eli
stsoutt
heat
tri
but
esassoci
atedwi
tht
heuseBeanact
ion−
S.
No. At
tri
but
e&Descr
ipt
ion
1 cl
ass
Desi
gnat
est
hef
ull
packagenameoft
hebean.
t
ype
2
Speci
fi
est
het
ypeoft
hev
ari
abl
ethatwi
l
lref
ert
otheobj
ect
.
beanName
3 Gi
vesthenameoft hebeanasspeci
fi
edbyt
hei
nst
ant
iat
e()met
hodof
t
hejav
a.beans.
Beanscl
ass.
Letusnowdiscussthej
sp:set
Propert
yandt
hej
sp:
get
Proper
tyact
ionsbef
oregi
vi
nga
val
idexampl
erelat
edtotheseacti
ons.
The<j
sp:
set
Proper
ty>Act
ion
ThesetProper
tyact
ion set
st heproper
ti
esofaBean.TheBean musthavebeen
previ
ousl
ydefi
nedbeforethi
sacti
on.Therear
etwobasi
cway
stouset
hesetPr
opert
y
acti
on−
You can usej
sp:
set
Proper
tyaf
ter
,butout
side ofaj
sp:
useBeanel
ement
,asgi
ven
bel
ow−
<j
sp:
useBeani
d=" my
Name"..
./>
..
.
<j
sp:
setPr
oper
tyname="
my Name"pr
oper
ty="
somePr
oper
ty".
..
/>
I
nthiscase,thej
sp:
setPr
oper
tyi
sexecut
edr
egar
dlessofwhet
heranew beanwas
i
nst
anti
atedoranexi
sti
ngbeanwasf
ound.
A second cont
extin whi
ch j
sp:
set
Proper
ty can appeari
sinsi
de t
he body of
aj
sp:useBeanel
ement
,asgi
venbel
ow−
<j
sp:useBeanid="myName".
..>
..
.
<j
sp:setProper
tyname="myName"pr
oper
ty="
somePr
oper
ty".
..
/>
</
jsp:
useBean>
Here,t
hejsp:
set
Proper
tyi
sexecut
edonl
yifanew obj
ectwasi
nst
ant
iat
ed,noti
fan
exi
sti
ngonewasfound.
Fol
l
owi
ngt
abl
eli
stsoutt
heat
tri
but
esassoci
atedwi
tht
heset
Proper
tyact
ion−
S.
No. At
tri
but
e&Descr
ipt
ion
name
1
Desi
gnatest
hebeanthepr oper
tyofwhi
chwi
l
lbeset
.TheBeanmust
havebeenpr
evi
ousl
ydef
ined.
pr
oper
ty
2 I
ndicatesthepropert
yy ouwanttoset.A v
alueof"*
"meansthatal
l
requestparamet
erswhosenamesmat chbeanproper
tynameswil
lbe
passedtotheappropr
iat
eset
termet
hods.
v
alue
par
am
4 Theparam att
ri
but
eist henameoft her
equestpar
amet
erwhosevalue
thepr
opertyi
st or
eceive.Youcan'
tusebothval
ueandpar
am,butitis
permi
ssibl
etousenei
ther.
The<j
sp:
get
Proper
ty>Act
ion
ThegetProper
tyact
ionisusedtoretr
iev
et heval
ueofagi
venpr
oper
tyandconv
ert
sit
toastr
ing,andfi
nal
l
yinsert
siti
ntotheoutput.
Theget
Proper
tyact
ionhasonl
ytwoat
tri
but
es,bot
hofwhi
char
erequi
red.Thesy
ntax
oft
hegetPr
opert
yacti
onisasf
oll
ows−
<j
sp:
useBeani
d="myName"..
./>
..
.
<j
sp:
getPr
oper
tyname="myName"pr
oper
ty="
somePr
oper
ty".
..
/>
Fol
l
owi
ngt
abl
eli
stsoutt
her
equi
redat
tri
but
esassoci
atedwi
tht
heget
Proper
tyact
ion
−
S.
No. At
tri
but
e&Descr
ipt
ion
name
1
ThenameoftheBeanthathasapr
oper
tyt
ober
etr
iev
ed.TheBeanmust
havebeenpr
evi
ousl
ydefi
ned.
pr
oper
ty
2
Thepr
oper
tyat
tri
but
eist
henameoft
heBeanpr
oper
tyt
ober
etr
iev
ed.
Exampl
e
Letusdef
ineat
estbeant
hatwi
l
lfur
therbeusedi
nourexampl
e−
/*Fil
e:Test
Bean.
jav
a*/
packageacti
on;
publ
i
cclassTest
Bean{
pri
vat
eStri
ngmessage="
Nomessagespeci
fi
ed"
;
publ
icStri
nggetMessage(
){
ret
urn(message)
;
}
publ
icvoidset
Message(St
ringmessage){
thi
s.message=message;
}
}
Compiletheabovecodetothegenerat
edTest
Bean.classfi
leandmakesur ethatyou
copi
ed the Test
Bean.
class i
nC:\apache-tomcat-
7.0.2\
webapps\WEB-
INF\cl
asses\
acti
onfol
derand theCLASSPATHv ar
iableshould also besettothis
fol
der−
Now usethef ol
lowi
ngcodei
nmai
n.j
spf
il
e.Thi
sloadst
hebeanandset
s/get
sa
si
mpleStr
ingparameter−
<ht
ml>
<head>
<tit
le>Usi
ngJav
aBeansi
nJSP</
ti
tl
e>
</head>
<body>
<center
>
<h2>UsingJavaBeansinJSP</ h2>
<j
sp:useBeani
d=" t
est"cl
ass=" acti
on.Test
Bean"/>
<j
sp:setPr
oper
t yname=" test
"pr opert
y=" message"
value="Hell
oJSP. .
."/>
<p>Gotmessage.
..
.</
p>
<j
sp:getPr
oper
tyname="t
est
"pr
oper
ty="
message"/
>
</center>
</body>
</
html>
Letusnowt
ryt
oaccessmai
n.j
sp,
itwoul
ddi
spl
ayt
hef
oll
owi
ngr
esul
t−
Usi
ngJav
aBeansi
nJSP
Gotmessage.
..
.
Hel
loJSP..
.
The<j
sp:
for
war
d>Act
ion
Thefor
wardacti
onterminat
est
heact
ionofthecurr
entpageandfor
war
dstherequest
toanot
herr
esourcesuchasast
ati
cpage,anot
herJSPpage,oraJav
aServ
let
.
Fol
l
owi
ngi
sthesy
ntaxoft
hef
orwar
dact
ion−
<j
sp:
for
war
dpage="
Rel
ati
veURL"/
>
Fol
l
owi
ngt
abl
eli
stsoutt
her
equi
redat
tri
but
esassoci
atedwi
tht
hef
orwar
dact
ion−
S.
No. At
tri
but
e&Descr
ipt
ion
page
1
Shoul
dconsi
stofar el
ati
veURLofanotherr
esour
cesuchasast
ati
c
page,
anot
herJSPpage,
oraJavaSer
vlet
.
Exampl
e
Letusr
euset
hef
oll
owi
ngt
wof
il
es(
a)dat
e.j
spand(
b)mai
n.j
spasf
oll
ows−
Fol
l
owi
ngi
sthecont
entoft
hedat
e.j
spf
il
e−
<p>Today
'sdat
e:<%=(
newj
ava.
uti
l
.Dat
e()
).
toLocal
eSt
ri
ng(
)%></
p>
Fol
l
owi
ngi
sthecont
entoft
hemai
n.j
spf
il
e−
<ht
ml >
<head>
<tit
le>Thei
ncl
udeAct
ionExampl
e</
ti
tl
e>
</head>
<body>
<center>
<h2>Theincl
udeact
ionExampl
e</
h2>
<j
sp:for
wardpage="dat
e.j
sp"/
>
</center>
</body>
</
html>
Letusnow keepallt
hesefi
lesi
ntherootdi
rect
oryandt
ryt
oaccessmai
n.j
sp.Thi
s
woulddi
spl
ayresul
tsomethi
ngli
keasbel
ow.
Her
eitdiscar
dedt
hecont
entf
rom t
hemai
npageanddi
spl
ayedt
hecont
entf
rom
f
orwar
dedpageonl
y.
Today
'sdat
e:12-
Sep-
201014:
54:
22
The<j
sp:
plugi
n>Act
ion
Thepl
ugi
nacti
onisusedtoi
nser
tJavacomponentsi
ntoaJSPpage.I
tdet
ermi
nest
he
ty
peofbr
owserandinser
tst
he<obj
ect>or<embed>t
agsasneeded.
Ift
heneededplugi
nisnotpresent
,itdownl
oadst
hepluginandthenexecut
est
heJav
a
component.TheJavacomponentcanbeeit
heranAppletoraJavaBean.
Thepl
uginact
ionhassever
alatt
ri
but
esthatcor
respondt
ocommonHTMLt agsused
tofor
matJav a components.The<par
am>elementcan al
so be used t
o send
par
ameter
stotheAppl
etorBean.
Fol
l
owi
ngi
sthet
ypi
cal
synt
axofusi
ngt
hepl
ugi
nact
ion−
<j
sp:pl
ugi
ntype="applet"codebase="di
rname"code="
MyAppl
et.
class"
width="60"hei
ght="80">
<j
sp:par
am name=" fontcol
or"val
ue="red"/>
<j
sp:par
am name=" background"val
ue=" bl
ack"/
>
<j
sp:f
all
back>
Unabletoini
ti
ali
zeJav
aPl
ugi
n
</
jsp:
fal
lback>
</
jsp:
plugi
n>
Youcant
ryt
hisact
ionusi
ngsomeappl
eti
fyouar
eint
erest
ed.A new el
ement
,
the<f
all
back>el
ement,canbeusedt
ospeci
fyaner
rorst
ri
ngt
obesentt
otheuseri
n
casethecomponentf
ail
s.
The<j
sp:
element
>Act
ion
The<j
sp:
att
ri
but
e>Act
ion
The<j
sp:
body
>Act
ion
The<j
sp:el
ement>,<jsp:
att
ri
bute>and<jsp:
body
>act i
ons are used to def
ine XML
el
ementsdynamical
l
y.Thewor ddynamicall
yisi
mpor t
ant,becauseitmeanst hatthe
XMLelementscanbegeneratedatr
equestti
meratherthanstat
ical
lyatcompil
etime.
Fol
l
owi
ngi
sasi
mpl
eex
ampl
etodef
ineXMLel
ement
sdy
nami
cal
l
y−
<%@pagel anguage=" j
ava"cont
entType=" text
/html
"%>
<ht
ml xmlns=" ht
tp:
//www. w3c.
org/1999/xhtml"
xmlns:
jsp=" htt
p:/
/jav
a.sun.
com/ JSP/Page">
<head><t
it
le>Gener
ateXMLEl
ement
</t
it
le></
head>
<body>
<jsp:
elementname=" xmlEl
ement">
<j
sp:att
ri
butename="xmlElementAt
tr
">
Valuefortheatt
ri
but
e
</j
sp:at
tri
bute>
<j
sp:body>
Bodyf orXMLel
ement
</
jsp:body>
</jsp:
element
>
</body>
</
html>
Thi
swoul
dpr
oducet
hef
oll
owi
ngHTMLcodeatr
unt
ime−
<html xmlns = "
htt
p:/
/www.w3c.or
g/1999/
xht
ml" xml
ns:
j
sp =
"htt
p://
java.
sun.com/JSP/Page"
>
<head><ti
tl
e>Gener
ateXMLEl ement</
ti
tl
e></
head>
<body>
<xmlElementxml
Element
Att
r="
Val
uef
ort
heat
tr
ibut
e">
BodyforXMLel
ement
</xmlElement
>
</body>
</
html>
The<j
sp:
text
>Act
ion
The<jsp:
text
>acti
on can be used t
o wri
tethe templ
atet
exti
n JSP pages and
documents.Fol
l
owingist
hesimplesyntaxf
ort
hisacti
on−
<j
sp:
text
>Templ
atedat
a</
jsp:
text
>
Thebodyoft hetemplat
ecannotcont ainotherelements;i
tcanonlycontai
ntextand
ELexpr essi
ons( Note−ELexpr essi
onsar eexplainedi
nasubsequentchapter)
.Note
thatinXMLf i
les,youcannotuseexpr essi
onssuchas${ what
ever>0},becausethe
greaterthansignsareil
legal.Inst
ead,uset hegtform,suchas${whatevergt0}oran
alt
ernati
v ei
stoembedt hev alueinaCDATAsect ion.
<j
sp:
text
><!
[CDATA[
<br
>]]
></
jsp:
text
>
I
fyouneedtoi ncl
udeaDOCTYPEdeclar
ati
on,f
ori
nst
ancef
orXHTML,y
oumustal
so
uset
he<jsp:
text>elementasf
oll
ows−
<j
sp:t
ext><!
[CDATA[<!DOCTYPEhtml PUBLI
C"-
//W3C/
/DTDXHTML1.
0St
ri
ct/
/EN"
"DTD/xhtml1-
str
ict
.dtd"
>]]
></
jsp:
text>
<head><t
it
le>j
sp:
textact
ion</
ti
tl
e></
head>
<body>
<books><book><j
sp:
text
>
WelcometoJSPProgramming
</jsp:
text
></
book></
books>
</body>
</
html>
Tr
ytheabov
eexampl
ewi
thandwi
thout<j
sp:
text
>act
ion.