Vous êtes sur la page 1sur 131

WELCOME TO J2EE NOTES

SERVLETS
Web Components
Servlets
Java Server Pages (JSP)
Tags and Tag Libraries
Whats a Servlet?
Javas answer to CGI prograing
Progra r!ns on Web server and b!ilds pages on the "l#
When wo!ld #o! !se servlets?
$%ata &hanges "re'!entl# e(g( weather)reports
$Page !ses in"oration "ro databases e(g( on)line stores
$Page is based on !ser)s!bitted data e(g sear&h engines
$%ata &hanges "re'!entl# e(g( weather)reports
$Page !ses in"oration "ro databases e(g( on)line stores
$Page is based on !ser)s!bitted data e(g sear&h engines
Servlet Class Hierarchy

*ava+(servlet(Servlet
$%e"ines ethods that all servlets !st ipleent
init()
servi&e()
destro#()
*ava+(servlet(Generi&Servlet
$%e"ines a generi&, proto&ol)independent servlet
*ava+(servlet(http(-ttpServlet
$To write an -TTP servlet "or !se on the Web
doGet()
doPost()

*ava+(servlet(ServletCon"ig
$. servlet &on"ig!ration ob*e&t
$Passes in"oration to a servlet d!ring initiali/ation
Servlet(getServletCon"ig()
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8
WELCOME TO J2EE NOTES
*ava+(servlet(ServletConte+t
$To &o!ni&ate with the servlet &ontainer
$Contained within the ServletCon"ig ob*e&t
ServletCon"ig(getServletConte+t()
*ava+(servlet(Servlet5e'!est
$Provides &lient re'!est in"oration to a servlet
*ava+(servlet(Servlet5esponse
$Sending a response to the &lient

asic Servlet Str!ct!re

iport *ava(io(9:
iport *ava+(servlet(9:
iport *ava+(servlet(http(9:
p!bli& &lass -ello World e+tends -ttpServlet ;
// Handle get request
p!bli& void doGet(-ttpServlet5e'!est re'!est, -ttpServlet5esponse
response) throws Servlet6+&eption, I36+&eption ;
// request access incoming HTTP headers and HTML form data
// response - specify the HTTP response line and headers
// (e.g. specifying the content type, setting cooies!.
PrintWriter o!t < response(getWriter(): //out - send content to
"ro#ser
o!t(println(=-ello World=):
>
>

Servlet Li"e Cycle
Loading and Instantiation
Initiali/ation
5e'!est -andling
6nd o" Servi&e
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
?
WELCOME TO J2EE NOTES
Session Trac#in$
T#pi&al s&enario $ shopping &art in online store
0e&essar# be&a!se -TTP is a =stateless= proto&ol
Session Tra&@ing .PI allows #o! to
$loo@ !p session ob*e&t asso&iated with &!rrent re'!est
$&reate a new session ob*e&t when ne&essar#
$loo@ !p in"oration asso&iated with a session
$store in"oration in a session
$dis&ard &opleted or abandoned sessions
Session Trac#in$ %&' ( '
Loo@ing !p a session ob*e&t
$-ttpSession session < re'!est(getSession(tr!e):
$Pass true to &reate a new session i" one does not e+ist
.sso&iating in"oration with session
$session(set.ttrib!te(A!serB,re'!est(getParaeter(AnaeB))
$Session attrib!tes &an be o" an# t#pe
Loo@ing !p session in"oration
$String nae < (String) session(get.ttrib!te(A!serB)
Session Trac#in$ %&' ( ''
$et') * $the !ni'!e identi"ier generated "or the session
isNe+ * $tr!e i" the &lient (browser) has never seen the session
$etCreationTime * $tie in illise&onds sin&e session was ade
$etLast%ccesse)Time * $tie in illise&onds sin&e the session was
last sent "ro &lient
$etMa,'nactive'nterval * $C o" se&onds session sho!ld go witho!t
a&&ess be"ore being invalidated ( $negative val!e indi&ates that session
sho!ld never tieo!t
Java,-Servlet 'nter"ace Classes
Servlet Generi&servlet
Servlet5e'!est ServletInp!tStrea
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
D
WELCOME TO J2EE NOTES
Servlet5espon&e Servlet3!tp!tStrea
ServletCon"ig Servlet6+&eption
ServletConte+t 4navailable6+&eption
SingleThreadEodel )
Java,-Servlet-Http Classes
-ttpServlet5e'!est Coo@ie
-ttpServlet5esponse -ttpServlet
-ttpSession -ttpSession7inding6vent
-ttpSessionConte+t -ttp4tils
-ttpSession7indingListener )
E,ceptions
Servlet6+&eption
4navailable6+&eption
SERVLETS
.- What is the servlet/
Servlets are od!les that e+tend re'!estFresponse)oriented servers,
s!&h as Java)enabled web servers( Gor e+aple, a servlet a# be
responsible "or ta@ing data in an -TEL order)entr# "or and appl#ing
the b!siness logi& !sed to !pdate a &opan#Hs order database(
)Servlets are !sed to enhan&e and e+tend the "!n&tionalit# o" Webserver(
)Servlets handles Java and -TEL separatel#(
2- What are the !ses o" Servlets/
. servlet &an handle !ltiple re'!ests &on&!rrentl#, and &an
s#n&hroni/e re'!ests( This allows servlets to s!pport s#stes s!&h as
on)line &on"eren&ing( Servlets &an "orward re'!ests to other servers and
servlets( Th!s servlets &an be !sed to balan&e load aong several
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
I
WELCOME TO J2EE NOTES
servers that irror the sae &ontent, and to partition a single logi&al
servi&e over several servers, a&&ording to tas@(
0- What are th characters o" Servlet/
.s Servlet are written in *ava, the# &an a@e !se o" e+tensive power o"
the J.J. .PI,s!&h as networ@ing and 45L
a&&ess,!ltithreading,database&onne&tivit#,5EI ob*e&t seriali/ation(
E""icient K The initila/ation &ode "or a servlet is e+e&!ted onl# on&e,
when the servlet is e+e&!ted "or the "irst tie(
Robest * provide all the power"!ll "eat!res o" J.J., s!&h as 6+&eption
handling and garbage &olle&tion(
&ortableK This enables eas# portabilit# a&ross Web Servers(
&ersistance K In&rease the per"oran&e o" the s#ste b# e+e&!ting
"eat!res data a&&ess(

1- What is the )i""erence bet+een JS& an) SERVLETS
Servlets K servlet tie!p "iles to independitentl# handle the stati&
presentation logi& and d#nai& b!siness logi& , d!e to this a &hanges
ade to an# "ile re'!ires re&opilation o" the servlet(
) The servlet is Pre)Copile(
JS& K Ga&ilities segregation o" wor@ pro"iles to Web)%eveloper and
Web)%esigner , .!toati&all# in&orporates &hanges ade to an# "ile
(PL L 7L) , no need to re&opile(
Web)%eveloper write the &ode "or 7!ssiness logi& whereas Web)
%esigner designs the la#o!t "or the WebPage b# -TEL L JSP(
) The JSP is Post)Copile(
2- What are the a)vanta$es !sin$ servlets than !sin$ C3'/
Servlets provide a wa# to generate d#nai& do&!ents that is both
easier to write and "aster to r!n( It is e""i&ient, &onvenient, power"!l,
portable, se&!re and ine+pensive( Servlets also address the proble o"
doing server)side prograing with plat"or)spe&i"i& .PIs( The# are
developed with Java Servlet .PI, a standard Java e+tension(
4- What is the )i""erence bet+een servlets an) applets/
Servlets are to servers( .pplets are to browsers( 4nli@e applets,
however, servlets have no graphi&al !ser inter"a&e(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
M
WELCOME TO J2EE NOTES
5- What is the )i""erence bet+een 3enericServlet an) HttpServlet/
3enericServlet is "or servlets that ight not !se -TTP, li@e "or
instan&e GTP servi&e(.s o" onl# -ttp is ipleented &opletel# in
-ttpServlet( The Generi&Servlet has a servi&e() ethod that gets &alled
when a &lient re'!est is ade( This eans that it gets &alled b# both
in&oing re'!ests and the -TTP re'!ests are given to the servlet as
the# are(
Generi&Servlet belongs to *ava+(servlet pa&@age
Generi&Servlet is an abstra&t &lass whi&h e+tends 3b*e&t and
ipleents Servlet, ServletCon"ig and *ava(io(Seriali/able inter"a&es(
The dire&t s!b&lass to Generi&Servlet is -ttpServlet(It is a proto&ol)
independent servlet
6- What are the )i""erences bet+een 3ET an) &OST service metho)s/
3et Metho) * 4ses N!er# String to send additional in"oration to the
server(
)N!er# String is displa#ed on the &lient 7rowser(
N!er# String K The additional se'!en&e o" &hara&ters that are appended
to the 45L ia &alled N!er# String( The length o" the N!er# string is
liited to ?MM &hara&ters(
)The ao!nt o" in"oration #o! &an send ba&@ !sing a G6T is restri&ted
as 45Ls &an onl# be 8O?I &hara&ters(
&OST Metho) * The Post Eethod sends the %ata as pa&@ets thro!gh a
separate so&@et &onne&tion( The &oplete transa&tion is invisible to the
&lient( The post ethod is slower &opared to the Get ethod be&a!se
%ata is sent to the server as separate pa&@ates(
))2o! &an send !&h ore in"oration to the server this wa# ) and itHs
not restri&ted to te+t!al data either( It is possible to send "iles and even
binar# data s!&h as seriali/ed Java ob*e&tsP
7- What is the servlet li"e cycle/
In Servlet li"e &#&les are,
init(),servi&es(),destor#()(
'nit8 9 K Is &alled b# the Servlet &ontainer a"ter the servlet has ben
Instantiated(
))Contains all in"oration &ode "or servlet and is invo@ed when the servlet is
"irst loaded(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
Q
WELCOME TO J2EE NOTES
)The init( ) does not re'!ire an# arg!ent , ret!rns a void and throws Servlet
6+&eption(
)I" init() e+e&!ted at the tie o" servlet &lass loading(.nd init() e+e&!ted
onl# "or "irst !ser(
)2o! &an 3verride this ethod to write initiali/ation &ode that needs to r!n
onl# on&e, s!&h as loading a driver , initiali/ing val!es and soon, Inother
&ase #o! &an leave norall# blan@(
P!bli& void init(ServletCon"ig Con"ig) throws Servlet6+&eption
Service8 9 K is &alled b# the Servlet &ontainer a"ter the init ethod to allow
the servlet to respond to a re'!est(
)5e&eives the re'!est "ro the &lient and identi"ies the t#pe o" re'!est and
deligates the to doGet( ) or doPost( ) "or pro&essing(
P!bli& void servi&e(Servlet5e'!est re'!est,Servlet5espon&e response)
throws Servlet6+&eption, I36+&eption
:estroy8 9 K The Servlet Container &alls the destro#( ) be"ore reoving a
Servlet Instan&e "ro Sevi&e(
)6+&!tes onl# on&e when the Servlet is reoved "ro Server(
P!bli& void destro#( )
$f ser%ices(! are "oth for get and post methods(
)So i" ! want to !se post ethod in htl page,we !se doPost() or servi&es()
in servlet &lass(
)i" want to !se get ethods in htl page,we &an !se doGet() or servi&es() in
servlet &alss(
)Ginall# destor#() is !sed to "ree the ob*e&t(
.;- What is the )i""erence bet+een ServletConte,t an) ServletCon"i$/
7oth are inter"a&es(
Servlet Con"i$89*The servlet engine ipleents the ServletCon"ig
inter"a&e in order to pass &on"ig!ration in"oration to a servlet( The
server passes an ob*e&t that ipleents the ServletCon"ig inter"a&e to
the servletHs init() ethod(
. ServletCon"ig ob*e&t passes &on"ig!ration in"oration "ro the
server to a servlet( ServletCon"ig also in&l!des ServletConte+t ob*e&t(
getParameter( ! , get&er%let'onte(t( ! , get&er%let'onfig( !,
)et&er%let*ame( !
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
R
WELCOME TO J2EE NOTES
Servlet Conte,t89* The ServletConte+t inter"a&e provides in"oration
to servlets regarding the environent in whi&h the# are r!nning( It also
provides standard wa# "or servlets to write events to a log "ile(
ServletConte+t de"ines ethods that allow a servlet to intera&t with
the host server( This in&l!des reading server)spe&i"i& attrib!tes, "inding
in"oration abo!t parti&!lar "iles lo&ated on the server, and writing to
the server log "iles( I " there are several virt!al servers r!nning, ea&h one
a# ret!rn a di""erent ServletConte+t(
getM$M+Type( ! , get,esourse( !,
get'onte(t( !,get&er%er$nfo( !,get&er%let'ontet*ame( !
..- Can ' invo#e a JS& error pa$e "rom a servlet/
2es, #o! &an invo@e the JSP error page and pass the e+&eption ob*e&t
to it "ro within a servlet( The tri&@ is to &reate a re'!est dispat&her "or
the JSP error page, and pass the e+&eption ob*e&t as a
*ava+(servlet(*sp(*sp6+&eption re'!est attrib!te( -owever, note that #o!
&an do this "ro onl# within &ontroller servlets(
.2- '" yo!r servlet opens an O!tp!tStream or &rintWriter< the JS&
en$ine +ill thro+ the "ollo+in$ translation error*
*ava(lang(IllegalState6+&eptionK Cannot "orward as 3!tp!tStrea or
Writer has alread# been obtained
.0- Can ' =!st abort processin$ a JS&/
2es(7e&a!se #o!r JSP is *!st a servlet ethod,#o! &an *!st p!t
(whereever ne&essar#) a S T ret!rn: TU
.1- What is a better approach "or enablin$ threa)(sa"e servlets an)
JS&s/ Sin$leThrea)Mo)el 'nter"ace or Synchroni>ation/
.ltho!gh the SingleThreadEodel te&hni'!e is eas# to !se, and
wor@s well "or low vol!e sites, it does not s&ale well( I" #o! anti&ipate
#o!r !sers to in&rease in the "!t!re, #o! a# be better o"" ipleenting
e+pli&it s#n&hroni/ation "or #o!r shared data( The @e# however, is to
e""e&tivel# inii/e the ao!nt o" &ode that is s#n&hron/ied so that
#o! ta@e a+i! advantage o" !ltithreading(
.lso, note that SingleThreadEodel is prett# reso!r&e intensive "ro the
serverHs perspe&tive( The ost serio!s iss!e however is when the
n!ber o" &on&!rrent re'!ests e+ha!st the servlet instan&e pool( In that
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
V
WELCOME TO J2EE NOTES
&ase, all the !nservi&ed re'!ests are '!e!ed !ntil soething be&oes
"ree ) whi&h res!lts in poor per"oran&e( Sin&e the !sage is non)
deterinisti&, it a# not help !&h even i" #o! did add ore eor#
and in&reased the si/e o" the instan&e pool(
.2- '" yo! +ant a servlet to ta#e the same action "or both 3ET an)
&OST re?!est< +hat sho!l) yo! )o/
Sipl# have doGet &all doPost, or vi&e versa(
.4- Which co)e line m!st be set be"ore any o" the lines that !se the
&rintWriter/
setContentT#pe() ethod !st be set be"ore transitting the a&t!al
do&!ent(
.5- Ho+ HTT& Servlet han)les client re?!ests/
.n -TTP Servlet handles &lient re'!ests thro!gh its servi&e ethod(
The servi&e ethod s!pports standard -TTP &lient re'!ests b#
dispat&hing ea&h re'!est to a ethod designed to handle that re'!est(
.6- What is the Servlet 'nter"ace/
The &entral abstra&tion in the Servlet .PI is the Servlet inter"a&e(
.ll servlets ipleent this inter"a&e, either dire&tl# or, ore &oonl#,
b# e+tending a &lass that ipleents it s!&h as -ttpServlet(
Servlets))UGeneri& Servlet))U-ttpServlet))UE#Servlet(
The Servlet inter"a&e de&lares, b!t does not ipleent, ethods that
anage the servlet and its &o!ni&ations with &lients( Servlet writers
provide soe or all o" these ethods when developing a servlet(
.7- When a servlet accepts a call "rom a client< it receives t+o ob=ects-
What are they/
Servelt5e'!estK whi&h en&aps!lates the &o!ni&ation "ro the &lient
to the server(
Servlet5esponseK whi&h en&aps!lates the &o!ni&ation "ro the
servlet ba&@ to the
Client(
Servlet5e'!est and Servlet5esponse are inter"a&es de"ined b# the
*ava+(servlet pa&@age(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
W
WELCOME TO J2EE NOTES
2;- What in"ormation that the ServletRe?!est inter"ace allo+s the
servlet access to/
In"oration s!&h as the naes o" the paraeters passed in b# the
&lient, the proto&ol (s&hee) being !sed b# the &lient, and the naes o"
the reote host that ade the re'!est and the server that re&eived it( The
inp!t strea, ServletInp!tStrea(Servlets !se the inp!t strea to get
data
"ro &lients that !se appli&ation proto&ols s!&h as the -TTP P3ST and
P4T ethods(
2.- What in"ormation that the ServletResponse inter"ace $ives the
servlet metho)s "or replyin$ to the client/
It .llows the servlet to set the &ontent length and EIE6 t#pe o" the
repl#( Provides an o!tp!t strea, Servlet3!tp!tStrea and a Writer
thro!gh whi&h the servlet &an send the repl# data(
22- :i""erence bet+een sin$le threa) an) m!lti threa) mo)el servlet
. servlet that ipleents SingleThreadEodel eans that "or ever#
re'!est, a single servlet instan&e is &reated( This is not a ver# s&alable
sol!tion as ost web servers handle !ltit!des o" re'!ests( . !lti)
threaded servlet eans that one servlet is &apable o" handling an#
re'!ests whi&h is the wa# ost servlets sho!ld be ipleented(
a( . single thread odel "or servlets is generall# !sed to prote&t sensitive
data ( ban@ a&&o!nt operations )(
b( Single thread odel eans instan&e o" the servlet gets &reated "or ea&h
re'!est re&ieved( Its not thread sa"e whereas in !lti threaded onl#
single instan&e o" the servlet e+ists "or what ever C o" re'!ests re&ieved(
Its thread sa"e and is ta@en &are b# the servlet &ontainer(
&( . servlet that ipleents SingleThreadEodel eans that "or ever#
re'!est, a single servlet instan&e is &reated( This is not a ver# s&alable
sol!tion as ost web servers handle !ltit!des o" re'!ests( . !lti)
threaded servlet eans that one servlet is &apable o" handling an#
re'!ests whi&h is the wa# ost servlets sho!ld be ipleented(
. single thread odel "or servlets is generall# !sed to prote&t sensitive
data ( ban@ a&&o!nt operations )(
20- What is servlet conte,t an) +hat it ta#es act!ally as parameters/
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8O
WELCOME TO J2EE NOTES
Servlet &onte+t is an ob*e&t whi&h is &reated as soon as the Servlet gets
initiali/ed(Servlet &onte+t ob*e&t is &ontained in Servlet Con"ig( With the
&onte+t ob*e&t ! &an get a&&ess to spe&i"i&
reso!r&e (li@e "ile) in the server and pass it as a 45L to be displa#ed as a
ne+t s&reen with the help o" 5e'!est%ispat&her
eg K)
ServletConte+t app < getServletConte+t():
5e'!est%ispat&her disp:
i"(b<<tr!e)
disp < app(get5e'!est%ispat&her
(=*spFloginF!pdatepassword(*sp=):
else
disp < app(get5e'!est%ispat&her
(=*spFloginFerror(*sp=):
this &ode will ta@e !ser to the s&reen depending !pon the val!e o" b(
in ServletConte+t ! &an also get or set soe variables whi&h ! wo!ld
li@e to retreive in ne+t s&reen(
eg
&onte+t(set.ttrib!te(=s!pport.ddress=, =tepXtep(&o=):
7etter #et, #o! &o!ld !se the web(+l &onte+t)para eleent to
designate the address, then read it with the getInitParaeter ethod
o" ServletConte+t(
21- Can +e call )estroy89 metho) on servlets "rom service metho)/
destro#() is a servlet li"e)&#&le ethod &alled b# servlet &ontainer to @ill
the instan&e o" the servlet( =2es=( 2o! &an &all destro#() "ro within the
servi&e()( It will do whatever logi& #o! have in destro#() (&lean!p, reove
attrib!tes, et&() b!t it wonHt =!nload= the servlet instan&e itsel"( That &an onl#
be done b# the &ontainer
22- What is the !se o" ServletCon"i$ an) ServletConte,t--/
.n inter"a&e that des&ribes the &on"ig!ration paraeters "or a
servlet( This is passed to the servlet when the web server &alls its init()
ethod( 0ote that the servlet sho!ld save the re"eren&e to the
ServletCon"ig ob*e&t, and de"ine a getServletCon"ig() ethod to ret!rn
it when as@ed( This inter"a&e de"ines how to get the initiali/ation
paraeters "or the servlet and the &onte+t !nder whi&h the servlet is
r!nning(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
88
WELCOME TO J2EE NOTES
.n inter"a&e that des&ribes how a servlet &an get in"oration abo!t
the server in whi&h it is r!nning( It &an be retrieved via the
getServletConte+t() ethod o" the ServletCon"ig ob*e&t(
24- What is )i""erence bet+een "or+ar)89 an) sen)Re)irect89-- / Which
one is "aster then other an) +hich +or#s on server/
@or+ar)8 9 * *ava+(Servlet(5e'!est%ispat&her inter"a&e(
)5e'!est%ispat&her("orward( ) wor@s on the Server(
)The "orward( ) wor@s inside the WebContainer(
)The "orward( ) restri&ts #o! to redire&t onl# to a reso!r&e in the sae
web).ppli&ation(
)."ter e+e&!ting the "orward( ), the &ontrol will ret!rn ba&@ to the sae
ethod "ro where the "orward ethod was &alled(
)the "orward( ) will redire&t in the appli&ation server itsel", it doesn
&oe ba&@ to the &lient(
) The "orward( ) is "aster than Sendredire&t( ) (
To !se the "orward( ) o" the re'!est%ispat&her inter"a&e, the "irst thing
to do is to obtain 5e'!est%ispat&her 3b*e&t( The Servlet te&hnolog#
provides in three wa#s(
8( 7# !sing the get5e'!est%ispat&her( ) o" the
*ava+(Servlet(ServletConte+t inter"a&e , passing a String &ontaining the
path o" the other reso!r&es, path is relative to the root o" the
ServletConte+t(
,equest-ispatcher
rd.request.get,equest-ispatcher(/second&er%let0!1
,d.for#ard(request, response!1
?( get5e'!est%ispat&her( ) o" the 2a%a(.&er%let.,equest interface , the
path is relative to &!rrent -tp5e'!est(
,equest-ispatcher
rd.get&er%let'onte(t( !.get,equest-ispatcher(/ser%let/second&er%let0!
1
,d.for#ard(request, response!1
D( 7# !sing the get0ae%ispat&her( ) o" the
2a%a(.&er%let.&er%let'onte(t inter"a&e(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8?
WELCOME TO J2EE NOTES
,equest-ispatcher
rd.get&er%let'onte(t( !.get*ame-ispatcher(/second&er%let0!1
,d.for#ard(request, response!1
Sen)re)irect8 9 * *ava+(Servlet(-ttp(-ttpServlet5espon&e inter"a&e
)5e'!est%ispat&her(Send5edire&t( ) wor@s on the browser(
)The Send5edire&t( ) allows #o! to redire&t trip to the Client(
)The Send5edire&t( ) allows #o! to redire&t to an# 45L(
)."ter e+e&!ting the Send5edire&t( ) the &ontrol will not ret!rn ba&@ to
sae ethod(
)The Client re&eives the -ttp response &ode DO? indi&ating that
teporarl# the &lient is being redire&ted to the spe&i"ied lo&ation , i" the
spe&i"ied lo&ation is relative , this ethod &onverts it into an absol!te
45L be"ore redire&ting(
)The Send5edire&t( ) will &oe to the Client and go ba&@,(( ie 45L
appending will happen(
5esponse( Send5edire&t( Aabsol!te pathB):
.bsol!tepath $ other than appli&ation , relative path ) sae appli&ation(
When #o! invo@e a "orward re'!est, the re'!est is sent to another
reso!r&e on the server, witho!t the &lient being in"ored that a di""erent
reso!r&e is going to pro&ess the re'!est( This pro&ess o&&!rs &opletel#
with in the web &ontainer( When a send5edirte&t ethod is invo@ed, it
&a!ses the web &ontainer to ret!rn to the browser indi&ating that a new
45L sho!ld be re'!ested( 7e&a!se the browser iss!es a &opletel# new
re'!est an# ob*e&t that are stored as re'!est attrib!tes be"ore the redire&t
o&&!rs will be lost( This e+tra ro!nd trip a redire&t is slower than
"orward(
25- )o +e have a constr!ctor in servlet / can +e e,plictly provi)e a
constr!ctor in servlet pro$ramme as in =ava pro$ram /
We &an have a &onstr!&tor in servlet (
Session K . session is a gro!p o" a&tivities that are per"ored b# a !ser
while a&&esing a parti&!lar website(
Session Trac#in$ KThe pro&ess o" @eeping tra&@ o" settings a&ross
session is &alled session tra&@ing(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8D
WELCOME TO J2EE NOTES
Hi))en @orm @iel)s K 4sed to @eep tra&@ o" !sers b# pla&ing hidden
"ields in the "or(
)The val!es that have been entered in these "ields are sent to the server
when the !ser s!bits the Gor(
ARL(re+ritin$ K this is a te&hni'!e b# whi&h the 45L is odi"ied to
in&l!de the session I% o" a parti&!lar !ser and is sent ba&@ to the Client(
)The session Id is !sed b# the &lient "or s!bse'!ent transa&tions with
the server(
Coo#ies K Coo@ies are sall te+t "iles that are !sed b# a webserver to
@eep tra&@ the 4sers(
. &oo@ie is &reated b# the server and send ba&@ to the &lient , the val!e
is in the "or o" 1e#)val!e pairs( .&lient &an a&&ept ?O &oo@ies per host
and the si/e o" ea&h &oo@ie &an be a+i! o" I b#tes ea&h(
HttpSession K 6ver# !ser who logs on to the website is a!toeta&all#
asso&iated with an -ttpSession 3b*e&t(
)The Servlet &an !se this 3b*e&t to store in"oration abo!t the !sers
Session(
)-ttpSession 3b*e&t enables the !ser to aintain two t#pes o" %ata(
ie State and .ppli&ation(
26- Ho+ to comm!nicate bet+een t+o servlets/
Two wa#sK
a( Gorward or redire&t "ro one Servlet to another(
b( Load the Servlet "ro ServletConte+t and a&&ess ethods(
27- Ho+ to $et one ServletBs Conte,t 'n"ormation in another Servlet/
.&&ess or load the Servlet "ro the Servlet Conte+t and a&&ess the
Conte+t In"oration
0;- The "ollo+in$ co)e snippet )emonstrates the invocation o" a JS&
error pa$e "rom +ithin a controller servlet*
prote&ted void send6rror5edire&t(-ttpServlet5e'!est re'!est,
-ttpServlet5esponse response, String errorPage45L, Throwable e) throws
Servlet6+&eption, I36+&eption ;
re'!est(set.ttrib!te (=*ava+(servlet(*sp(*sp6+&eption=, e):
getServletCon"ig()(getServletConte+t()(
get5e'!est%ispat&her(errorPage45L)("orward(re'!est, response):
>
p!bli& void doPost(-ttpServlet5e'!est re'!est, -ttpServlet5esponse
response)
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8I
WELCOME TO J2EE NOTES
;
tr# ;
FF do soething
> &at&h (6+&eption e+) ;
tr# ;
send6rror5edire&t(re'!est,response,=F*spFE#6rrorPage(*sp=,e+):
> &at&h (6+&eption e) ;
e(printSta&@Tra&e():
>
> >

JS& 8 JavaServer &a$es9
Wh# JSP Te&hnolog#?
Servlets are good at r!nning logi&
$0ot so good at prod!&ing large ao!nts o" o!tp!t
$o!t(+rite() is !gl#
JSP pages are great at prod!&ing lots o" te+t!al o!tp!t
$0ot so good at lots o" logi&
$CD DE is !gl#
Ho+ )oes it Wor#
A3&P pageB
$Ei+t!re o" te+t, S&ript and dire&tives
$Te+t &o!ld be te+tF htl, te+tF +l or te+tF plain
A3&P engineB
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8M
WELCOME TO J2EE NOTES
$YCopiles page to servlet
$6+e&!tes servlets servi&e() ethod
Sends te+t ba&@ to &aller
Page is
$Copiled on&e
$6+e&!ted an# ties
%natomy o" a JS&
STX page lang!age<A*avaB &ontentT#pe<Ate+tFhtlB TU
ShtlU
Sbod# bg&olor<AwhiteBU
S*spK!se7ean id<AgreetingB
&lass<A&o(praati(*sp(beans(Greeting7eanBU
S*spKsetPropert# nae<AgreetingB propert#<A9BFU
SF*spK!ser7eanU
The "ollowing in"oration was savedK
4ser 0aeK
S*spKgetPropert# nae<AgreetingB propert#<A!ser0aeBFU
Wel&oeP
SFbod#U
SFhtlU
JS& Elements
-irecti%e +lements 4 $In"oration abo!t the page
$5eains sae between re'!ests
$6(g(, s&ripting lang!age !sed
5ction +lements 4 $Ta@e a&tion based on in"o re'!ired at re'!est)tie
Standard
C!sto (Tags and Tag Libraries)
&cripting +lements
$.dd pie&es o" &ode to generate o!tp!t based on &onditions
:irectives
Global in"oration !sed b# the AJSP engineB
3" "or CDF )irective attrG list DE
3r C=sp* )irective- )irective attrG list HE
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8Q
WELCOME TO J2EE NOTES
$%ire&tive &o!ld be
Page
In&l!de
Taglib
$6( g(,
STX page in"o<A written b# %evelopEentorB TU
S*spK dire&tive( page iport<A *ava( s'l(9B FU
STX in&l!de "ile <AZ soe"ile( t+tB TU
STX taglib !ri < tags pre"i+<A "ooB TU
%ctions Within a JS& &a$e
Spe&i"ies an a&tion to be &arried o!t b# the AJSP engineB
Standard or &!sto
$Standard !st be ipleented b# all engines
$C!sto de"ined in tag libraries
Standard a&tions Ys&oped b# Y*sp naespa&e
-ave nae and attrib!tes
S*spK !se7ean id<A &lo&@B &lass<A *ava(!til(%ateB FU
S!lU The &!rrent date at the server isK
SliU %ateK S*spK getPropert# nae<A&lo&@B propert#<AdateB FU
SliU EonthK S*spK getPropert# nae<A&lo&@B propert#<AonthB FU
SF!lU
Stan)ar) JS& %ctions *
*spK!se7ean
*spKgetPropert#
*spKsetPropert#
*spKin&l!de
*spK"orward
*spKpara
*spKpl!gin
Scriptlets
3" "or CD HI co)e $oes hereIH DE
$Gets &opied into [ =spService ethod o" generated servlet
.n# valid Java &ode &an go here
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8R
WELCOME TO J2EE NOTES
CO:E* OAT&AT
ST int *: TU Sval!eU OSF val!eU
ST "or (* < O: * S D: *\\) ;TU Sval!eU 8SF val!eU
Sval!eU Sval!eU ?SF val!eU
ST o!t( write(==\ *): TU
SF val!eUST > TU
:eclarations 8CDJ K DE9
4sed to de&lare &lass s&ope variables or ethods
STP int * < O: TU
Gets de&lared at &lass) level s&ope in the generated servlet

p!bli& &lass SoeJSP e+tends -ttpServlet ipleents -ttpJspPage ;


]
int * < O:
void [*spServi&e(]) ;>
>
:eclarations 8CDJ K DE9
4sed to de&lare &lass s&ope variables or ethods
STP int * < O: TU
Gets de&lared at &lass) level s&ope in the generated servlet

p!bli& &lass SoeJSP e+tends -ttpServlet ipleents -ttpJspPage ;


]
int * < O:
void [*spServi&e(]) ;>
>
JS& to Servlet Translation
STX page iport<=*ava+(e*b(9,*ava+(naing(9,*ava(ri(9 ,*ava(!til(9=
TU
S-TELUS-6.%USTITL6U-ello(*spSFTITL6USF-6.%US73%2U
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8V
WELCOME TO J2EE NOTES
ST String &he&@ing < n!ll:
String nae < n!ll:
&he&@ing < re'!est(getParaeter(=&at&h=):
i" (&he&@ing P< n!ll) ;
nae < re'!est(getParaeter(=nae=):TU
SbU -ello ST<naeTU
ST > TU
SG35E E6T-3%<HP3STH a&tion<=-ello(*sp=U
Stable width<=MOO= &ellspa&ing<=O= &ellpadding<=D= border<=O=U
S&aptionU6nter #o!r naeSF&aptionU
StrUStdUSbU0aeSFbUSFtdUStdUSI0P4T si/e<=?O=
a+length<=?O= T2P6<=te+t= 0.E6<=nae=USFtdUSFtrU
SFtableU
SI0P4T T2P6<HS47EITH 0.E6<HS!bitH J.L46<HS!bitHU
SI0P4T T2P6<HhiddenH 0.E6<H&at&hH J.L46<H#esHU
SFG35EUSF73%2USF-TELU
3enerate) ServletK
p!bli& void [*spServi&e(-ttpServlet5e'!est re'!est ,
-ttpServlet5esponse response)
throws Servlet6+&eption ,I36+&eption ;
o!t(write(=S-TELUS-6.%USTITL6U-ello(*spSFTITL6USF-6.%US
73%2U= ):
String &he&@ing < n!ll:
String nae < n!ll:
&he&@ing < re'!est(getParaeter(=&at&h=):
i" (&he&@ing P< n!ll) ;
nae < re'!est(getParaeter(=nae=):
o!t(write(=ZrZnZtZtSbU -ello = ):
o!t(print(nae):
o!t(write(=ZrZnZtZt= ):
>
o!t(write(=ZrZnZtZtSG35E E6T-3%<HP3STH a&tion<=
\=Z=-ello(*spZ=UZrZnZtZtZtStable width<Z=MOOZ=
&ellA]]]]]]]]]]]((
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8W
WELCOME TO J2EE NOTES
>
>
Ta$s L Ta$ Libraries
What Is a Tag Librar#?
JSP te&hnolog# has a set o" pre) de"ined tags
$S*spK !se7ean ]FU
These are -TEL li@e b!t]
] have liited "!n&tionalit#
Can de"ine new tags
$Loo@ li@e -TEL
$Can be !sed b# page a!thors
$AJava &odeB is e+e&!ted when tag is en&o!ntered
$.llow !s to @eep Java &ode o"" the page
7etter separation o" &ontent and logi&
Ea# -ave Tags To]
Pro&ess an SNL &oand
Parse ^EL and o!tp!t -TEL
.!toati&all# &all into an A6J7 &oponentB (6J7 _ te&hnolog#)
based &oponent)
Get &alled on ever# re'!est to initiali/e s&ript variables
Iterate over a 5es!ltSet and displa# the o!tp!t in an -TEL table
Priar# Tag Classes (=ava,-servlet-=sp-ta$e,t-Ta$9

ipleents

T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
?O
7od#Tag
Inter"a&e
7od#TagS!pport
&lass
WELCOME TO J2EE NOTES
Siple Tag 6+aple K
STX taglib !ri<AFW67)I0GF#lib(tldB pre"i+<AtestB TU
ShtlUSbod# bg&olor<AwhiteBU
StestKhello nae<A5obertB FU
SFbod#U SFhtlU
p!blic class HelloTa$ e,ten)s Ta$S!pport M
private Strin$ name N OWorl)PQ
p!blic voi) setName8Strin$ name9 M this-name N nameQ R
p!blic int )oEn)Ta$89 M pa$eConte,t-$etO!t89-println8OHello O
S name9Q R
R
#lib(tld
StaglibU ]]
StagUSnaeUhelloSFnaeU
Stag&lassU&o(praati(-elloTagSFtag&lassU
Sbod#&ontentUept#SFbod#&ontentU
Sattrib!teUSnaeUnaeSFnaeUSFattrib!teU
SFtagU
SFtaglibU
-ow Tag -andler ethods are invo@ed K
Spre"i+Ktag0ae
attr8<Aval!e8B )))))))))))) set.ttr8(Aval!e8B)
attr?<Aval!e?B )))))))))))) set.ttr?(Aval!e?B)
U )))))))))))) doStartTag()
This tagsHs bod#
SF pre"i+Ktag0aeU)))))))))))) do6ndTag()
Ipleentation o" JSP page will !se the tag handler "or ea&h Ya&tion
on page(
S!mmary
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
?8
WELCOME TO J2EE NOTES
The JSP spe&i"i&ation is a power"!l s#ste "or &reating str!&t!red web
&ontent
JSP te&hnolog# allows non) prograers to develop d#nai& web
pages
JSP te&hnolog# allows &ollaboration between prograers and page
designers when b!ilding web appli&ations
JSP te&hnolog# !ses the Java prograing lang!age as the s&ript
lang!age
The generated servlet &an be anaged b# dire&tives
JSP &oponents &an be !sed as the view in the EJC ar&hite&t!re
.!thors !sing JSP te&hnolog# are not ne&essaril# prograers !sing
Java te&hnolog#
Want to @eep AJava &odeB o"" a AJSP PageB
C!sto a&tions (tag libraries) allow the !se o" eleents as a
repla&eent "or Java &ode
What is JS&( JavaServer &a$es /
JavaServer Pages( . server)side te&hnolog#, JavaServer pages are
an e+tension to the Java servlet te&hnolog# that was developed b# S!n(
JSPs have d#nai& s&ripting &apabilit# that wor@s in tande with
-TEL &ode, separating the page logi& "ro the stati& eleents )) the
a&t!al design and displa# o" the page( 6bedded in the -TEL page, the
Java so!r&e &ode and its e+tensions help a@e the -TEL ore
"!n&tional, being !sed in d#nai& database '!eries, "or e+aple( JSPs
are not restri&ted to an# spe&i"i& plat"or or server(
Jsp &ontains both stati& and d#nai& reso!r&es at r!n tie(Jsp e+tends
web server "!n&tionalities
What are a)vanta$es o" JS&
whenever there is a &hange in the &ode, we dont have to re&opile the
*sp( it a!toati&all# does the &opilation( b# !sing &!sto tags and tag
libraries the length o" the *ava &ode is red!&ed(
What is the )i""erence bet+een incl!)e )irective L =sp*incl!)e
action
incl!)e )irective 89K i" the "ile in&l!des stati& te+t i" the "ile is rarel#
&hanged (the JSP engine a# not re&opile the JSP i" this t#pe o"
in&l!ded "ile is odi"ied) (
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
??
WELCOME TO J2EE NOTES
i" #o! have a &oon &ode snippet that #o! &an re!se a&ross !ltiple
pages (e(g( headers and "ooters)
=sp*incl!)e * "or &ontent that &hanges at r!ntie (to sele&t whi&h
&ontent to render at r!ntie (be&a!se the page and sr& attrib!tes &an ta@e
r!ntie e+pressions) "or "iles that &hange o"ten JSPKin&l!den!ll
What are C!stom ta$s- Why )o yo! nee) C!stom ta$s- Ho+ )o yo!
create C!stom ta$
8) C!sto tags are those whi&h are !ser de"ined(
?) Inorder to separate the presentation logi& in a separate &lass rather
than @eeping in *sp page we &an !se &!sto tags(
D) Step 8 K 7!ild a &lass that ipleents the *ava+(servlet(*sp(tage+t(Tag
inter"a&e as "ollows( Copile it and pla&e it !nder the web)in"F&lasses
dire&tor# (in the appropriate pa&@age str!&t!re)(
pa&@age e+aples:
iport *ava(io(9: FFFF T-IS P53G5.E IS 6J652 TIE6 I E6.0
W-60 4 56G56S- T-.T P.5TIC4L.5 C45560T %.T6 T-IS
C4ST3E T.G WILL %ISPL.2
iport *ava+(servlet(*sp(9:
iport *ava+(servlet(*sp(tage+t(9:
p!bli& &lass Show%ateTag ipleents Tag ;
private PageConte+t pageConte+t:
private Tag parent:
p!bli& int doStartTag() throws Jsp6+&eption ;
ret!rn S1IP[73%2: >
p!bli& int do6ndTag() throws Jsp6+&eption ;
tr# ;
pageConte+t(get3!t()(write(== \ new *ava(!til(%ate()):
> &at&h (I36+&eption ioe) ;
throw new Jsp6+&eption(ioe(getEessage()):
>
ret!rn 6J.L[P.G6: >
p!bli& void release() ;
>
p!bli& void setPageConte+t(PageConte+t page) ;
this(pageConte+t < page:
>
p!bli& void setParent(Tag tag) ;
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
?D
WELCOME TO J2EE NOTES
this(parent < tag:
>
p!bli& Tag getParent() ;
ret!rn this(parent: > >
Step ?K0ow we need to des&ribe the tag, so &reate a "ile &alled taglib(tld
and pla&e it !nder the web)in"
dire&tor#(=httpKFF*ava(s!n(&oF*?eeFdtdsFweb)*sptaglibrar#[8[8(dtd=U
8(O 8(8
#Tag httpKFFwww(#&opan#(&oFtaglib E# own tag librar#
show%ate e+aples(Show%ateTag Show the &!rrent date
Step D K 0ow we need to tell the web appli&ation where to "ind the
&!sto tags, and how the# will be re"eren&ed "ro JSP pages( 6dit the
web(+l "ile !nder the web)in" dire&tor# and insert the "ollowing ^EL
"rageent(httpKFFwww(#&opan#(&oFtaglib FW67)I0GFtaglib(tld
Step I K .nd "inall#, &reate a JSP page that !ses the &!sto tag(0ow
restart the server and &all !p the JSP pageP 2o! sho!ld noti&e that ever#
tie the page is re'!ested, the &!rrent date is displa#ed in the browser(
Whilst this doesnHt e+plain what all the vario!s parts o" the tag are "or
(e(g( the tag des&ription, page &onte+t, et&) it sho!ld get #o! going( I"
#o! !se the t!torial (above) and this e+aple, #o! sho!ld be able to
grasp whatHs going onP There are soe ethods in &onte+t ob*e&t with
the help o" whi&h ! &an get the server (or servlet &ontainer) in"oration(
.part "ro all this with the help o" ServletConte+t ! &an ipleent
ServletConte+tListener and then !se the get)InitParaeterethod to
read &onte+t initiali/ation paraeters as the basis o" data that will be
ade available to all servlets and JSP pages(
What are the implicit ob=ects in JS& L )i""erences bet+een them
There are nine ipli&it ob*e&ts in JSP(
8( re?!est K The re'!est ob*e&t represents httpre'!est that are trigged b#
servi&e( ) invo&ation( *ava+(servlet
2- responseKThe response ob*e&t represents the servers response to
re'!est(
*ava+(servlet
D( pa$eConte,t K The page &onte+t spe&i"ies the single entr# point to
an# o" the page attrib!tes and is the &onvient pla&e to p!t shared data(
*ava+(servlet(*sp(page&onte+t
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
?I
WELCOME TO J2EE NOTES
I( session K the session ob*e&t represents the session &reated b# the
&!rrent !ser(
*ava+(Servlet(http(-ttpSession
M( application K the appli&ation ob*e&t represents servlet &onte+t ,
obtained "ro servlet &on"igaration ( *ava+(Servlet(ServletConte+t
Q( o!t K the o!t ob*e&t represents to write the o!t p!t strea (
*ava+(Servlet(*sp(*spWriter
R( Con"i$ Kthe &on"ig ob*e&t represents the servlet &on"ig inter"a&e "ro
this page,and has s&ope attrib!te( *ava+(Servlet(ServletCon"ig
V( pa$e K The ob*e&t is th eInstan&e o" page ipleentation servlet &lass
that are pro&essing the &!rrent re'!est(
*ava(lang(3b*e&t
W( e,ception K These are !sed "or di""erent p!rposes and a&t!all# ! no
need to &reate these ob*e&ts in JSP( JSP &ontainer will &reate these
ob*e&ts a!toati&all#(
*ava(lang(Throwable
2o! &an dire&tl# !se these ob*e&ts(
E,ample*
I" i want to p!t # !sernae in the session in JSP(
JSP PageK In the abo!t page, i a !sing session ob*e&t( 7!t this session
ob*e&t is not de&lared in JSP "ile, be&a!se, this is ipli&it ob*e&t and it
will be &reated b# the *sp &ontainer(
I" ! see the *ava "ile "or this *sp page in the wor@ "older o" apa&he
to&at, ! will "ind these ob*e&ts are &reated(
What is =sp*!sebean- What are the scope attrib!tes L )i""erence
bet+een these attrib!tes
page, re'!est, session, appli&ation
What is )i""erence bet+een scriptlet an) e,pression
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
?M
WELCOME TO J2EE NOTES
With e,pressions in JSP, the res!lts o" eval!ating the e+pression
are &onverted to a string and dire&tl# in&l!ded within the o!tp!t page(
T#pi&all# e+pressions are !sed to displa# siple val!es o" variables or
ret!rn val!es b# invo@ing a beanHs getter ethods( JSP e+pressions
begin within tags and do not in&l!de sei&olonsK
7!t scriptlet &an &ontain an# n!ber o" lang!age stateents, variable
or ethod de&larations, or e+pressions that are valid in the page
s&ripting lang!age( Within s&riptlet tags, #o! &an de&lare variables or
ethods to !se later in the "ile, write e+pressions valid in the page
s&ripting lang!age,!se an# o" the JSP pli&it ob*e&ts or an# ob*e&t
de&lared with a
What is :eclaration
:eclaration is !sed in JSP to de&lare ethods and variables(To add a
de&laration, #o! !st !se the se'!en&es to en&lose #o!r de&larations(
Ho+ )o yo! connect to the )atabase "rom JS&
To be pre&ise to &onne&t *db& "ro *sp is not good idea o"&o!rse i"
!r wor@ing on d!# pro*e&ts &onne&ting to sa&&ess ! &an ver# well
!se the sae &onne&tion ob*e&ts ad ethods in !r s&riplets and de"ine
!r &onne&tion ob*e&t in init() ethod(
7!t i" its real tie ! &an !se %.3 design patterns whi&h is widel#
!sed( "or e+ ! write all !r &onne&tion ob*e&t and and s'l '!ires in a
de"iened ethod later !se trans"er ob*e&t `T3 awhi&h is all !r "ields have
getFset ethods and &all it in b!siness ob*e&t`73a so %.3 is a&&essd
with pre&a!tion as it is the &r!&ial( Ginall# ! de"ine *ava bean whi&h is a
&lass holding getFset ethod ipleenting seriali/ation th!s the bean is
&alled in the *sp( So never &onne&t to *db& dire&tl# "ro &lient side sin&e
it &an be ha&@ed b# an# one to get !r password or &redit &ard in"o(
Ho+ )o yo! call store) proce)!res "rom JS&
7# !sing &allable stateent we &an &all stored pro&ed!res and "!n&tions
"ro the database(
Ho+ )o yo! restrict pa$e errors )isplay in the JS& pa$e
set is6rrorPage<"alse
Ho+ )o yo! pass control "rom one JS& pa$e to another
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
?Q
WELCOME TO J2EE NOTES
we &an "orward &ontrol to aother *sp !sing *sp a&tion tags "orward or
in&ll!de
Ho+ )o ' have the JS&($enerate) servlet s!bclass my o+n c!stom
servlet class< instea) o" the )e"a!lt/
3ne sho!ld be ver# &are"!l when having JSP pages e+tend &!sto
servlet &lasses as opposed to the de"a!lt one generated b# the JSP
engine( In doing so, #o! a# lose o!t on an# advan&ed optii/ation that
a# be provided b# the JSPengine( In an# &ase, #o!r new s!per&lass has
to "!l"ill the &ontra&t with the JSPngine b#K Ipleenting the
-ttpJspPage inter"a&e, i" the proto&ol !sed is -TTP, or ipleenting
JspPage otherwise 6ns!ring that all the ethods in the Servlet inter"a&e
are de&lared "inal .dditionall#, #o!r servlet s!per&lass also needs to do
the "ollowingK
The servi&e() ethod has to invo@e the [*spServi&e() ethod
The init() ethod has to invo@e the *spInit() ethod
The destro#() ethod has to invo@e *sp%estro#()
I" an# o" the above &onditions are not satis"ied, the JSP engine a#
throw a translation error( 3n&e the s!per&lass has been developed, #o!
&an have #o!r JSP e+tend it as "ollowsK
STX page e+tends<=pa&@age0ae(Servlet0ae= TS
Ho+ )oes a servlet comm!nicate +ith a JS& pa$e/
The "ollowing &ode snippet shows how a servlet instantiates a bean
and initiali/es it with G35E data posted b# a browser( The bean is then
pla&ed into the re'!est, and the &all is then "orwarded to the JSP page,
7ean8(*sp, b# eans o" a re'!est dispat&her "or downstrea pro&essing(
p!bli& void doPost (-ttpServlet5e'!est re'!est, -ttpServlet5esponse
response)
;
tr# ;
govi(Gor7ean " < new govi(Gor7ean():
String id < re'!est(getParaeter(=id=):
"(set0ae(re'!est(getParaeter(=nae=)):
"(set.ddr(re'!est(getParaeter(=addr=)):
"(set.ge(re'!est(getParaeter(=age=)):
FF!se the id to &op!te
FFadditional bean properties li@e in"o
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
?R
WELCOME TO J2EE NOTES
FFa#be per"or a db '!er#, et&(
FF ( ( (
"(setPersonali/ationIn"o(in"o):
re'!est(set.ttrib!te(="7ean=,"):
$etServletCon"i$89-$etServletConte,t89-$etRe?!est:ispatcher
8TH=spHean.-=spT9-"or+ar)8re?!est< response9Q
> &at&h (6+&eption e+) ;
( ( (
> >
The JSP page 7ean8(*sp &an then pro&ess "7ean, a"ter "irst e+tra&ting it
"ro the de"a!lt re'!est s&ope via the !se7ean a&tion(
*spK!se7ean id<="7ean= &lass<=govi(Gor7ean= s&ope<=re'!est=F
*spKgetPropert# nae<="7ean= propert#<=nae= F
*spKgetPropert# nae<="7ean= propert#<=addr= F
*spKgetPropert# nae<="7ean= propert#<=age= F
*spKgetPropert# nae<="7ean= propert#<=personali/ationIn"o= F
's there a +ay ' can set the inactivity lease perio) on a per(session
basis/
T#pi&all#, a de"a!lt ina&tivit# lease period "or all sessions is set
within #o!r JSPengine adin s&reen or asso&iated properties "ile(
-owever, i" #o!r JSP engine s!pports the Servlet ?(8 .PI, #o! &an
anage the ina&tivit# lease period on a per)session basis( This is done
b# invo@ing the -ttpSession(setEa+Ina&tiveInterval() ethod, right
a"ter the session has been &reated(
Gor e+apleK
ST session(setEa+Ina&tiveInterval(DOO): TU
wo!ld reset the ina&tivit# period "or this session to M in!tes( The
ina&tivit# interval is set in se&onds(
Ho+ can ' set a coo#ie an) )elete a coo#ie "rom +ithin a JS& pa$e/
. &oo@ie, #&oo@ie, &an be deleted !sing the "ollowing s&riptletK
ST
FF&reating a &oo@ie
Coo@ie #&oo@ie < new Coo@ie(=a0ae=,=aJal!e=):
response(addCoo@ie(#&oo@ie):
FFdelete a &oo@ie
Coo@ie @illE#Coo@ie < new Coo@ie(=#&oo@ie=, n!ll):
@illE#Coo@ie(setEa+.ge(O):
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
?V
WELCOME TO J2EE NOTES
@illE#Coo@ie(setPath(=F=):
response(addCoo@ie(@illE#Coo@ie):
TU
Ho+ can ' )eclare metho)s +ithin my JS& pa$e/
2o! &an de&lare ethods "or !se within #o!r JSP page as
de&larations( The ethods &an then be invo@ed within an# other
ethods #o! de&lare, or within JSP s&riptlets and e+pressions(
%o note that #o! do not have dire&t a&&ess to an# o" the JSP ipli&it
ob*e&ts li@e re'!est, response, session and so "orth "ro within JSP
ethods( -owever, #o! sho!ld be able to pass an# o" the ipli&it JSP
variables as paraeters to the ethods #o! de&lare(
@or e,ampleK
STP
p!bli& String whereGro(-ttpServlet5e'!est re') ;
-ttpSession ses < re'(getSession():
(((
ret!rn re'(get5eote-ost():
>
TU
ST
o!t(print(=-i there, I see that #o! are &oing in "ro =):
TU
ST< whereGro(re'!est) TU
.nother 6+apleK
"ile8(*spK
STXpage &ontentT#pe<=te+tFhtl=TU
STP
p!bli& void test(JspWriter writer) throws I36+&eption;
writer(println(=-elloP=):
>
TU
"ile?(*sp
STXin&l!de "ile<="ile8(*sp=TU
ShtlU
Sbod#U
STtest(o!t):T U
SFbod#U
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
?W
WELCOME TO J2EE NOTES
SFhtlU
Ho+ can ' enable session trac#in$ "or JS& pa$es i" the bro+ser has
)isable) coo#ies/
We @now that session tra&@ing !ses &oo@ies b# de"a!lt to asso&iate
a session identi"ier with a !ni'!e !ser( I" the browser does not s!pport
&oo@ies, or i" &oo@ies are disabled, #o! &an still enable session tra&@ing
!sing 45L rewriting( 45L rewriting essentiall# in&l!des the session I%
within the lin@ itsel" as a naeFval!e pair( -owever, "or this to be
e""e&tive, #o! need to append the session I% "or ea&h and ever# lin@ that
is part o" #o!r servlet response( .dding the session I% to a lin@ is
greatl# sipli"ied b# eans o" o" a &o!ple o" ethodsK
response(en&ode45L() asso&iates a session I% with a given 45L, and i"
#o! are !sing redire&tion, response(en&ode5edire&t45L() &an be !sed
b# giving the redire&ted 45L as inp!t( 7oth en&ode45L() and
en&ode5edire&ted45L() "irst deterine whether &oo@ies are s!pported
b# the browser: i" so, the inp!t 45L is ret!rned !n&hanged sin&e the
session I% will be persisted as a &oo@ie(
Consider the "ollowing e+aple, in whi&h two JSP "iles, sa#
hello8(*sp and hello?(*sp, intera&t with ea&h other( 7asi&all#, we &reate a
new session within hello8(*sp and pla&e an ob*e&t within this session(
The !ser &an then traverse to hello?(*sp b# &li&@ing on the lin@ present
within the page(Within hello?(*sp, we sipl# e+tra&t the ob*e&t that was
earlier pla&ed in the session and displa# its &ontents( 0oti&e that we
invo@e the en&ode45L() within hello8(*sp on the lin@ !sed to invo@e
hello?(*sp: i" &oo@ies are disabled, the session I% is a!toati&all#
appended to the 45L, allowing hello?(*sp to still retrieve the session
ob*e&t( Tr# this e+aple "irst with &oo@ies enabled( Then disable &oo@ie
s!pport, restart the brower, and tr# again( 6a&h tie #o! sho!ld see the
aintenan&e o" the session a&ross pages( %o note that to get this
e+aple to wor@ with &oo@ies disabled at the browser, #o!r JSP engine
has to s!pport ARL re+ritin$(
hello8(*sp
STX page session<=tr!e= TU
ST
Integer n! < new Integer(8OO):
session(p!tJal!e(=n!=,n!):
String !rl <response(en&ode45L(=hello?(*sp=):
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
DO
WELCOME TO J2EE NOTES
TU
Sa hre"<HST<!rlTUHUhello?(*spSFaU
hello?(*sp
STX page session<=tr!e= TU
ST
Integer i< (Integer )session(getJal!e(=n!=):
o!t(println(=0! val!e in session is =\i(intJal!e()):
Ho+ )o ' !se a scriptlet to initiali>e a ne+ly instantiate) bean/
. =sp*!seean a&tion a# optionall# have a bod#( I" the bod# is
spe&i"ied, its &ontents will be a!toati&all# invo@ed when the spe&i"ied
bean is instantiated( T#pi&all#, the bod# will &ontain s&riptlets or
*spKsetPropert# tags to initiali/e the newl# instantiated bean, altho!gh
#o! are not restri&ted to !sing those alone( The "ollowing e+aple
shows the =toda#= propert# o" the Goo bean initiali/ed to the &!rrent
date when it is instantiated( 0ote that here, we a@e !se o" a JSP
e+pression within the *spKsetPropert# a&tion(
S*spK!se7ean id<="oo= &lass<=&o(7ar(Goo= U
S*spKsetPropert# nae<="oo= propert#<=toda#=
val!e<=ST<*ava(te+t(%ateGorat(get%ateInstan&e()("orat(new
*ava(!til(%ate())
TU=F U
ST)) s&riptlets &alling bean setter ethods go here ))TU
SF*spK!se7ean U
Ho+ )oes JS& han)le r!n(time e,ceptions/
2o! &an !se the errorPage attrib!te o" the page dire&tive to have
!n&a!ght r!ntie e+&eptions a!toati&all# "orwarded to an error
pro&essing page(
Gor e+apleK
CDF pa$e error&a$eNTerror-=spT DE
redire&ts the browser to the JSP page error(*sp i" an !n&a!ght e+&eption
is en&o!ntered d!ring re'!est pro&essing( Within error(*sp, i" #o!
indi&ate that it is an error)pro&essing page, via the dire&tiveK
STX page is6rrorPage<=tr!e= TU
the Throwable ob*e&t des&ribing the e+&eption a# be a&&essed within
the error page via the e+&eption ipli&it ob*e&t(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
D8
WELCOME TO J2EE NOTES
0oteK 2o! !st alwa#s !se a relative 45L as the val!e "or the
errorPage attrib!te(
Ho+ )o ' prevent the o!tp!t o" my JS& or Servlet pa$es "rom bein$
cache) by the bro+ser/
2o! will need to set the appropriate -TTP header attrib!tes to
prevent the d#nai& &ontent o!tp!t b# the JSP page "ro being &a&hed
b# the browser( J!st e+e&!te the "ollowing s&riptlet at the beginning o"
#o!r JSP pages to prevent the "ro being &a&hed at the browser( 2o!
need both the stateents to ta@e &are o" soe o" the older browser
versions(
ST
response(set-eader(=Ca&he)Control=,=no)store=): FF-TTP 8(8
response(set-eader(=Praga=,=no)&a&he=): FF-TTP 8(O
response(set%ate-eader (=6+pires=, O): FFprevents &a&hing at the pro+#
server
TU
Ho+ )o ' !se comments +ithin a JS& pa$e
2o! &an !se =JSP)st#le= &oents to sele&tivel# blo&@ o!t &ode
while deb!gging or sipl# to &oent #o!r s&riptlets( JSP &oents
are not visible at the &lient(
Gor e+apleK
ST)) the s&riptlet is now &oented o!t
ST
o!t(println(=-ello World=):
TU ))TU
2o! &an also !se -TEL)st#le &oents an#where within #o!r JSP
page( These &oents are visible at the &lient( Gor e+apleK
SP)) (&) ?OOI *avagala+#(&o ))U
3" &o!rse, #o! &an also !se &oents s!pported b# #o!r JSP s&ripting
lang!age within #o!r s&riptlets( Gor e+aple, ass!ing Java is the
s&ripting lang!age, #o! &an haveK
ST
FFsoe &oent
F99
#et another &oent 99F TU
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
D?
WELCOME TO J2EE NOTES
Can ' stop JS& e,ec!tion +hile in the mi)st o" processin$ a re?!est/
2es( Preeptive terination o" re'!est pro&essing on an error
&ondition is a good wa# to a+ii/e the thro!ghp!t o" a high)vol!e
JSP engine( The tri&@ (as!ing Java is #o!r s&ripting lang!age) is to !se
the ret!rn stateent when #o! want to terinate "!rther pro&essing( Gor
e+aple, &onsiderK
ST i" (re'!est(getParaeter(="oo=) P< n!ll) ;
FF generate soe htl or !pdate bean propert#
> else ;
F9 o!tp!t soe error essage or provide redire&tion ba&@ to the inp!t
"or a"ter &reating a eento bean !pdated with the HvalidH "or
eleents that were inp!t( This bean &an now be !sed b# the previo!s
"or to initiali/e the inp!t eleents that were valid then, ret!rn "ro the
bod# o" the [*spServi&e() ethod to terinate "!rther pro&essing 9F
ret!rn:
>
TU
's there a +ay to re"erence the TthisT variable +ithin a JS& pa$e/
2es, there is( 4nder JSP 8(O, the page ipli&it ob*e&t is e'!ivalent to
=this=, and ret!rns a re"eren&e to the servlet generated b# the JSP page(
Ho+ )o ' per"orm bro+ser re)irection "rom a JS& pa$e/
2o! &an !se the response ipli&it ob*e&t to redire&t the browser to a
di""erent reso!r&e, asK
response-sen)Re)irect8Thttp*HH+++-e,"orsys-comHpathHerror-htmlT9
Q
2o! &an also ph#si&all# alter the Lo&ation -TTP header attrib!te, as
shown belowK
ST
response-setStat!s8HttpServletResponse-SCGMOVE:G&ERM%NE
NTLU9Q
String newLo&n < =FnewpathFinde+(htl=:
response(set-eader(=Lo&ation=,newLo&n):
TU
2o! &an also !se theK S*spK"orward page<=Fnewpage(*sp= FU .lso note
that #o! &an onl# !se this be"ore an# o!tp!t has been sent to the &lient( I
beleve this is the &ase with the response(send5edire&t() ethod as well(
I" #o! want to pass an# paraateres then #o! &an pass !sing
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
DD
WELCOME TO J2EE NOTES
C=sp*"or+ar) pa$eNTHservletHlo$inTE C=sp*param
nameNT!sernameT val!eNTH%R'T HE CH=sp*"or+ar)E
Ho+ )o ' incl!)e static "iles +ithin a JS& pa$e/
.nswer Stati& reso!r&es sho!ld alwa#s be in&l!ded !sing the JSP
in&l!de dire&tive( This wa#, the in&l!sion is per"ored *!st on&e d!ring
the translation phase( The "ollowing e+aple shows the s#nta+K
CDF incl!)e "ileNTcopyri$ht-htmlT DE
%o note that #o! sho!ld alwa#s s!ppl# a relative 45L "or the "ile
attrib!te( .ltho!gh #o! &an also in&l!de stati& reso!r&es !sing the
a&tion, this is not advisable as the in&l!sion is then per"ored "or ea&h
and ever# re'!est(
What JS& li"ecycle metho)s can ' overri)e/
2o! cannot override the [*spServi&e() ethod within a JSP page(
2o! &an however, override the *spInit() and *sp%estro#() ethods
within a JSP page( *spInit() &an be !se"!l "or allo&ating reso!r&es li@e
database &onne&tions, networ@ &onne&tions, and so "orth "or the JSP
page( It is good prograing pra&ti&e to "ree an# allo&ated reso!r&es
within *sp%estro#()(
The *spInit() and *sp%estro#() ethods are ea&h e+e&!ted *!st on&e
d!ring the li"e&#&le o" a JSP page and are t#pi&all# de&lared as JSP
de&larationsK
STP p!bli& void *spInit() ;
( ( ( >
TU
STP
p!bli& void *sp%estro#() ;
( ( ( >
TU
Can a JS& pa$e process HTML @ORM )ata/
2es( -owever, !nli@e servlets, #o! are not re'!ired to ipleent
-TTP)proto&ol spe&i"i& ethods li@e doGet() or doPost() within #o!r
JSP page( 2o! &an obtain the data "or the G35E inp!t eleents via the
re'!est ipli&it ob*e&t within a s&riptlet or e+pression asK
ST
String ite < re'!est(getParaeter(=ite=):
int howEan# < new Integer(re'!est(getParaeter(=!nits=))(intJal!e():
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
DI
WELCOME TO J2EE NOTES
TU
or
ST< re'!est(getParaeter(=ite=) TU
Ho+ )o ' mi, JS& an) SS' Vincl!)e/
I" #o!Hre *!st in&l!ding raw -TEL, !se the Cin&l!de dire&tive as !s!al
inside #o!r (*sp "ile(
SP))Cin&l!de "ile<=data(in&=))U
7!t itHs a little tri&@ier i" #o! want the server to eval!ate an# JSP &ode
thatHs inside the in&l!ded "ile( I" #o!r data(in& "ile &ontains *sp &ode #o!
will have to !se STX vin&l!de<=data(in&= TU The SP))Cin&l!de
"ile<=data(in&=))U is !sed "or in&l!ding non)JSP "iles(
Ho+ can ' implement a threa)(sa"e JS& pa$e/
2o! &an a@e #o!r JSPs thread)sa"e b# having the ipleent the
Sin$leThrea)Mo)el inter"a&e( This is done b# adding the dire&tive
CDF pa$e isThrea)Sa"eNT"alseT D E within #o!r JSP page(
Ho+ )o ' incl!)e static "iles +ithin a JS& pa$e/
Stati& reso!r&es sho!ld alwa#s be in&l!ded !sing the JSP in&l!de
dire&tive( This wa#, the in&l!sion is per"ored *!st on&e d!ring the
translation phase( The "ollowing e+aple shows the s#nta+K %o note
that #o! sho!ld alwa#s s!ppl# a relative 45L "or the "ile attrib!te(
.ltho!gh #o! &an also in&l!de stati& reso!r&es !sing the a&tion, this is
not advisable as the in&l!sion is then per"ored "or ea&h and ever#
re'!est(
Ho+ )o yo! prevent the Creation o" a Session in a JS& &a$e an)
+hy/
7# de"a!lt, a JSP page will a!toati&all# &reate a session "or the
re'!est i" one does not e+ist( -owever, sessions &ons!e reso!r&es and
i" it is not ne&essar# to aintain a session, one sho!ld not be &reated(
Gor e+aple, a ar@eting &apaign a# s!ggest the reader visit a web
page "or ore in"oration( I" it is anti&ipated that a lot o" tra""i& will hit
that page, #o! a# want to optii/e the load on the a&hine b# not
&reating !seless sessions(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
DM
WELCOME TO J2EE NOTES
What is the pa$e )irective is !se) to prevent a JS& pa$e "rom
a!tomatically creatin$ a session*
CDF pa$e sessionNT"alseTE
's it possible to share an HttpSession bet+een a JS& an) EJ/
What happens +hen ' chan$e a val!e in the HttpSession "rom insi)e
an EJ/
2o! &an pass the -ttpSession as paraeter to an 6J7 ethod,
onl# i" all ob*e&ts in session are seriali/able(This has to be &onsider as
=passed)b#)val!e=, that eans that itHs read)onl# in the 6J7( I" an#thing
is altered "ro inside the 6J7, it wonHt be re"le&ted ba&@ to the
-ttpSession o" the Servlet Container(The =pass)b#re"eren&e= &an be
!sed between 6J7s 5eote Inter"a&es, as the# are reote re"eren&es(
While it IS possible to pass an -ttpSession as a paraeter to an 6J7
ob*e&t, it is &onsidered to be =bad pra&ti&e (8)= in ters o" ob*e&t
oriented design( This is be&a!se #o! are &reating an !nne&essar#
&o!pling between ba&@)end ob*e&ts (e*bs) and "ront)end ob*e&ts
(-ttpSession)( Create a higher)level o" abstra&tion "or #o!r e*bHs api(
5ather than passing the whole, "at, -ttpSession (whi&h &arries with it a
b!n&h o" http seanti&s), &reate a &lass that a&ts as a val!e ob*e&t (or
str!&t!re) that holds all the data #o! need to pass ba&@ and "orth
between "ront)endFba&@)end( Consider the &ase where #o!r e*b needs to
s!pport a non)http)based &lient( This higher level o" abstra&tion will be
"le+ible eno!gh to s!pport it( (8) Core J?66 design patterns (?OO8)
Can a JS& pa$e instantiate a seriali>e) bean/
0o probleP The !se7ean a&tion spe&i"ies the bean0ae attrib!te,
whi&h &an be !sed "or indi&ating a seriali/ed bean( Gor e+apleK
S*spK!se7ean id<=shop= t#pe<=shopping(C%= bean0ae<=C%= FU
S*spKgetPropert# nae<=shop= propert#<=alb!= FU
. &o!ple o" iportant points to note( .ltho!gh #o! wo!ld have to nae
#o!r seriali/ed "ile ="ilenae(ser=, #o! onl# indi&ate ="ilenae= as the
val!e "or the bean0ae attrib!te( .lso, #o! will have to pla&e #o!r
seriali/ed "ile within the W67)I0G*spbeans dire&tor# "or it to be lo&ated
b# the JSP engine(
Can yo! ma#e !se o" a ServletO!tp!tStream ob=ect "rom +ithin a
JS& pa$e/
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
DQ
WELCOME TO J2EE NOTES
No- 2o! are s!pposed to a@e !se o" onl# a JSPWriter ob*e&t (given
to #o! in the "or o" the ipli&it ob*e&t o!t) "or repl#ing to &lients( .
JSPWriter &an be viewed as a b!""ered version o" the strea ob*e&t
ret!rned b# response(getWriter(), altho!gh "ro an ipleentational
perspe&tive, it is not( . page a!thor &an alwa#s disable the de"a!lt
b!""ering "or an# page !sing a page dire&tive asK
CDF pa$e b!""erNTnoneT DE
Can +e implements inter"ace or e,ten)s class in JS&/
No , we &anHt ipleents inter"a&e or e+tends &lass in JSP
What are the steps re?!ire) in a))in$ a JS& Ta$ Libraries/
8( Create a TL% "ile and &on"ig!re the re'!ired &lass In"oration(
?( Create the Java Ipleentation So!r&e e+tending the JSP Tag Lib
Class (TagS!pport)(
D( Copile and pa&@age it as loosed &lass "ile or as a *ar !nder lib "older
in Web .r&hive Gile "or Class loading(
I( Pla&e the TL% "ile !nder the W67)I0G "older(
M( .dd re"eren&e to the tag librar# in the web(+l "ile(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
DR
WELCOME TO J2EE NOTES
Str!ts .-.
8( Introd!&tion to EJC
a( 3verview o" EJC .r&hite&t!re QD
b( .ppl#ing EJC in Servlets and JSP
&( Jiew on JSP
d( JSP Eodel 8 .r&hite&t!re
e( JSP Eodel ? .r&hite&t!re
"( Liitation in traditional EJC approa&h
g( EJC Eodel ? .r&hite&t!re
h( The bene"its
i( .ppli&ation "low
?( 3verview o" Str!ts Graewor@ QQ
a( Introd!&tion to Str!ts Graewor@
b( Str!ts .r&hite&t!re
&( Gront Controller %esign Pattern
d( Controller servlet ) .&tionServlet
e( .&tion ob*e&ts
"( .&tion Gor ob*e&ts
g( .&tion appings
h( Con"ig!ring web(+l "ile and str!ts)&on"ig(+l "ile
D( Str!ts $ Jiew &oponents
a( Coposite Jiew
b( 7!ilding page "ro teplates
&( JspKin&l!de Js str!ts teplate e&hanis
d( 7ean tags
e( -tl tags
"( Logi& tags
g( Teplate tags
I( Str!ts $ Controller &oponents
a( .&tion &lasses
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
DV
WELCOME TO J2EE NOTES
b( .&tionServlet
&( Str!ts data so!r&e
M( .dvan&ed Str!ts
a( .&&essing .ppli&ation 5eso!r&e Gile
b( 4se o" To@ens
&( .&&essing Inde+ed properties
d( Gorward Js 5edire&t
e( %#nai& &reating .&tion Gorwards
Q( Str!ts 8(8
a( %#na.&tionGor
b( %#naJalidator.&tionGor
Jalidating Inp!t %ata
%e&larative approa&h
4sing Str!ts Jalidator
Con"ig!ring the Jalidator
Spe&i"#ing validation r!les
Client side validation
&( Pl!gins
d( I8V0 ('nternationali/atioN)
Spe&i"#ing a reso!r&e b!ndle
Generating Lo&ale spe&i"i& essages
e( Tiles
'ntro)!ction to MVC8Mo)el Vie+ Controler9
Str!ts K Str!ts is an open so!r&e "raewor@ "ro Ja@artha Pro*e&t
designed "or developing the web appli&ations with Java S65JL6T .PI
and Java Server Pages Te&hnologies(Str!ts &on"ors the Eodel Jiew
Controller design pattern( Str!ts pa&@age provides !ni"ied re!sable
&oponents (s!&h as a&tion servlet) to b!ild the !ser inter"a&e that &an
be applied to an# web &onne&tion( It en&o!rages so"tware developent
"ollowing the EJC design pattern(
Overvie+ o" MVC %rchitect!re
The EJC design pattern divides appli&ations into three &oponentsK
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
DW
WELCOME TO J2EE NOTES
The Mo)el aintains the state and data that the appli&ation
represents (
The Vie+ allows the displa# o" in"oration abo!t the odel to the
!ser(
The Controller allows the !ser to anip!late the appli&ation (
In Str!ts, the view is handled b# JSPs and presentation &oponents,
the odel is represented b# Java 7eans and the &ontroller !ses Servlets
to per"or its a&tion(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
IO
Users

UI Components
UI Process Components
Service Interfaces
Business
Workflows
Business
Components
Business
Entities
Data Access Logic
Components
Service Agents
Data Sources

Services

S
e
c
u
r
i t
y

p
e
r
a
t i
o
n
a
l

m
a
n
a
g
e
m
e
n
t
C
o
m
m
u
n
i
c
a
t i
o
n
WELCOME TO J2EE NOTES
7# developing a "ailiar Web)based shopping &art, #o!Hll learn how to
!tili/e the Eodel)Jiew)Controller (EJC) design pattern and tr!l#
separate presentation "ro &ontent when !sing Java Server Pages(
%pplyin$ MVC in Servlets an) JS&
Ean# web appli&ations are JSP)onl# or Servlets)onl#( With JSP,
Java &ode is ebedded in the -TEL &ode: with Servlets the Java &ode
&alls println ethods to generate the -TEL &ode( 7oth approa&hes have
their advantages and drawba&@s: Str!ts gathers their strengths to get the
best o" their asso&iation(
7elow #o! will "ind one e+aple on registration "or pro&essing !sing
EJC in Servlets and JSPK
1. In the above appli&ation 5eg(*sp a&t as view a&&epts IFP "ro
&lient and s!bits to Controller Servlet(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
I8
Reg_mast
er
Confirm.jsp Error.jsp
If()
If()
Controller Servlet
Reg JSP
User
WELCOME TO J2EE NOTES
2. Controller Servlet validates the "or data, i" valid, stores the data
into %7
3. 7ased on the validation and %7 operations Controller Servlet
de&ides to respond either Con"ir(*sp or 6rror(*sp to &lients
browser(
. When the 6rror(*sp is responded, the page !st in&l!de all the list
o" errors with detailed des&ription(
!. The above shown appli&ation ar&hite&t!re is the odel "or EJC(
". IG EJC Eodel ? wants to be ipleented in #o!r appli&ation
b!siness logi& and odel operations !st be separated "ro
&ontroller progra(
Vie+ on JS&
The earl# JSP spe&i"i&ation "ollows two approa&hes "or b!ilding
appli&ations !sing JSP te&hnolog#( These two approa&hes are &alled as
JSP Eodel 8 and JSP Eodel ? ar&hite&t!res(
JS& Mo)el . %rchitect!re

In Mo)el . architect!re the JSP page is alone responsible "or
pro&essing the in&oing re'!est and repl#ing ba&@ to the &lient( There is
still separation o" presentation "ro &ontent, be&a!se all data a&&ess is
per"ored !sing beans( .ltho!gh the JSP Eodel 8 .r&hite&t!re is ore
s!itable "or siple appli&ations, it a# not be desirable "or &ople+
ipleentations(
JS& Mo)el 2 %rchitect!re ) MVC
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
I?
WELCOME TO J2EE NOTES
The Mo)el 2 %rchitect!re is an approa&h "or serving d#nai&
&ontent, sin&e it &obines the !se o" both Servlets and JSP( It ta@es
advantages o" the predoinant strengths o" both te&hnologies, !sing JSP
to generate the presentation la#er and Servlets to per"or pro&ess)
intensive tas@s( -ere servlet a&ts as controller and is in &harge o"
re'!est pro&essing and the &reation o" an# beans or ob*e&ts !sed b# the
JSP as well as de&iding depending on the !sers a&tions, whi&h JSP page
to "orward the re'!est to( 0ote that there is no pro&essing logi& within
the JSP page itsel": it is sipl# responsible "or retrieving an# ob*e&ts or
beans that a# have been previo!sl# &reated b# the servlet, and
e+tra&ting the d#nai& &ontent "ro that servlet "or insertion within
stati& teplates(
Limitation in tra)itional MVC approach
The ain liitation in the traditional EJC approa&h is, in that
there is no separation o" b!siness logi& (validationF &onditionsF an#thing
related to b!siness r!les) "ro &ontroller (is responsible "or &ontrolling
o" the appli&ation "low b# !sing stati&Fd#nai& re'!est dispat&her(
MVC Mo)el 2 %rchitect!re is Eodel Jiew Controller
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
ID
Browser
Servlet
Controller
Servlet
Validator
Servlet
Model
JSP
View
Beans
1 2
3
4
5
6
7
User
Pass
Login
WELCOME TO J2EE NOTES
1. Client s!bits login re'!est to servlet appli&ation
2. Servlet appli&ation a&ts as &ontroller it "irst de&ides to re'!est
validator another servlet progra whi&h is responsible "or not n!ll
&he&@ing (b!siness r!le)
3. &ontrol &oes to &ontroller ba&@ and based on the validation
response, i" the response is positive, servlet &ontroller sends the
re'!est to odel
. Eodel re'!ests %7 to veri"# whether the database is having the
sae !ser nae and password, I" "o!nd login operation is
s!&&ess"!l
!. 7eans are !sed to store i" an# data retrieved "ro the database and
@ept into -TTPSession
". Controller then gives response ba&@ to response JSP (view) whi&h
!ses the bean ob*e&ts stored in -TTPSession ob*e&t
#. and prepares presentation response on to the browser
Overvie+ o" Str!ts @rame+or#
'ntro)!ction to Str!ts @rame+or#
The goal o" this pro*e&t is to provide an open so!r&e "raewor@ "or
b!ilding Java web appli&ations( The &ore o" the Str!ts "raewor@ is a
"le+ible &ontrol la#er based on standard te&hnologies li@e Java Servlets,
Java7eans, 5eso!r&e 7!ndles, and ^EL, as well as vario!s Ja@arta
Coons pa&@ages( Str!ts en&o!rages appli&ation ar&hite&t!res based
on the Eodel ? approa&h, a variation o" the &lassi& Mo)el(Vie+(
Controller (EJC) design paradig(
Str!ts provides its own Controller &oponent and integrates with
other te&hnologies to provide the Eodel and the Jiew(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
II
WELCOME TO J2EE NOTES
Gor the Mo)el, Str!ts &an intera&t with standard data a&&ess
te&hnologies, li@e J%7C and 6J7, as well as ost an# third)part#
pa&@ages, li@e -ibernate, i7.TIS, or 3b*e&t 5elational 7ridge(
Gor the Vie+, Str!ts wor@s well with Java Server Pages, in&l!ding
JSTL and JSG, as well as Jelo&it# Teplates, ^SLT, and other
presentation s#stes(
Gor Controller, .&tionServlet and .&tionEapping ) The 'ontroller
portion o" the appli&ation is "o&!sed on re&eiving re'!ests "ro the
&lient de&iding what b!siness logi& "!n&tion is to be per"ored, and
then delegating responsibilit# "or prod!&ing the ne+t phase o" the !ser
inter"a&e to an appropriate Jiew &oponent( In Str!ts, the priar#
&oponent o" the Controller is a servlet o" &lass .&tionServlet( This
servlet is &on"ig!red b# de"ining a set o" .&tionEappings( .n
.&tionEapping de"ines a path that is at&hed against the re'!est 45I
o" the in&oing re'!est, and !s!all# spe&i"ies the "!ll# '!ali"ied &lass
nae o" an .&tion &lass( .&tions en&aps!late the b!siness logi&,
interpret the o!t&oe, and !ltiatel# dispat&h &ontrol to the
appropriate Jiew &oponent to &reate the response(
The &truts pro2ect #as launched in May 6777 "y 'raig Mc'lanahan to
pro%ide a standard M8' frame#or to the 3a%a community. $n 3uly
6779.
In the EJC design pattern, appli&ation "low is ediated b# a
&entral Controller( The Controller delegates re'!ests ) in o!r &ase,
-TTP re'!ests ) to an appropriate handler( The handlers are tied to a
Eodel, and ea&h handler a&ts as an adapter between the re'!est and the
Eodel( The Eodel represents, or en&aps!lates, an appli&ationHs b!siness
logi& or state( Control is !s!all# then "orwarded ba&@ thro!gh the
Controller to the appropriate Jiew( The "orwarding &an be deterined
b# &ons!lting a set o" appings, !s!all# loaded "ro a database or
&on"ig!ration "ile( This provides a loose &o!pling between the Jiew and
Eodel, whi&h &an a@e appli&ations signi"i&antl# easier to &reate and
aintain(
Str!ts %rchitect!re
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
IM
5e'!est(*sp
Str!ts)
&on"ig(+l
J?66
Coponent
(6J7)
WELCOME TO J2EE NOTES
@ront Controller
Context
The presentation)tier re'!est handling e&hanis !st &ontrol and
&oordinate pro&essing o" ea&h !ser a&ross !ltiple re'!ests( S!&h
&ontrol e&haniss a# be anaged in either a &entrali/ed or
de&entrali/ed anner(
Problem
The s#ste re'!ires a &entrali/ed a&&ess point "or presentation)tier
re'!est handling to s!pport the integration o" s#ste servi&es, &ontent
retrieval, view anageent, and navigation( When the !ser a&&esses the
view dire&tl# witho!t going thro!gh a &entrali/ed e&hanis,
Two probles a# o&&!rK
6a&h view is re'!ired to provide its own s#ste servi&es, o"ten
res!lting in d!pli&ate &ode(
Jiew navigation is le"t to the views( This a# res!lt in &oingled
view &ontent and view navigation(
.dditionall#, distrib!ted &ontrol is ore di""i&!lt to aintain, sin&e
&hanges will o"ten need to be ade in n!ero!s pla&es(
Sol!tion *
4se a &ontroller as the initial point o" &onta&t "or handling a re'!est(
The &ontroller anages the handling o" the re'!est, in&l!ding invo@ing
se&!rit# servi&es s!&h as a!thenti&ation and a!thori/ation, delegating
b!siness pro&essing, anaging the &hoi&e o" an appropriate view,
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
IQ
.&tion
Servlet
.&tionGor
.&tion
S!&&ess
5esponse
6rror
5esponse
%7
Lega&
# &ode
WELCOME TO J2EE NOTES
handling errors, and anaging the sele&tion o" &ontent &reation
strategies(
The &ontroller provides a &entrali/ed entr# point that &ontrols and
anages Web re'!est handling( 7# &entrali/ing de&ision points and
&ontrols, the &ontroller also helps red!&e the ao!nt o" Java &ode, &alled
scriptlets, ebedded in the JavaServer Pages (JSP) page(

Centrali/ing &ontrol in the &ontroller and red!&ing b!siness logi& in
the view prootes &ode re!se a&ross re'!ests( It is a pre"erable approa&h
to the alternative)ebedding &ode in !ltiple views)be&a!se that
approa&h a# lead to a ore error)prone, re!se)b#)&op#) and)paste
environent(

T#pi&all#, a &ontroller &oordinates with a dispat&her &oponent(
%ispat&hers are responsible "or view anageent and navigation( Th!s,
a dispat&her &hooses the ne+t view "or the !ser and ve&tors &ontrol to the
reso!r&e( %ispat&hers a# be en&aps!lated within the &ontroller dire&tl#
or &an be e+tra&ted into a separate &oponent(
While the Gront Controller pattern s!ggests &entrali/ing the
handling o" all re'!ests, it does not liit the n!ber o" handlers in the
s#ste, as does a Singleton( .n appli&ation a# !se !ltiple &ontrollers
in a s#ste, ea&h apping to a set o" distin&t servi&es(
Structure
7elow "ig!re represents the Gront Controller &lass diagra pattern(
Figure: Front Controller class diagram
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
IR
WELCOME TO J2EE NOTES
Participants and Responsibilities
7elow "ig!re shows the se'!en&e diagra representing the Gront
Controller pattern( It depi&ts how the &ontroller handles a re'!est(
Figure: Front Controller sequence diagram
Controller 4 The &ontroller is the initial &onta&t point "or handling all
re'!ests in the s#ste( The &ontroller a# delegate to a helper to
&oplete a!thenti&ation and a!thori/ation o" a !ser or to initiate &onta&t
retrieval(
Dispatcher :
. dispat&her is responsible "or view anageent and navigation,
anaging the &hoi&e o" the ne+t view to present to the !ser, and
providing the e&hanis "or ve&toring &ontrol to this reso!r&e(
. dispat&her &an be en&aps!lated within a &ontroller or &an be a
separate &oponent wor@ing in &oordination( The dispat&her provides
either a stati& dispat&hing to the view or a ore sophisti&ated d#nai&
dispat&hing e&hanis(
The dispat&her !ses the 5e'!est %ispat&her ob*e&t (s!pported in the
servlet spe&i"i&ation) and en&aps!lates soe additional pro&essing(
Helper :
. helper is responsible "or helping a view or &ontroller &oplete its
pro&essing( Th!s, helpers have n!ero!s responsibilities, in&l!ding
gathering data re'!ired b# the view and storing this interediate odel,
in whi&h &ase the helper is soeties re"erred to as a val!e bean(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
IV
WELCOME TO J2EE NOTES
.dditionall#, helpers a# adapt this data odel "or !se b# the view(
-elpers &an servi&e re'!ests "or data "ro the view b# sipl# providing
a&&ess to the raw data or b# "oratting the data as Web &ontent(
. view a# wor@ with an# n!ber o" helpers, whi&h are t#pi&all#
ipleented as Java7eans &oponents (JSP 8(O\) and &!sto tags
(JSP 8(8\)( .dditionall#, a helper a# represent a Coand ob*e&t, a
delegate, or an ^SL Trans"orer, whi&h is !sed in &obination with a
st#lesheet to adapt and &onvert the odel into the appropriate "or(
View 4 . view represents and displa#s in"oration to the &lient( The
view retrieves in"oration "ro a odel( -elpers s!pport views b#
en&aps!lating and adapting the !nderl#ing data odel "or !se in the
displa#(
Controller Servlet W %ction Servlet
Gor those o" #o! "ailiar with EJC ar&hite&t!re, the .&tionServlet
represents the C ) the &ontroller( The *ob o" the &ontroller is toK
pro&ess !ser re'!ests,
deterine what the !ser is tr#ing to a&hieve a&&ording to the
re'!est,
p!ll data "ro the odel (i" ne&essar#) to be given to the
appropriate view, and
sele&t the proper view to respond to the !ser(
The Str!ts &ontroller delegates ost o" this gr!nt wor@ to the ,equest
Processor and .&tion &lasses(
In addition to being the "ront &ontroller "or #o!r appli&ation,
the .&tionServlet instan&e also is responsible "or initiali/ation and
&lean)!p o" reso!r&es( When the &ontroller initiali/es, it "irst loads the
appli&ation &on"ig &orresponding to the =&on"ig= init)para( It then goes
thro!gh an en!eration o" all init-param eleents, loo@ing "or those
eleents whoHs nae starts with config/( Gor ea&h o" these eleents,
Str!ts loads the &on"ig!ration "ile spe&i"ied b# the val!e o" that init-
param, and assigns a =pre"i+= val!e to that od!leHs Eod!leCon"ig
instan&e &onsisting o" the pie&e o" the init-param nae "ollowing
=&on"igF=( Gor e+aple, the od!le pre"i+ spe&i"ied b# the init-param
config/foo wo!ld be ="oo=( This is iportant to @now, sin&e this is how
the &ontroller deterines whi&h od!le will be given &ontrol o"
pro&essing the re'!est( To a&&ess the od!le "oo, #o! wo!ld !se a 45L
li@eK
httpKFFlo&alhostKVOVOF#.ppF"ooFsoe.&tion(do
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
IW
WELCOME TO J2EE NOTES
Gor ea&h re'!est ade o" the &ontroller, the ethod
process(HttpServletRequest, HttpServletResponse) will be &alled( This
ethod sipl# deterines whi&h od!le sho!ld servi&e the re'!est and
then invo@es that od!leHs 5e'!estPro&essorHs pro&ess ethod, passing
the sae re'!est and response(
Re?!est &rocessor *
The 5e'!estPro&essor is where the a*orit# o" the &ore pro&essing
o&&!rs "or ea&h re'!est( LetHs ta@e a loo@ at the helper "!n&tions the
pro&ess ethod invo@es in)t!rnK
pro&essPath
%eterine the path that invo@ed !s( This will be !sed
later to retrieve an .&tionEapping(
pro&essLo&ale
Sele&t a lo&ale "or this re'!est, i" one hasnHt alread#
been sele&ted, and pla&e it in the re'!est(
pro&essContent
Set the de"a!lt &ontent t#pe (with optional &hara&ter
en&oding) "or all responses i" re'!ested(
pro&ess0oCa&h
e
I" appropriate, set the "ollowing response headersK
=Praga=, =Ca&he)Control=, and =6+pires=(
pro&essPrepro&e
ss
This is one o" the =hoo@s= the 5e'!estPro&essor a@es
available "or s!b&lasses to override( The de"a!lt
ipleentation sipl# ret!rns tr!e( I" #o! s!b&lass
5e'!estPro&essor and override pro&essPrepro&ess #o!
sho!ld either ret!rn tr!e (indi&ating pro&ess sho!ld
&ontin!e pro&essing the re'!est) or "alse (indi&ating #o!
have handled the re'!est and the pro&ess sho!ld ret!rn)
pro&essEapping
%eterine the .&tionEapping asso&iated with this
path(
pro&ess5oles
I" the apping has a role asso&iated with it, ens!re the
re'!esting !ser is has the spe&i"ied role( I" the# do not,
raise an error and stop pro&essing o" the re'!est(
pro&ess.&tionG
or
Instantiate (i" ne&essar#) the .&tionGor asso&iated
with this apping (i" an#) and pla&e it into the
appropriate s&ope(
pro&essPop!late
Pop!late the .&tionGor asso&iated with this re'!est, i"
an#(
pro&essJalidate Per"or validation (i" re'!ested) on the .&tionGor
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
MO
WELCOME TO J2EE NOTES
asso&iated with this re'!est (i" an#)(
pro&essGorward
I" this apping represents a "orward, "orward to the
path spe&i"ied b# the apping(
pro&essIn&l!de
I" this apping represents an in&l!de, in&l!de the res!lt
o" invo@ing the path in this re'!est(
pro&ess.&tionC
reate
Instantiate an instan&e o" the &lass spe&i"ied b# the
&!rrent .&tionEapping (i" ne&essar#)(
pro&ess.&tionP
er"or
This is the point at whi&h #o!r a&tionHs per"or or e+e&!te
ethod will be &alled(
pro&essGorward
Con"ig
Ginall#, the pro&ess ethod o" the 5e'!estPro&essor
ta@es the .&tionGorward ret!rned b# #o!r .&tion &lass,
and !ses to sele&t the ne+t reso!r&e (i" an#)( Eost o"ten
the .&tionGorward leads to the presentation page that
renders the response(
%ction class
The Action &lass de"ines two ethods that &o!ld be e+e&!ted depending
on #o!r servlet environentK
p!bli& .&tionGorward e+e&!te(.&tionEapping apping,
.&tionGor "or,
Servlet5e'!est re'!est,
Servlet5esponse response)
throws 6+&eption:
p!bli& .&tionGorward e+e&!te(.&tionEapping apping,
.&tionGor "or,
-ttpServlet5e'!est re'!est,
-ttpServlet5esponse response)
throws 6+&eption:
Sin&e the a*orit# o" Str!ts pro*e&ts are "o&!sed on b!ilding web
appli&ations, ost pro*e&ts will onl# !se the =-ttpServlet5e'!est=
version( . non)-TTP e+e&!te() ethod has been provided "or
appli&ations that are not spe&i"i&all# geared towards the -TTP proto&ol(
The goal o" an Action &lass is to pro&ess a re'!est, via its execute ethod,
and ret!rn an ActionForward ob*e&t that identi"ies where &ontrol sho!ld be
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
M8
WELCOME TO J2EE NOTES
"orwarded (e(g( a JSP, Tile de"inition, Jelo&it# teplate, or another
.&tion) to provide the appropriate response( In the EJCFEodel ?
design pattern, a t#pi&al Action &lass will o"ten ipleent logi& li@e the
"ollowing in its execute ethodK
Jalidate the &!rrent state o" the !serHs session ("or e+aple,
&he&@ing that the !ser has s!&&ess"!ll# logged on)( I" the .&tion
&lass "inds that no logon e+ists, the re'!est &an be "orwarded to
the presentation page that displa#s the !sernae and password
propts "or logging on( This &o!ld o&&!r be&a!se a !ser tried to
enter an appli&ation =in the iddle= (sa#, "ro a boo@ar@), or
be&a!se the session has tied o!t, and the servlet &ontainer
&reated a new one(
I" validation is not &oplete, validate the "or bean properties as
needed( I" a proble is "o!nd, store the appropriate error essage
@e#s as a re'!est attrib!te, and "orward &ontrol ba&@ to the inp!t
"or so that the errors &an be &orre&ted(
Per"or the pro&essing re'!ired to deal with this re'!est (s!&h as
saving a row into a database)( This &an be done b# logi& &ode
ebedded within the .&tion &lass itsel", b!t sho!ld generall# be
per"ored b# &alling an appropriate ethod o" a b!siness logi&
bean(
4pdate the server)side ob*e&ts that will be !sed to &reate the ne+t
page o" the !ser inter"a&e (t#pi&all# re'!est s&ope or session s&ope
beans, depending on how long #o! need to @eep these ites
available)(
5et!rn an appropriate .&tionGorward ob*e&t that identi"ies the
presentation page to be !sed to generate this response, based on
the newl# !pdated beans( T#pi&all#, #o! will a&'!ire a re"eren&e
to s!&h an ob*e&t b# &alling "indGorward on either the .&tionEapping ob*e&t
#o! re&eived (i" #o! are !sing a logi&al nae lo&al to this
apping), or on the &ontroller servlet itsel" (i" #o! are !sing a
logi&al nae global to the appli&ation)(
In Str!ts 8(O, .&tions &alled a per"or ethod instead o" the now)
pre"erred e+e&!te ethod( These ethods !se the sae paraeters and
di""er onl# in whi&h e+&eptions the# throw( The elder per"or ethod
throws Serlvet6+&eption and I36+&eption( The new e+e&!te ethod
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
M?
WELCOME TO J2EE NOTES
sipl# throws 6+&eption( The &hange was to "a&ilitate the %e&larative
6+&eption handling "eat!re introd!&ed in Str!ts 8(8(
The per"or ethod a# still be !sed in Str!ts 8(8 b!t is depre&ated(
The Str!ts 8(8 ethod sipl# &alls the new e+e&!te ethod and wraps
an# 6+&eption thrown as a Servlet6+&eption(
%ction @orm class
.n .&tionGor represents an -TEL "or that the !ser intera&ts with
over one or ore pages( 2o! will provide properties to hold the state o"
the "or with getters and setters to a&&ess the( .&tionGors &an be
stored in either the session (de"a!lt) or re'!est s&opes( I" the#Hre in the
session itHs iportant to ipleent the "orHs reset ethod to initiali/e
the "or be"ore ea&h !se( Str!ts sets the .&tionGorHs properties "ro
the re'!est paraeters and sends the validated "or to the appropriate
.&tionHs e+e&!te ethod(
When #o! &ode #o!r ActionForm beans, @eep the "ollowing prin&iples in
indK
The .&tionGor &lass itsel" re'!ires no spe&i"i& ethods to be
ipleented( It is !sed to identi"# the role these parti&!lar beans
pla# in the overall ar&hite&t!re( T#pi&all#, an .&tionGor bean
will have onl# propert# getter and propert# setter ethods, with
no b!siness logi&(
The .&tionGor ob*e&t also o""ers a standard validation
e&hanis( I" #o! override a =st!b= ethod, and provide error
essages in the standard appli&ation reso!r&e, Str!ts will
a!toati&all# validate the inp!t "ro the "or (!sing #o!r
ethod)( See =.!toati& Gor Jalidation= "or details( 3" &o!rse,
#o! &an also ignore the .&tionGor validation and provide #o!r
own in the .&tion ob*e&t(
%e"ine a propert# (with asso&iated get^++ and set^++ ethods)
"or ea&h "ield that is present in the "or( The "ield nae and
propert# nae !st at&h a&&ording to the !s!al Java7eans
&onventions (see the Javado& "or the *ava(beans(Introspe&tor &lass
"or a start on in"oration abo!t this)( Gor e+aple, an inp!t "ield
naed !sernae will &a!se the set4sernae ethod to be &alled(
7!ttons and other &ontrols on #o!r "or &an also be de"ined as
properties( This &an help deterine whi&h b!tton or &ontrol was
sele&ted when the "or was s!bitted( 5eeber, the
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
MD
WELCOME TO J2EE NOTES
.&tionGor is eant to represent #o!r data)entr# "or, not *!st
the data beans(
Thin@ o" #o!r .&tionGor beans as a "irewall between -TTP and
the .&tion( 4se the validate ethod to ens!re all re'!ired
properties are present, and that the# &ontain reasonable val!es( .n
.&tionGor that "ails validation will not even be presented to the
.&tion "or handling(
2o! a# also pla&e a bean instan&e on #o!r "or, and !se nested
propert# re"eren&es( Gor e+aple, #o! ight have a =&!stoer=
bean on #o!r .&tionGor, and then re"er to the propert#
=&!stoer(nae= in #o!r presentation page( This wo!ld
&orrespond to the ethods &!stoer(get0ae() and &!stoer(set0ae(string 0ae)
on #o!r &!stoer bean( See the Tag Librar# %eveloper G!ides "or
ore abo!t !sing nested s#nta+ with the Str!ts JSP tags(
Ca!tionK I" #o! nest an e+isting bean instan&e on #o!r "or, thin@
abo!t the properties it e+poses( .n# p!bli& propert# on an
.&tionGor that a&&epts a single String val!e &an be set with a
'!er# string( It a# be !se"!l to pla&e beans that &an a""e&t the
b!siness state inside a thin =wrapper= that e+poses onl# the
properties re'!ired( This wrapper &an also provide a "ilter to be
s!re r!ntie properties are not set to inappropriate val!es(
%ction class :esi$n $!i)elines
5eeber the "ollowing design g!idelines when &oding Action &lassesK
Write co)e "or a m!lti(threa)e) environment ) The &ontroller
servlet &reates only one instance o" yo!r %ction class, and !ses this one
instan&e to servi&e all re'!ests( Th!s, #o! need to write thread)sa"e
.&tion &lasses( Gollow the sae g!idelines #o! wo!ld !se to write
thread)sa"e Servlets( -ere are two general g!idelines that will help #o!
write s&alable, thread)sa"e .&tion &lassesK
o Only Ase Local Variables ) The ost iportant prin&iple that aids in
thread)sa"e &oding is to !se onl# lo&al variables, not instance
variables, in #o!r .&tion &lass( Lo&al variables are &reated on a sta&@ that
is assigned (b# #o!r JJE) to ea&h re'!est thread, so there is no need to
worr# abo!t sharing the( .n .&tion &an be "a&tored into several lo&al
ethods, so long as all variables needed are passed as ethod
paraeters( This ass!res thread sa"et#, as the JJE handles s!&h
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
MI
WELCOME TO J2EE NOTES
variables internall# !sing the &all sta&@ whi&h is asso&iated with a
single Thread(
o Conserve Reso!rces ) .s a general r!le, allo&ating s&ar&e reso!r&es
and @eeping the a&ross re'!ests "ro the sae !ser (in the !serHs
session) &an &a!se s&alabilit# probles( Gor e+aple, i" #o!r
appli&ation !ses J%7C and #o! allo&ate a separate J%7C &onne&tion "or
ever# !ser, #o! are probabl# going to r!n in soe s&alabilit# iss!es
when #o!r site s!ddenl# shows !p on Slashdot( 2o! sho!ld strive to
!se pools and release reso!r&es (s!&h as database &onne&tions) prior to
"orwarding &ontrol to the appropriate Jiew &oponent )) even i" a bean
ethod #o! have &alled throws an e+&eption(
:onBt thro+ it< catch itJ ) 6ver !sed a &oer&ial website onl# to
have a sta&@ tra&e or e+&eption thrown in #o!r "a&e a"ter #o!Hve alread#
t#ped in #o!r &redit &ard n!ber and &li&@ed the p!r&hase b!tton? LetHs
*!st sa# it doesnHt inspire &on"iden&e( 0ow is #o!r &han&e to deal with
these appli&ation errors ) in the .&tion &lass( I" #o!r appli&ation spe&i"i&
&ode throws e+pe&tions #o! sho!ld &at&h these e+&eptions in #o!r
.&tion &lass, log the in #o!r appli&ationHs log (servlet(log(=6rror essage=,
e+&eption)) and ret!rn the appropriate .&tionGorward(
It is wise to avoid &reating length# and &ople+ .&tion &lasses( I" #o!
start to ebed too !&h logi& in the .&tion &lass itsel", #o! will begin
to "ind the .&tion &lass hard to !nderstand, aintain, and ipossible to
re!se( 5ather than &reating overl# &ople+ .&tion &lasses, it is
generall# a good pra&ti&e to ove ost o" the persisten&e, and =b!siness
logi&= to a separate appli&ation la#er( When an .&tion &lass be&oes
length# and pro&ed!ral, it a# be a good tie to re"a&tor #o!r
appli&ation ar&hite&t!re and ove soe o" this logi& to another
&on&ept!al la#er: otherwise, #o! a# be le"t with an in"le+ible
appli&ation whi&h &an onl# be a&&essed in a web)appli&ation
environent( Str!ts sho!ld be viewed as sipl# the "o!n)ation "or
ipleenting EJC in #o!r appli&ations( Str!ts provides #o! with a
!se"!l &ontrol la#er, b!t it is not a "!ll# "eat!red plat"or "or b!ilding
EJC appli&ations, so!p to n!ts(
The Eail5eader e+aple appli&ation in&l!ded with Str!ts stret&hes this
design prin&iple soewhat, be&a!se the b!siness logi& itsel" is
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
MM
WELCOME TO J2EE NOTES
ebedded in the Action &lasses( This sho!ld be &onsidered soething o"
a b!g in the design o" the e+aple, rather than an intrinsi& "eat!re o" the
Str!ts ar&hite&t!re, or an approa&h to be e!lated( In order to
deonstrate, in siple ters, the di""erent wa#s Str!ts &an be !sed, the
Eail5eader appli&ation does not alwa#s "ollow best pra&ti&es(
%ction mappin$ implementation
In order to operate s!&&ess"!ll#, the Str!ts &ontroller servlet needs to
@now several things abo!t how ea&h re'!est 45I sho!ld be apped to
an appropriate Action &lass( The re'!ired @nowledge has been
en&aps!lated in a Java &lass naed 5ctionMapping, the ost iportant
properties are as "ollowsK
o t#pe ) G!ll# '!ali"ied Java &lass nae o" the .&tion ipleentation
&lass !sed b# this apping(
o nae ) The nae o" the "or bean de"ined in the &on"ig "ile that this
a&tion will !se(
o path ) The re'!est 45I path that is at&hed to sele&t this apping( See
below "or e+aples o" how at&hing wor@s and how to !se wild&ards
to at&h !ltiple re'!est 45Is(
o !n@nown ) Set to tr!e i" this a&tion sho!ld be &on"ig!red as the de"a!lt
"or this appli&ation, to handle all re'!ests not handled b# another
a&tion( 3nl# one a&tion &an be de"ined as a de"a!lt within a single
appli&ation(
o validate ) Set to tr!e i" the validate ethod o" the a&tion asso&iated with this
apping sho!ld be &alled(
o "orward ) The re'!est 45I path to whi&h &ontrol is passed when this
apping is invo@ed( This is an alternative to de&laring a t#pe propert#(
Writin$ %ction Mappin$s
-ow does the &ontroller servlet learn abo!t the appings #o! want?
It wo!ld be possible (b!t tedio!s) to write a sall Java &lass that sipl#
instantiated new .&tionEapping instan&es, and &alled all o" the
appropriate setter ethods( To a@e this pro&ess easier, Str!ts !ses the
Ja@arta Coons %igester &oponent to parse an ^EL)based
des&ription o" the desired appings and &reate the appropriate ob*e&ts
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
MQ
WELCOME TO J2EE NOTES
initiali/ed to the appropriate de"a!lt val!es( See the Ja@arta Coons
website "or ore in"oration abo!t the %igester(
The developerHs responsibilit# is to &reate an ^EL "ile naed str!ts)
&on"ig(+l and pla&e it in the W67)I0G dire&tor# o" #o!r appli&ation(
This "orat o" this do&!ent is des&ribed b# the %o&!ent T#pe
%e"inition (%T%) aintained at
httpKFF*a@arta(apa&he(orgFstr!tsFdtdsFstr!ts)&on"ig[8[?(dtd( This &hapter
&overs the &on"ig!ration eleents that #o! will t#pi&all# write as part o"
developing #o!r appli&ation( There are several other eleents that &an
be pla&ed in the str!ts)&on"ig "ile to &!stoi/e #o!r appli&ation( See
=Con"ig!ring .ppli&ations= "or ore abo!t the other eleents in the
Str!ts &on"ig!ration "ile(
The &ontroller !ses an internal &op# o" this do&!ent to parse the
&on"ig!ration: an Internet &onne&tion is not re'!ired "or operation(
The o!terost ^EL eleent !st be struts-config!( Inside o" the
Sstr!ts)&on"igU eleent, there are three iportant eleents that are !sed
to des&ribe #o!r a&tionsK
S"or)beansU
Sglobal)"orwardsU
Sa&tion)appingsU
C"orm(beansE
This se&tion &ontains #o!r "or bean de"initions( Gor beans are
des&riptors that are !sed to &reate .&tionGor instan&es at r!ntie( 2o!
!se a S"or)beanU eleent "or ea&h "or bean, whi&h has the "ollowing
iportant attrib!tesK
naeK . !ni'!e identi"ier "or this bean, whi&h will be !sed to
re"eren&e it in &orresponding a&tion appings( 4s!all#, this is also
the nae o" the re'!est or session attrib!te !nder whi&h this "or
bean will be stored(
t#peK The "!ll#)'!ali"ied Java &lassnae o" the .&tionGor
s!b&lass to !se with this "or bean(
C$lobal("or+ar)sE
This se&tion &ontains #o!r global "orward de"initions( Gorwards are
instan&es o" the .&tionGorward &lass ret!rned "ro an .&tionGorHs
execute ethod( These ap logi&al naes to spe&i"i& reso!r&es (t#pi&all#
JSPs), allowing #o! to &hange the reso!r&e witho!t &hanging re"eren&es
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
MR
WELCOME TO J2EE NOTES
to it thro!gho!t #o!r appli&ation( 2o! !se a forward! eleent "or ea&h
"orward de"inition, whi&h has the "ollowing iportant attrib!tesK
naeK The logi&al nae "or this "orward( This is !sed in #o!r
.&tionGorHs e+e&!te ethod to "orward to the ne+t appropriate
reso!r&e( 6+apleK hoepage
pathK The &onte+t relative path to the reso!r&e( 6+apleK Finde+(*sp
or Finde+(do
redire&tK Tr!e or "alse (de"a!lt)( Sho!ld the .&tionServlet redire&t to
the reso!r&e instead o" "orward?
Caction(mappin$sE
This se&tion &ontains #o!r a&tion de"initions( 2o! !se an action!
eleent "or ea&h o" the appings #o! wo!ld li@e to de"ine( Eost a&tion
eleents will de"ine at least the "ollowing attrib!tesK
pathK The appli&ation &onte+t)relative path to the a&tion(
t#peK The "!ll# '!ali"ied *ava &lassnae o" #o!r .&tion &lass(
naeK The nae o" #o!r S"or)beanU eleent to !se with this a&tion
3ther o"ten)!sed attrib!tes in&l!deK
paraeterK . general)p!rpose attrib!te o"ten !sed b# =standard=
.&tions to pass a re'!ired propert#(
rolesK . &oa)deliited list o" the !ser se&!rit# roles that &an
a&&ess this apping(
Gor a &oplete des&ription o" the eleents that &an be !sed with the
action eleent, see the Str!ts Con"ig!ration %T% and the
.&tionEapping do&!entation(
%ction Mappin$ E,ample
-ereHs a apping entr# based on the Eail5eader e+aple
appli&ation( The Eail5eader appli&ation now !ses %#na.&tionGors(
7!t in this e+aple, weHll show a &onventinal .&tionGor instead, to
ill!strate the !s!al wor@"low( 0ote that the entries "or all the other
a&tions are le"t o!tK
Cstr!ts(con"i$E
<form-beans>
S"or)bean
nae<=logonGor=
t#pe<=org(apa&he(str!ts(webapp(e+aple(LogonGor= FU
</form-beans>
<global-forwards
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
MV
WELCOME TO J2EE NOTES
t#pe<=org(apa&he(str!ts(a&tion(.&tionGorward=U
S"orward
nae<=logon=
path<=Flogon(*sp=
redire&t<="alse= FU
CH$lobal("or+ar)sE
Caction(mappin$sE
Sa&tion
path <=Flogon=
t#pe <=org(apa&he(str!ts(webapp(e+aple(Logon.&tion=
nae <=logonGor=
s&ope <=re'!est=
inp!t <=Flogon(*sp=
!n@nown<="alse=
validate<=tr!e= FU
</action-mappings>
</struts-config>
Girst the "or bean is de"ined( . basi& bean o" &lass
=org(apa&he(str!ts(webapp(e+aple(LogonGor= is apped to the
logi&al nae =logonGor=( This nae is !sed as a re'!est attrib!te
nae "or the "or bean(
The =global)"orwards= se&tion is !sed to &reate logi&al nae
appings "or &oonl# !sed presentation pages( 6a&h o" these
"orwards is available thro!gh a &all to #o!r a&tion apping instan&e, i(e(
apping("indGorward(=logi&al0ae=)(
.s #o! &an see, this apping at&hes the path Flogon (a&t!all#,
be&a!se the Eail5eader e+aple appli&ation !ses e+tension apping,
the re'!est 45I #o! spe&i"# in a JSP page wo!ld end in Flogon(do)(
When a re'!est that at&hes this path is re&eived, an instan&e o" the
Logon5ction &lass will be &reated (the "irst tie onl#) and !sed( The
&ontroller servlet will loo@ "or a bean in re'!est s&ope !nder @e#
logonGor, &reating and saving a bean o" the spe&i"ied &lass i" needed(
3ptional b!t ver# !se"!l are the lo&al =forward= eleents( In the
Eail5eader e+aple appli&ation, an# a&tions in&l!de a lo&al =s!&&ess=
andFor ="ail!re= "orward as part o" an a&tion apping(
SP)) 6dit ail s!bs&ription ))U
Sa&tion
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
MW
WELCOME TO J2EE NOTES
path<=FeditS!bs&ription=
t#pe<=org(apa&he(str!ts(webapp(e+aple(6ditS!bs&ription.&tion=
nae<=s!bs&riptionGor=
s&ope<=re'!est=
validate<="alse=U
S"orward
nae<="ail!re=
path<=FainEen!(*sp=FU
S"orward
nae<=s!&&ess=
path<=Fs!bs&ription(*sp=FU
SFa&tionU
4sing *!st these two e+tra properties, the .&tion &lasses are alost
totall# independent o" the a&t!al naes o" the presentation pages( The
pages &an be renaed ("or e+aple) d!ring a redesign, with negligible
ipa&t on the .&tion &lasses theselves( I" the naes o" the =ne+t=
pages were hard &oded into the .&tion &lasses, all o" these &lasses wo!ld
also need to be odi"ied( 3" &o!rse, #o! &an de"ine whatever lo&al
"orward properties a@es sense "or #o!r own appli&ation(
The Str!ts &on"ig!ration "ile in&l!des several other eleents that #o!
&an !se to &!stoi/e #o!r appli&ation( See =Con"ig!ring .ppli&ations=
"or details(
Asin$ %ction Mappin$ "or pa$es
Gronting #o!r pages with .&tionEappings is essential when !sing
od!les, sin&e doing so is the onl# wa# #o! involve the &ontroller in the
re'!est )) and #o! want toP The &ontroller p!ts the appli&ation
&on"ig!ration in the re'!est, whi&h a@es available all o" #o!r od!le)
spe&i"i& &on"ig!ration data (in&l!ding whi&h essage reso!r&es #o! are
!sing, re'!est)pro&essor, dataso!r&es, and so "orth)(
The siplest wa# to do this is to !se the forward propert# o" the
.&tionEappingK
Sa&tion path<=Fview= "orward<=Fview(*sp=FU
Con"i$!rin$ str!ts(con"i$-,ml "ile
The 7!ilding Controller Coponents &hapter &overed writing the
"or)bean and a&tion)apping portions o" the Str!ts &on"ig!ration "ile(
These eleents !s!all# pla# an iportant role in the developent o" a
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
QO
WELCOME TO J2EE NOTES
Str!ts appli&ation( The other eleents in Str!ts &on"ig!ration "ile tend to
be stati&K #o! set the on&e and leave the alone(
These =stati&= &on"ig!ration eleents areK
&ontroller
essage)reso!r&es
pl!g)in
data)so!r&es
Controller con"i$!ration
The controller! eleent allows #o! to &on"ig!re the
.&tionServlet( Ean# o" the &ontroller paraeters were previo!sl#
de"ined b# servlet initiali/ation paraeters in #o!r we"#xml "ile b!t have
been oved to this se&tion o" struts-config#xml in order to allow di""erent
od!les in the sae web appli&ation to be &on"ig!red di""erentl#( Gor
"!ll details on available paraeters see the str!ts)&on"ig[8[?(dtd or the
list below(
b!""erSi>e ) The si/e (in b#tes) o" the inp!t b!""er !sed when
pro&essing "ile !ploads( `IOWQa (optional)
className ) Classnae o" &on"ig!ration bean(
`org(apa&he(str!ts(&on"ig(ControllerCon"iga (optional)
contentType ) %e"a!lt &ontent t#pe (and optional &hara&ter
en&oding) to be set on ea&h response( Ea# be overridden b# the
.&tion, JSP, or other reso!r&e to whi&h the re'!est is "orwarded(
`te+tFhtla (optional)
"or+ar)&attern ) 5epla&eent pattern de"ining how the =path=
attrib!te o" a S"orwardU eleent is apped to a &onte+t)relative 45L
when it starts with a slash (and when the &onte+t5elative propert# is
"alse)( This val!e a# &onsist o" an# &obination o" the "ollowingK
o :M ) 5epla&ed b# the od!le pre"i+ o" this od!le(
o :P ) 5epla&ed b# the =path= attrib!te o" the sele&ted S"orwardU
eleent(
o :: ) Ca!ses a literal dollar sign to be rendered(
o :( ) (Where =+= is an# &hara&ter not de"ined above) Silentl#
swallowed, reserved "or "!t!re !se(
I" not spe&i"ied, the de"a!lt "orwardPattern is &onsistent with the
previo!s behavior o" "orwards( `bEbPa (optional)
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
Q8
WELCOME TO J2EE NOTES
inp!t@or+ar) ) Set to tr!e i" #o! want the inp!t attrib!te o" Sa&tionU
eleents to be the nae o" a lo&al or global .&tionGorward, whi&h will
then be !sed to &al&!late the !ltiate 45L( Set to "alse to treat the
inp!t paraeter o" Sa&tionU eleents as a od!le)relative path to the
reso!r&e to be !sed as the inp!t "or( `"alsea (optional)
locale ) Set to tr!e i" #o! want a Lo&ale ob*e&t stored in the !serHs
session i" not alread# present( `tr!ea (optional)
ma,@ileSi>e ) The a+i! si/e (in b#tes) o" a "ile to be
a&&epted as a "ile !pload( Can be e+pressed as a n!ber "ollowed
b# a =1=, =E=, or =G=, whi&h are interpreted to ean @ilob#tes,
egab#tes, or gigab#tes, respe&tivel#( `?MOEa (optional)
m!ltipartClass ) The "!ll# '!ali"ied Java &lass nae o" the
!ltipart re'!est handler &lass to be !sed with this od!le(
`org(apa&he(str!ts(!pload(CoonsE!ltipart5e'!est-andlera
(optional)
nocache ) Set to tr!e i" #o! want the &ontroller to add -TTP
headers "or de"eating &a&hing to ever# response "ro this od!le(
`"alsea (optional)
pa$e&attern ) 5epla&eent pattern de"ining how the page attrib!te
o" &!sto tags !sing it is apped to a &onte+t)relative 45L o" the
&orresponding reso!r&e( This val!e a# &onsist o" an#
&obination o" the "ollowingK
o :M ) 5epla&ed b# the od!le pre"i+ o" this od!le(
o :P ) 5epla&ed b# the =path= attrib!te o" the sele&ted S"orwardU
eleent(
o :: ) Ca!ses a literal dollar sign to be rendered(
o :( ) (Where =+= is an# &hara&ter not de"ined above) Silentl#
swallowed, reserved "or "!t!re !se(
I" not spe&i"ied, the de"a!lt pagePattern is &onsistent with the
previo!s behavior o" 45L &al&!lation( `bEbPa (optional)
processorClass ) The "!ll# '!ali"ied Java &lass nae o" the
5e'!estPro&essor s!b&lass to be !sed with this od!le(
`org(apa&he(str!ts(a&tion(5e'!estPro&essora (optional)
temp:ir ) Teporar# wor@ing dire&tor# to !se when pro&essing
"ile !ploads( `;the dire&tor# provided b# the servlet &ontainer>a
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
Q?
WELCOME TO J2EE NOTES
This e+aple !ses the de"a!lt val!es "or several &ontroller paraeters(
I" #o! onl# want de"a!lt behavior #o! &an oit the &ontroller se&tion
altogether(
S&ontroller
pro&essorClass<=org(apa&he(str!ts(a&tion(5e'!estPro&essor=
deb!g<=O=
&ontentT#pe<=te+tFhtl=FU:
Messa$e Reso!rce con"i$!ration
Str!ts has b!ilt in s!pport "or internationali/ation (I8V0)( 2o! &an
de"ine one or ore message-resources! eleents "or #o!r webapp:
od!les &an de"ine their own reso!r&e b!ndles( %i""erent b!ndles &an
be !sed si!ltaneo!sl# in #o!r appli&ation, the H@e#H attrib!te is !sed to
spe&i"# the desired b!ndle(
&lass0ae ) Classnae o" &on"ig!ration bean(
`org(apa&he(str!ts(&on"ig(Eessage5eso!r&esCon"iga (optional)
"a&tor# ) Classnae o" Eessage5eso!r&esGa&tor#(
`org(apa&he(str!ts(!til(Propert#Eessage5eso!r&esGa&tor#a
(optional)
@e# ) ServletConte+t attrib!te @e# to store this b!ndle(
`org(apa&he(str!ts(a&tion(E6SS.G6a (optional)
n!ll ) Set to "alse to displa# issing reso!r&e @e#s in #o!r
appli&ation li@e H;;;eyname;;;H instead o" n!ll( `tr!ea (optional)
paraeter ) 0ae o" the reso!r&e b!ndle( (re'!ired)
6+aple &on"ig!rationK
Sessage)reso!r&es
paraeter<=E#Web.pp5eso!r&es=
n!ll<="alse= FU
This wo!ld set !p a essage reso!r&e b!ndle provided in the "ile
$%&e"AppResources#properties !nder the de"a!lt @e#( Eissing reso!r&e @e#s
wo!ld be displa#ed as H;;;eyname;;;H(
&l!$'n con"i$!ration
Str!ts Pl!gIns are &on"ig!red !sing the Spl!g)inU eleent within the
Str!ts &on"ig!ration "ile( This eleent has onl# one valid attrib!te,
H&lass0aeH, whi&h is the "!ll# '!ali"ied nae o" the Java &lass whi&h
ipleents the org(apa&he(str!ts(a&tion(Pl!gIn inter"a&e(
Gor Pl!gIns that re'!ire &on"ig!ration theselves, the nested Sset)
propert#U eleent is available(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
QD
WELCOME TO J2EE NOTES
This is an e+aple !sing the Tiles pl!ginK
Spl!g)in &lass0ae<=org(apa&he(str!ts(tiles(TilesPl!gin= U
Sset)propert#
propert#<=de"initions)&on"ig=
val!e<=FW67)I0GFtiles)de"s(+l=FU
SFpl!g)inU
:ataSo!rce con"i$!ration
7esides the ob*e&ts related to de"ining .&tionEappings, the Str!ts
&on"ig!ration a# &ontain eleents that &reate other !se"!l ob*e&ts(
The Sdata)so!r&esU se&tion &an be !sed to spe&i"# a &olle&tion o"
%ataSo!r&es `*ava+(s'l(%ataSo!r&ea "or the !se o" #o!r appli&ation(
T#pi&all#, a %ataSo!r&e represents a &onne&tion pool to a database or
other persistent store( .s a &onvenien&e, the Str!ts %ataSo!r&e anager
&an be !sed to instantiate whatever standard pool #o!r appli&ation a#
need( 3" &o!rse, i" #o!r persisten&e la#er provides "or its own
&onne&tions, then #o! do not need to spe&i"# a data)so!r&es eleent(
Sin&e %ataSo!r&e ipleentations var# in what properties need to be
set, !nli@e other Str!ts &on"ig!ration eleents, the data-source eleent
does not pre)de"ine a slate o" properties( Instead, the generi& set-propert%
"eat!re is !sed to set whatever properties #o!r ipleentation a#
re'!ire( T#pi&all#, these settings wo!ld in&l!deK
. driver &lass nae
. !rl to a&&ess the driver
. des&ription
.nd other s!ndr# properties(
Sdata)so!r&e t#pe<=org(apa&he(&oons(db&p(7asi&%ataSo!r&e=U
SP)) ((( set)propert# eleents ((( ))U
SFdata)so!r&eU
In Str!ts 8(?(O, the Generi&%ataSo!r&e has been reoved, and it is
re&oended that #o! !se the Coons 7asi&%ataSo!r&e or other
%ataSo!r&e ipleentation instead( In pra&ti&e, i" #o! need to !se the
%ataSo!r&e anager, #o! sho!ld !se whatever %ataSo!r&e
ipleentation wor@s best with #o!r &ontainer or database(
Gor e+aples o" spe&i"#ing a data)so!r&es eleent and !sing the
%ataSo!r&e with an .&tion,
The Str!ts con"i$!ration "ile
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
QI
WELCOME TO J2EE NOTES
The 7!ilding Controller Coponents &hapter &overed writing the "or)
bean and a&tion)apping portions o" the Str!ts &on"ig!ration "ile( These
eleents !s!all# pla# an iportant role in the developent o" a Str!ts
appli&ation( The other eleents in Str!ts &on"ig!ration "ile tend to be
stati&K #o! set the on&e and leave the alone(
These =stati&= &on"ig!ration eleents areK
&ontroller
essage)reso!r&es
pl!g)in
data)so!r&es
Controller Con"i$!ration
The controller! eleent allows #o! to &on"ig!re the .&tionServlet(
Ean# o" the &ontroller paraeters were previo!sl# de"ined b# servlet
initiali/ation paraeters in #o!r we"#xml "ile b!t have been oved to this
se&tion o" struts-config#xml in order to allow di""erent od!les in the
sae web appli&ation to be &on"ig!red di""erentl#( Gor "!ll details on
available paraeters see the str!ts)&on"ig[8[?(dtd or the list below(
b!""erSi>e ) The si/e (in b#tes) o" the inp!t b!""er !sed when
pro&essing "ile !ploads( `IOWQa (optional)
className ) Classnae o" &on"ig!ration bean(
`org(apa&he(str!ts(&on"ig(ControllerCon"iga (optional)
contentType ) %e"a!lt &ontent t#pe (and optional &hara&ter
en&oding) to be set on ea&h response( Ea# be overridden b# the
.&tion, JSP, or other reso!r&e to whi&h the re'!est is "orwarded(
`te+tFhtla (optional)
"or+ar)&attern ) 5epla&eent pattern de"ining how the =path=
attrib!te o" a S"orwardU eleent is apped to a &onte+t)relative 45L
when it starts with a slash (and when the &onte+t5elative propert# is
"alse)( This val!e a# &onsist o" an# &obination o" the "ollowingK
o :M ) 5epla&ed b# the od!le pre"i+ o" this od!le(
o :P ) 5epla&ed b# the =path= attrib!te o" the sele&ted S"orwardU
eleent(
o :: ) Ca!ses a literal dollar sign to be rendered(
o :( ) (Where =+= is an# &hara&ter not de"ined above) Silentl#
swallowed, reserved "or "!t!re !se(
I" not spe&i"ied, the de"a!lt "orwardPattern is &onsistent with the
previo!s behavior o" "orwards( `bEbPa (optional)
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
QM
WELCOME TO J2EE NOTES
inp!t@or+ar) ) Set to tr!e i" #o! want the inp!t attrib!te o" Sa&tionU
eleents to be the nae o" a lo&al or global .&tionGorward, whi&h will
then be !sed to &al&!late the !ltiate 45L( Set to "alse to treat the
inp!t paraeter o" Sa&tionU eleents as a od!le)relative path to the
reso!r&e to be !sed as the inp!t "or( `"alsea (optional)
locale ) Set to tr!e i" #o! want a Lo&ale ob*e&t stored in the !serHs
session i" not alread# present( `tr!ea (optional)
ma,@ileSi>e ) The a+i! si/e (in b#tes) o" a "ile to be
a&&epted as a "ile !pload( Can be e+pressed as a n!ber "ollowed
b# a =1=, =E=, or =G=, whi&h are interpreted to ean @ilob#tes,
egab#tes, or gigab#tes, respe&tivel#( `?MOEa (optional)
m!ltipartClass ) The "!ll# '!ali"ied Java &lass nae o" the
!ltipart re'!est handler &lass to be !sed with this od!le(
`org(apa&he(str!ts(!pload(CoonsE!ltipart5e'!est-andlera
(optional)
nocache ) Set to tr!e i" #o! want the &ontroller to add -TTP
headers "or de"eating &a&hing to ever# response "ro this od!le(
`"alsea (optional)
pa$e&attern ) 5epla&eent pattern de"ining how the page attrib!te
o" &!sto tags !sing it is apped to a &onte+t)relative 45L o" the
&orresponding reso!r&e( This val!e a# &onsist o" an#
&obination o" the "ollowingK
o :M ) 5epla&ed b# the od!le pre"i+ o" this od!le(
o :P ) 5epla&ed b# the =path= attrib!te o" the sele&ted S"orwardU
eleent(
o :: ) Ca!ses a literal dollar sign to be rendered(
o :( ) (Where =+= is an# &hara&ter not de"ined above) Silentl#
swallowed, reserved "or "!t!re !se(
I" not spe&i"ied, the de"a!lt pagePattern is &onsistent with the
previo!s behavior o" 45L &al&!lation( `bEbPa (optional)
processorClass ) The "!ll# '!ali"ied Java &lass nae o" the
5e'!estPro&essor s!b&lass to be !sed with this od!le(
`org(apa&he(str!ts(a&tion(5e'!estPro&essora (optional)
temp:ir ) Teporar# wor@ing dire&tor# to !se when pro&essing
"ile !ploads( `;the dire&tor# provided b# the servlet &ontainer>a
This e+aple !ses the de"a!lt val!es "or several &ontroller
paraeters( I" #o! onl# want de"a!lt behavior #o! &an oit the
&ontroller se&tion altogether(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
QQ
WELCOME TO J2EE NOTES
S&ontroller
pro&essorClass<=org(apa&he(str!ts(a&tion(5e'!estPro&essor=
deb!g<=O=
&ontentT#pe<=te+tFhtl=FU:
Messa$e Reso!rces Con"i$!ration
Str!ts has b!ilt in s!pport "or internationali/ation (I8V0)( 2o! &an
de"ine one or ore message-resources! eleents "or #o!r webapp:
od!les &an de"ine their own reso!r&e b!ndles( %i""erent b!ndles &an
be !sed si!ltaneo!sl# in #o!r appli&ation, the H@e#H attrib!te is !sed to
spe&i"# the desired b!ndle(
className ) Classnae o" &on"ig!ration bean(
`org(apa&he(str!ts(&on"ig(Eessage5eso!r&esCon"iga (optional)
"actory ) Classnae o" Eessage5eso!r&esGa&tor#(
`org(apa&he(str!ts(!til(Propert#Eessage5eso!r&esGa&tor#a
(optional)
#ey ) ServletConte+t attrib!te @e# to store this b!ndle(
`org(apa&he(str!ts(a&tion(E6SS.G6a (optional)
n!ll ) Set to "alse to displa# issing reso!r&e @e#s in #o!r
appli&ation li@e H;;;eyname;;;H instead o" n!ll( `tr!ea (optional)
parameter ) 0ae o" the reso!r&e b!ndle( (re'!ired)
E,ample con"i$!rationK
Cmessa$e(reso!rces parameterNTMyWeb%ppReso!rcesT
n!llNT"alseT HE
This wo!ld set !p a essage reso!r&e b!ndle provided in the "ile
E#Web.pp5eso!r&es(properties !nder the de"a!lt @e#(
Eissing reso!r&e @e#s wo!ld be displa#ed as H;;;eyname;;;H(
&l!$'n Con"i$!ration
Str!ts Pl!gIns are &on"ig!red !sing the Spl!g)inU eleent within the
Str!ts &on"ig!ration "ile( This eleent has onl# one valid attrib!te,
H&lass0aeH, whi&h is the "!ll# '!ali"ied nae o" the Java &lass whi&h
ipleents the org(apa&he(str!ts(a&tion(Pl!gIn inter"a&e(
Gor Pl!gIns that re'!ire &on"ig!ration theselves, the nested Sset)
propert#U eleent is available(
This is an e+aple !sing the Tiles pl!ginK
Cpl!$(in classNameNTor$-apache-str!ts-tiles-Tiles&l!$inT E
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
QR
WELCOME TO J2EE NOTES
Cset(property propertyNT)e"initions(con"i$T val!eNTHWE(
'N@Htiles()e"s-,mlTHE
CHpl!$(inE
:ata So!rce Con"i$!ration
7esides the ob*e&ts related to de"ining .&tionEappings, the Str!ts
&on"ig!ration a# &ontain eleents that &reate other !se"!l ob*e&ts(
The Sdata)so!r&esU se&tion &an be !sed to spe&i"# a &olle&tion o"
%ataSo!r&es `*ava+(s'l(%ataSo!r&ea "or the !se o" #o!r appli&ation(
T#pi&all#, a %ataSo!r&e represents a &onne&tion pool to a database or
other persistent store( .s a &onvenien&e, the Str!ts %ataSo!r&e anager
&an be !sed to instantiate whatever standard pool #o!r appli&ation a#
need( 3" &o!rse, i" #o!r persisten&e la#er provides "or its own
&onne&tions, then #o! do not need to spe&i"# a data)so!r&es eleent(
Sin&e %ataSo!r&e ipleentations var# in what properties need to be
set, !nli@e other Str!ts &on"ig!ration eleents, the data-source eleent
does not pre)de"ine a slate o" properties( Instead, the generi& set-propert%
"eat!re is !sed to set whatever properties #o!r ipleentation a#
re'!ire( T#pi&all#, these settings wo!ld in&l!deK
. driver &lass nae
. !rl to a&&ess the driver
. des&ription
.nd other s!ndr# properties(
Sdata)so!r&e t#pe<=org(apa&he(&oons(db&p(7asi&%ataSo!r&e=U
SP)) ((( set)propert# eleents ((( ))U
SFdata)so!r&eU
In Str!ts 8(?(O, the Generi&%ataSo!r&e has been reoved, and it is
re&oended that #o! !se the Coons 7asi&%ataSo!r&e or other
%ataSo!r&e ipleentation instead( In pra&ti&e, i" #o! need to !se the
%ataSo!r&e anager, #o! sho!ld !se whatever %ataSo!r&e
ipleentation wor@s best with #o!r &ontainer or database(
Gor e+aples o" spe&i"#ing a data)so!r&es eleent and !sing the
%ataSo!r&e with an .&tion, see the .&&essing a %atabase -owTo(
Con"i$!rin$ yo!r application "or mo)!les
Jer# little is re'!ired in order to start ta@ing advantage o" the Str!ts
od!le "eat!re( J!st go thro!gh the "ollowing stepsK
8( Prepare a &on"ig "ile "or ea&h od!le(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
QV
WELCOME TO J2EE NOTES
?( In"or the &ontroller o" #o!r od!le(
D( 4se a&tions to re"er to #o!r pages(
Mo)!le Con"i$!ration @iles
7a&@ in Str!ts 8(O, a "ew =boot)strap= options were pla&ed in the
web(+l "ile, and the b!l@ o" the &on"ig!ration was done in a single
str!ts)&on"ig(+l "ile( 3bvio!sl#, this wasnHt ideal "or a tea
environent, sin&e !ltiple !sers had to share the sae &on"ig!ration
"ile(
In Str!ts 8(8, #o! have two optionsK #o! &an list !ltiple str!ts)
&on"ig "iles as a &oa)deliited list, or #o! &an s!bdivide a larger
appli&ation into od!les(
With the advent o" od!les, a given od!le has its own &on"ig!ration
"ile( This eans ea&h tea (ea&h od!le wo!ld pres!abl# be
developed b# a single tea) has their own &on"ig!ration "ile, and there
sho!ld be a lot less &ontention when tr#ing to odi"# it(
'n"ormin$ the Controller
In str!ts 8(O, #o! listed #o!r &on"ig!ration "ile as an initiali/ation
paraeter to the a&tion servlet in web(+l( This is still done in 8(8, b!t
itHs a!gented a little( In order to tell the Str!ts a&hiner# abo!t #o!r
di""erent od!les, #o! spe&i"# !ltiple &on"ig initiali/ation paraeters,
with a slight twist( 2o!Hll still !se =&on"ig= to tell the a&tion servlet
abo!t #o!r =de"a!lt= od!le, however, "or ea&h additional od!le, #o!
will list an initiali/ation paraeter naed =&on"igFod!le=, where
od!le is the nae o" #o!r od!le (this gets !sed when deterining
whi&h 45Is "all !nder a given od!le, so &hoose soething
eaning"!lP)( Gor e+apleK
(((
Sinit)paraU
Spara)naeU&on"igSFpara)naeU
Spara)val!eUFW67)I0GF&on"Fstr!ts)de"a!lt(+lSFpara)val!eU
SFinit)paraU
Sinit)paraU
Spara)naeU&on"igFod!le8SFpara)naeU
Spara)val!eUFW67)I0GF&on"Fstr!ts)od!le8(+lSFpara)val!eU
SFinit)paraU
(((
This sa#s I have two od!les( 3ne happens to be the =de"a!lt=
od!le, whi&h has no =Fod!le= in itHs nae, and one naed =od!le8=
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
QW
WELCOME TO J2EE NOTES
(&on"igFod!le8)( IHve told the &ontroller it &an "ind their respe&tive
&on"ig!rations !nder FW67)I0GF&on" (whi&h is where I p!t all #
&on"ig!ration "iles)( Prett# sipleP
(E# str!ts)de"a!lt(+l wo!ld be e'!ivalent to what ost "ol@s &all
str!ts)&on"ig(+l( I *!st li@e the s#etr# o" having all # Str!ts
od!le "iles being naed str!ts)Sod!leU(+l)
I" #o!Hd li@e to var# where the pages "or ea&h od!le is stored, see the
"orwardPattern setting "or the Controller(
S+itchin$ Mo)!les
There are two basi& ethods to swit&hing "ro one od!le to
another( 2o! &an either !se a "orward (global or lo&al) and spe&i"# the
&onte+t5elative attrib!te with a val!e o" tr!e, or #o! &an !se the b!ilt)in
org(apa&he(str!ts(a&tions(Swit&h.&tion(
-ereHs an e+aple o" a global "orwardK
(((
Sstr!ts)&on"igU
(((
Sglobal)"orwardsU
S"orward nae<=toEod!le7=
&onte+t5elative<=tr!e=
path<=Fod!le7Finde+(do=
redire&t<=tr!e=FU
(((
SFglobal)"orwardsU
(((
SFstr!ts)&on"igU
2o! &o!ld do the sae thing with a lo&al "orward de&lared in an
.&tionEappingK
(((
Sstr!ts)&on"igU
(((
Sa&tion)appingsU
(((
Sa&tion ((( U
S"orward nae<=s!&&ess=
&onte+t5elative<=tr!e=
path<=Fod!le7Finde+(do=
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
RO
WELCOME TO J2EE NOTES
redire&t<=tr!e=FU
SFa&tionU
(((
SFa&tion)appingsU
(((
SFstr!ts)&on"igU
Ginall#, #o! &o!ld !se org#apac'e#struts#actions#Switc'Action, li@e soK
(((
Sa&tion)appingsU
Sa&tion path<=FtoEod!le=
t#pe<=org(apa&he(str!ts(a&tions(Swit&h.&tion=FU
(((
SFa&tion)appingsU
(((
0ow, to &hange to Eod!le7, we wo!ld !se a 45I li@e thisK
httpKFFlo&alhostKVOVOFtoEod!le(do?pre"i+<Fod!le7Lpage<Finde+(do
I" #o! are !sing the =de"a!lt= od!le as well as =naed= od!les (li@e
=Fod!le7=), #o! &an swit&h ba&@ to the =de"a!lt= od!le with a 45I
li@e thisK
httpKFFlo&alhostKVOVOFtoEod!le(do?pre"i+<Lpage<Finde+(do
ThatHs all there is to itP -app# od!le)swit&hingP
The Web %pplication :eployment :escriptor
The "inal step in setting !p the appli&ation is to &on"ig!re the
appli&ation deplo#ent des&riptor (stored in "ile &()-*+F/we"#xml) to
in&l!de all the Str!ts &oponents that are re'!ired( 4sing the
deplo#ent des&riptor "or the e+aple appli&ation as a g!ide, we see
that the "ollowing entries need to be &reated or odi"ied(
Con"i$!re the %ction Servlet 'nstance
.dd an entr# de"ining the a&tion servlet itsel", along with the
appropriate initiali/ation paraeters( S!&h an entr# ight loo@ li@e thisK
SservletU
Sservlet)naeUa&tionSFservlet)naeU
Sservlet)&lassU
org(apa&he(str!ts(a&tion(.&tionServlet
SFservlet)&lassU
Sinit)paraU
Spara)naeU&on"igSFpara)naeU
Spara)val!eU
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
R8
WELCOME TO J2EE NOTES
FW67)I0GFstr!ts)&on"ig(+l
SFpara)val!eU
SFinit)paraU
Sload)on)start!pU8SFload)on)start!pU
SFservletU
The initiali/ation paraeters s!pported b# the &ontroller servlet are
des&ribed below( (2o! &an also "ind these details in the Javado&s "or the
.&tionServlet &lass() S'!are bra&@ets des&ribe the de"a!lt val!es that are
ass!ed i" #o! do not provide a val!e "or that initiali/ation paraeter(
con"i$ ) Conte+t)relative path to the ^EL reso!r&e &ontaining the
&on"ig!ration in"oration "or the de"a!lt od!le( This a# also be a
&oa)deliited list o" &on"ig!ration "iles( 6a&h "ile is loaded in t!rn,
and its ob*e&ts are appended to the internal
data str!&t!re( `FW67)I0GFstr!ts)&on"ig(+la(
W%RN'N3 ) I" #o! de"ine an ob*e&t o" the sae nae in ore than one
&on"ig!ration "ile, the last one loaded '!ietl# wins(
con"i$HXMmo)!leR ) Conte+t)relative path to the ^EL reso!r&e
&ontaining the &on"ig!ration in"oration "or the appli&ation od!le that
will !se the spe&i"ied pre"i+ (Fb;od!le>)( This &an be repeated as
an# ties as re'!ired "or !ltiple appli&ation od!les( (Sin&e Str!ts
8(8)
convertN!ll ) Gor&e si!lation o" the Str!ts 8(O behavior when
pop!lating "ors( I" set to tr!e, the n!eri& Java wrapper &lass t#pes
(li@e *ava(lang(Integer) will de"a!lt to n!ll (rather than O)( (Sin&e Str!ts 8(8)
`"alsea
r!lesets ) Coa)deliited list o" "!ll# '!ali"ied &lassnaes o"
additional org(apa&he(&oons(digester(5!leSet instan&es that sho!ld be added to the
%igester that will be pro&essing str!ts)&on"ig(+l "iles( 7# de"a!lt, onl# the 5!leSet
"or the standard &on"ig!ration eleents is loaded( (Sin&e Str!ts 8(8)
vali)atin$ ) Sho!ld we !se a validating ^EL parser to pro&ess the
&on"ig!ration "ile (strongl# re&oended)? `tr!ea
W%RN'N3 ) Str!ts will not operate &orre&tl# i" #o! de"ine ore than
one servlet! eleent "or a &ontroller servlet, or a s!b&lass o" the
standard &ontroller servlet &lass( The &ontroller servlet MAST be a web
appli&ation wide singleton(
Con"i$!re the %ction Servlet Mappin$
Note* The aterial in this se&tion is not spe&i"i& to Str!ts( The
&on"ig!ration o" servlet appings is de"ined in the Java Servlet
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
R?
WELCOME TO J2EE NOTES
Spe&i"i&ation( This se&tion des&ribes the ost &oon eans o"
&on"ig!ring a Str!ts appli&ation(
There are two &oon approa&hes to de"ining the 45Ls that will be
pro&essed b# the &ontroller servlet )) pre"i+ at&hing and e+tension
at&hing( .n appropriate apping entr# "or ea&h approa&h will be
des&ribed below(
Pre"i+ at&hing eans that #o! want all 45Ls that start (a"ter the
&onte+t path part) with a parti&!lar val!e to be passed to this servlet(
S!&h an entr# ight loo@ li@e thisK
Sservlet)appingU
Sservlet)naeUa&tionSFservlet)naeU
S!rl)patternUFdoF9SF!rl)patternU
SFservlet)appingU
whi&h eans that a re'!est 45I to at&h the /logon path des&ribed
earlier ight loo@ li@e thisK
httpKFFwww(#&opan#(&oF#appli&ationFdoFlogon
where F#appli&ation is the &onte+t path !nder whi&h #o!r appli&ation
is deplo#ed(
6+tension apping, on the other hand, at&hes re'!est 45Is to the
a&tion servlet based on the "a&t that the 45I ends with a period "ollowed
b# a de"ined set o" &hara&ters( Gor e+aple, the JSP pro&essing servlet is
apped to the ,#-sp pattern so that it is &alled to pro&ess ever# JSP page
that is re'!ested( To !se the ,#do e+tension (whi&h iplies =do
soething=), the apping entr# wo!ld loo@ li@e thisK
Sservlet)appingU
Sservlet)naeUa&tionSFservlet)naeU
S!rl)patternU9(doSF!rl)patternU
SFservlet)appingU
and a re'!est 45I to at&h the /logon path des&ribed earlier ight
loo@ li@e thisK
httpKFFwww(#&opan#(&oF#appli&ationFlogon(do
W%RN'N3 ) Str!ts will not operate &orre&tl# i" #o! de"ine ore than
one servlet-mapping! eleent "or the &ontroller servlet(
W%RN'N3 ) I" #o! are !sing the new od!le s!pport in Str!ts 8(8,
#o! sho!ld be aware that only e+tension apping is s!pported(
Con"i$!re the Str!ts Ta$ Libraries
0e+t, #o! !st add an entr# de"ining the Str!ts tag librar#(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
RD
WELCOME TO J2EE NOTES
The str!ts)bean taglib &ontains tags !se"!l in a&&essing beans and their
properties, as well as de"ining new beans (based on these a&&esses) that
are a&&essible to the reainder o" the page via s&ripting variables and
page s&ope attrib!tes( Convenient e&haniss to &reate new beans
based on the val!e o" re'!est &oo@ies, headers, and paraeters are also
provided(
The str!ts)htl taglib &ontains tags !sed to &reate str!ts inp!t "ors, as
well as other tags generall# !se"!l in the &reation o" -TEL)based !ser
inter"a&es(
The str!ts)logi& taglib &ontains tags that are !se"!l in anaging
&onditional generation o" o!tp!t te+t, looping over ob*e&t &olle&tions "or
repetitive generation o" o!tp!t te+t, and appli&ation "low anageent(
The str!ts)tiles taglib &ontains tags !sed "or &obining vario!s view
&oponents, &alled =tiles=, into a "inal &oposite view(
The str!ts)nested taglib is an e+tension o" other str!ts taglibs that allows
the !se o" nested beans(
7elow is how #o! wo!ld de"ine all taglibs "or !se within #o!r
appli&ation( In pra&ti&e, #o! wo!ld onl# spe&i"# the taglibs that #o!r
appli&ation !sesK
Cta$libE
Staglib)!riUFtagsFstr!ts)bean
SFtaglib)!riU
Staglib)lo&ationUFW67)I0GFstruts-bean.tld
SFtaglib)lo&ationU
CHta$libE
StaglibU
Staglib)!riUFtagsFstr!ts)htl
SFtaglib)!riU
Staglib)lo&ationUFW67)I0GFstr!ts(html-tl)
/tagli"-location!
/tagli"!
tagli"!
tagli"-uri!/tags/struts-logic
/tagli"-uri!
tagli"-location!/&()-*+F/struts-logic.tld
SFtaglib)lo&ationU
SFtaglibU
StaglibU
Staglib)!riUFtagsFstr!ts)tiles
SFtaglib)!riU
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
RI
WELCOME TO J2EE NOTES
Staglib)lo&ationUFW67)I0GFstr!ts(tiles-tl)
/tagli"-location!
/tagli"!
This tells the JSP s#ste where to "ind the tag librar# des&riptor "or
this librar# (in #o!r appli&ationHs W67)I0G dire&tor#, instead o" o!t on
the Internet soewhere)(
Con"i$!re the Str!ts Ta$ Libraries 8Servlet 2-09
Servlet 2-0 Asers only* The Servlet ?(D spe&i"i&ation sipli"ies the
deplo#ent and &on"ig!ration o" tag libraries( The instr!&tions above
will wor@ on older &ontainers as well as ?(D &ontainers (Str!ts onl#
re'!ires a servlet ?(? &ontainer): however, i" #o!Hre !sing a ?(D &ontainer
s!&h as To&at I(+, #o! &an ta@e advantage o" a sipli"ied deplo#ent(
.ll thatHs re'!ired to install the str!ts tag libraries is to &op# str!ts(*ar
into #o!r FW67)I0GFlib dire&tor# and re"eren&e the tags in #o!r &ode
li@e thisK
CDF ta$lib !riNhttp*HHstr!ts-apache-or$Hta$s(html prefix="html" %>
0ote that #o! m!st !se the "!ll !ri de"ined in the vario!s str!ts tlds
so that the &ontainer @nows where to "ind the tagHs &lass "iles( 2o! donHt
have to alter #o!r web(+l "ile or &op# tlds into an# appli&ation
dire&tories(
%)) Str!ts Components To Uo!r %pplication
To !se Str!ts, #o! !st &op# the (tld "iles that #o! re'!ire into #o!r
&()-*+F dire&tor#, and &op# struts#-ar (and all o" the commons-,#-ar "iles)
into #o!r &()-*+F/li" dire&tor#(
Str!ts ean Ta$s
This tag librar# &ontains tags !se"!l in a&&essing beans and their
properties, as well as de"ining new beans (based on these a&&esses) that
are a&&essible to the reainder o" the page via s&ripting variables and
page s&ope attrib!tes( Convenient e&haniss to &reate new beans
based on the val!e o" re'!est &oo@ies, headers, and paraeters are also
provided(
Ean# o" the tags in this tag librar# will throw a Jsp6+&eption at
r!ntie when the# are !tili/ed in&orre&tl# (s!&h as when #o! spe&i"# an
invalid &obination o" tag attrib!tes)( JSP allows #o! to de&lare an
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
RM
WELCOME TO J2EE NOTES
=error page= in the STX page TU dire&tive( I" #o! wish to pro&ess the
a&t!al e+&eption that &a!sed the proble, it is passed to the error page as
a re'!est attrib!te !nder @e# org(apa&he(str!ts(a&tion(6^C6PTI30(
I" #o! are viewing this page "ro within the Str!ts %o&!entation
.ppli&ation (or online at httpKFFstr!ts(apa&he(orgF), #o! &an learn ore
abo!t !sing these tags in the 7ean Tags %eveloperHs G!ide(
Ta$ Name :escription
&oo@ie %e"ine a s&ripting variable based on the val!e(s) o" the
spe&i"ied re'!est &oo@ie(
de"ine %e"ine a s&ripting variable based on the val!e(s) o" the
spe&i"ied bean propert#(
header Load the response "ro a d#nai& appli&ation re'!est and a@e
it available as a bean
in&l!de 5ender an internationali/ed essage string to the response(
essage 6+pose a spe&i"ied ite "ro the page &onte+t as a bean(
page %e"ine a s&ripting variable based on the val!e(s) o" the spe&i"ied
re'!est paraeter(
paraeter Load a web appli&ation reso!r&e and a@e it available as a
bean(
reso!r&e %e"ine a bean &ontaining the n!ber o" eleents in a
Colle&tion or Eap(
si/e 6+pose a naed Str!ts internal &on"ig!ration ob*e&t as a
bean(
str!ts 5ender the val!e o" the spe&i"ied bean propert# to the
&!rrent JspWriter(
Str!ts

The &ore o" the Str!ts "raewor@ is a "le+ible &ontrol la#er based on
standard te&hnologies li@e Java Servlets, Java7eans, 5eso!r&e7!ndles,
and ^EL, as well as vario!s Ja@arta Coons pa&@ages( Str!ts
en&o!rages appli&ation ar&hite&t!res based on the Eodel ? approa&h, a
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
RQ
WELCOME TO J2EE NOTES
variation o" the &lassi& Eodel)Jiew)Controller (EJC) design
paradig(
Str!ts provides its own Controller &oponent and integrates with
other te&hnologies to provide the Eodel and the Jiew( Gor the Eodel,
Str!ts &an intera&t with standard data a&&ess te&hnologies, li@e J%7C
and 6J7, as well as ost an# third)part# pa&@ages, li@e -ibernate,
i7.TIS, or 3b*e&t 5elational 7ridge( Gor the Jiew, Str!ts wor@s well
with JavaServer Pages, in&l!ding JSTL and JSG, as well as Jelo&it#
Teplates, ^SLT, and other presentation s#stes(
The Str!ts "raewor@ provides the invisible !nderpinnings ever#
pro"essional web appli&ation needs to s!rvive( Str!ts helps #o! &reate an
e+tensible developent environent "or #o!r appli&ation, based on
p!blished standards and proven design patterns(
What is the )i""erence bet+een Str!ts .-; an) Str!ts .-.
The new "eat!res added to Str!ts 8(8 are 8( 5e'!estPro&essor &lass
?( Eethod per"orm89 repla&ed b# e,ec!te89 in Str!ts base .&tion Class
D( Changes to web(+l and str!ts)&on"ig(+l I(%e&larative e+&eption
handling M(%#nai& .&tionGors Q(Pl!g)ins R(E!ltiple .ppli&ation
Eod!les V(0ested Tags W(The Str!ts Jalidator 8O(Change to the 353
pa&@age 88(Change to Coons logging 8?(5eoval o" .din a&tions
8D( %epre&ation o" the Generi&%ataSo!r&e
E,plain Str!ts navi$ation "lo+
. &lient re'!ests a path that at&hes the .&tion 45I pattern( The
&ontainer passes the re'!est to the .&tionServlet( I" this is a od!lar
appli&ation, the .&tionServlet sele&ts the appropriate od!le( The
.&tionServlet loo@s !p the apping "or the path( I" the apping
spe&i"ies a "or bean, the .&tionServlet sees i" there is one alread# or
&reates one( I" a "or bean is in pla#, the .&tionServlet resets and
pop!lates it "ro the -TTP re'!est( I" the apping has the validate
propert# set to tr!e, it &alls validate on the "or bean( I" it "ails, the
servlet "orwards to the path spe&i"ied b# the inp!t propert# and this
&ontrol "low ends( I" the apping spe&i"ies an .&tion t#pe, it is re!sed i"
it alread# e+ists or instantiated(
The .&tions per"or or e+e&!te ethod is &alled and passed the
instantiated "or bean (or n!ll)( The .&tion a# pop!late the "or
bean, &all b!siness ob*e&ts, and do whatever else is needed( The .&tion
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
RR
WELCOME TO J2EE NOTES
ret!rns an .&tionGorward to the .&tionServlet( I" the .&tionGorward is
to another .&tion 45I, we begin again: otherwise, its o"" to a displa#
page or soe other reso!r&e( Eost o"ten, it is a JSP, in whi&h &ase
Jasper, or the e'!ivalent (not Str!ts), renders the page(
What is the )i""erence bet+een %ction@orm an) :yna%ction@orm
In str!ts 8(O, a&tion "or is !sed to pop!late the htl tags in *sp
!sing str!ts &!sto tag(when the *ava &ode &hanges, the &hange in
a&tion &lass is needed( To avoid the &hages in str!ts 8(8 d#na a&tion
"or is introd!&ed(This &an be !sed to develop !sing +l(The d#na
a&tion "or bloats !p with the str!ts)&on"ig(+l based de"inetion(
What is :ispatch%ction
The %ispat&h.&tion &lass is !sed to gro!p related a&tions into one
&lass( %ispat&h.&tion is an abstra&t &lass, so #o! !st override it to !se
it( It e+tends the .&tion &lass(
It sho!ld be noted that #o! dont have to !se the %ispat&h.&tion to gro!p
!ltiple a&tions into one .&tion &lass(
2o! &o!ld *!st !se a hidden "ield that #o! inspe&t to delegate to
eber() ethods inside o" #o!r a&tion(
Ho+ to call e=b "rom Str!ts
!se the Servi&e Lo&ator patter to loo@ !p the e*bs
3r 2o! &an !se InitialConte+t and get the hoe inter"a&e(
What are the vario!s Str!ts ta$ libraries
str!ts(html ta$ library ) !sed "or &reating d#nai& -TEL !ser
inter"a&es and "ors( str!ts(bean ta$ library ) provides s!bstantial
enhan&eents to the basi& &apabilit# provided b# (
str!ts(lo$ic ta$ library ) &an anage &onditional generation o" o!tp!t
te+t, looping over ob*e&t &olle&tions "or repetitive generation o" o!tp!t
te+t, and appli&ation "low anageent(
str!ts(template ta$ library ) &ontains tags that are !se"!l in &reating
d#nai& JSP teplates "or pages whi&h share a &oon "orat(
What is the )i""erence bet+een %ctionErrors an) %ctionMessa$es
The di""eren&e between the &lasses is /ero )) all behavior in
.&tion6rrors was p!shed !p into .&tionEessages and all behavior in
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
RV
WELCOME TO J2EE NOTES
.&tion6rror was p!shed !p into .&tionEessage( This was done in the
attept to &learl# signal that these &lasses &an be !sed to pass an# @ind
o" essages "ro the &ontroller to the view )) errors being onl# one @ind
o" essage
Ho+ yo! +ill han)le errors an) e,ceptions !sin$ Str!ts
There are vario!s wa#s to handle e+&eptionK
8) To handle errors server side validation &an be !sed !sing
.&tion6rrors &lasses &an be !sed(
?) The e+&eptions &an be wrapped a&ross di""erent la#ers to show a !ser
showable e+&eption(
D)!sing validators
Ho+ yo! +ill save the )ata across )i""erent pa$es "or a partic!lar
client re?!est !sin$ Str!ts
Several wa#s( The siilar to the wa#s session tra&@ing is enabled(
4sing &oo@ies, 45L)rewriting, SSLSession, and possibilt# threw in the
database(
What +e +ill )e"ine in Str!ts(con"i$-,ml "ile- %n) e,plain their
p!rpose
The ain &ontrol "ile in the Str!ts "raewor@ is the str!ts)&on"ig(+l
^EL "ile, where a&tion appings are spe&i"ied( This "ileHs str!&t!re is
des&ribed b# the str!ts)&on"ig %T% "ile, whi&h is de"ined at
httpKFF*a@arta(apa&he(orgFstr!tsF( . &op# o" the %T% &an be "o!nd on
the Fdo&sFdtds s!bdire&tor# o" the "raewor@Hs installation root
dire&tor#( The top)level eleent is str!ts)&on"ig( 7asi&all#, it &onsists o"
the "ollowing eleentsK
)ata(so!rcesc. set o" data)so!r&e eleents, des&ribing paraeters
needed to instantiate J%7C ?(O Standard 6+tension %ataSo!r&e ob*e&ts
"orm(beansc. set o" "or)bean eleents that des&ribe the "or beans
that this appli&ation !ses
$lobal("or+ar)sc. set o" "orward eleents des&ribing general
available "orward 45Is
action(mappin$sc. set o" a&tion eleents des&ribing a re'!est)to)
a&tion apping
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
RW
WELCOME TO J2EE NOTES
What is the p!rpose o" tiles()e"-,ml "ile< reso!rceb!n)le-properties
"ile< vali)ation-,ml "ile
The Tiles Graewor@ is an advan&ed version o" that &oes b!ndled
with the Str!ts Webapp "raewor@( Its p!rpose is red!&e the d!pli&ation
between *sp pages as well as a@e la#o!ts "le+ible and eas# to aintain(
It integrates with Str!ts !sing the &on&ept o" naed views or de"initions(
What is %ction Class- What are the metho)s in %ction class
.&tion &lass is re'!est handler in Str!ts( we will e+tend the .&tion
&lass and over ride the e+e&!te() ethod in whi&h we will spe&i"# the
b!siness logi& to be per"ored(
E,plain abo!t to#en "eat!re in Str!ts
To@ens are !sed to &he&@ "or invalid path "or b# the !erK
8) i" the !ser presses ba&@ b!tton and s!bits the sae page
?)or i" the !ser re"reshes the page whi&h will res!lt to the res!bit o"
the previo!s a&tion and ight lead to !nstabalit#((
to solve the abv probs we !se to@ens
8) in previo!s a&tion t#pe saveTo@ens(-ttpServletre!est)
?) in &!rrent a&tion &he&@ "or d!pli&ation b!
i"(PisJalidTo@en())
What part o" MVC )oes Str!ts represent
7ad '!estion( Str!ts is a "raewor@ whi&h s!pports the EJC pattern(
What are the core classes o" str!ts/
The &ore &lasses o" str!ts are .&tionGor, .&tion, .&tionEapping,
.&tionGorward et&(
What are the 'mportant Components o" Str!ts/
8( .&tion Servlet
?( .&tion Classes
D( .&tion Gor
I( Jalidator Graewor@
M( Eessage 5eso!r&es
Q( Str!ts Con"ig!ration ^EL Giles
R( Jiew &oponents li@e JSP
What is Str!ts/
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
VO
WELCOME TO J2EE NOTES
Str!ts is a web page developent "raewor@ and an open so!r&e
so"tware that helps developers b!ild web appli&ations '!i&@l# and
easil#( Str!ts &obines Java Servlets, Java Server Pages, &!sto tags,
and essage reso!r&es into a !ni"ied "raewor@( It is a &ooperative,
s#nergisti& plat"or, s!itable "or developent teas, independent
developers, and ever#one between(
Ho+ is the MVC )esi$n pattern !se) in Str!ts "rame+or#/
In the EJC design pattern, appli&ation "low is ediated b# a
&entral Controller( The Controller delegates re'!ests to an appropriate
handler( The handlers are tied to a Eodel, and ea&h handler a&ts as an
adapter between the re'!est and the Eodel( The Eodel represents, or
en&aps!lates, an appli&ationHs b!siness logi& or state( Control is !s!all#
then "orwarded ba&@ thro!gh the Controller to the appropriate Jiew(
The "orwarding &an be deterined b# &ons!lting a set o" appings,
!s!all# loaded "ro a database or &on"ig!ration "ile( This provides a
loose &o!pling between the Jiew and Eodel, whi&h &an a@e an
appli&ation signi"i&antl# easier to &reate and aintain(
Controller))Servlet &ontroller whi&h s!pplied b# Str!ts itsel": Jiew )))
what #o! &an see on the s&reen, a JSP page and presentation
&oponents: Eodel ))) S#ste state and a b!siness logi& Java7eans(

Who ma#es the Str!ts/
Str!ts is hosted b# the .pa&he So"tware Go!ndation(.SG) as part o"
its Ja@arta pro*e&t, li@e To&at, .nt and Jelo&it#(
Why it calle) Str!ts/
7e&a!se the designers want to reind !s o" the invisible !nderpinnings
that hold !p o!r ho!ses, b!ildings, bridges, and o!rselves when we are
on stilts( This e+&ellent des&ription o" Str!ts re"le&t the role the Str!ts
pla#s in developing web appli&ations(
:o +e nee) to pay the Str!ts i" bein$ !se) in commercial p!rpose/
0o( Str!ts is available "or &oer&ial !se at no &harge !nder the
.pa&he So"tware Li&ense( 2o! &an also integrate the Str!ts &oponents
into #o!r own "raewor@ *!st as i" the# were writtern in ho!se witho!t
an# red tape, "ees, or other hassles
What are the core classes o" Str!ts/
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
V8
WELCOME TO J2EE NOTES
.&tion, .&tionGor, .&tionServlet, .&tionEapping, .&tionGorward
are basi& &lasses o" Str!&ts(
What is the )esi$n role playe) by Str!ts/
The role pla#ed b# Str!&ts is &ontroller in
EodelFJiewFController(EJC) st#le( The Jiew is pla#ed b# JSP and
Eodel is pla#ed b# J%7C or generi& data so!r&e &lasses( The Str!ts
&ontroller is a set o" prograable &oponents that allow developers to
de"ine e+a&tl# how the appli&ation intera&ts with the !ser(
Ho+ Str!ts control )ata "lo+/
Str!ts ipleents the EJCFLa#ers pattern thro!gh the !se o"
.&tionGorwards and .&tionEappings to @eep &ontrol)"low de&isions o!t
o" presentation la#er(
What con"i$!ration "iles are !se) in Str!ts/
)).ppli&ation5eso!r&esl(properties
))str!ts)&on"ig(+l
These two "iles are !sed to bridge the gap between the Controller and
the Eodel(
What helpers in the "orm o" JS& pa$es are provi)e) in Str!ts
"rame+or#/
))str!ts)htl(tld
))str!ts)bean(tld
))str!ts)logi&(tld
's Str!ts e""icient/
))The Str!ts is not onl# thread)sa"e b!t thread)dependent(instantiates
ea&h .&tion on&e and allows other re'!ests to be threaded thro!gh the
original ob*e&t(
)).&tionGor beans inii/e s!b&lass &ode and shorten s!b&lass
hierar&hies
))The Str!ts tag libraries provide general)p!rpose "!n&tionalit#
))The Str!ts &oponents are re!sable b# the appli&ation
))The Str!ts lo&ali/ation strategies red!&e the need "or red!ndant JSPs
))The Str!ts is designed with an open ar&hite&t!re))s!b&lass available
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
V?
WELCOME TO J2EE NOTES
))The Str!ts is lightweight (M &ore pa&@ages, M tag libraries)
))The Str!ts is open so!r&e and well do&!ented (&ode to be e+ained
easil#)
))The Str!ts is odel ne!tral
What is Ja#arta Str!ts @rame+or#/ ( Ja@arta Str!ts is open so!r&e
ipleentation o" EJC (Eodel)Jiew)Controller) pattern "or the
developent o" web based appli&ations( Ja@arta Str!ts is rob!st
ar&hite&t!re and &an be !sed "or the developent o" appli&ation o" an#
si/e( Str!ts "raewor@ a@es it !&h easier to design s&alable, reliable
Web appli&ations with Java(
What is %ctionServlet/
The &lass or$-apache-str!ts-action-%ctionServlet is the &alled the
.&tionServlet( In the the Ja@arta Str!ts Graewor@ this &lass pla#s the
role o" &ontroller( .ll the re'!ests to the server goes thro!gh the
&ontroller( Controller is responsible "or handling all the re'!ests(
Ho+ yo! +ill ma#e available any Messa$e Reso!rces :e"initions "ile
to the Str!ts @rame+or# Environment/
Eessage 5eso!r&es %e"initions "ile are siple (properties "iles and
these "iles &ontains the essages that &an be !sed in the str!ts pro*e&t(
Eessage 5eso!r&es %e"initions "iles &an be added to the str!ts)
&on"ig(+l "ile thro!gh Sessage)reso!r&es FU tag(
E,ample*
Sessage)reso!r&es paraeter<BEessage5eso!r&esB FU
What is %ction Class/
The .&tion Class is part o" the Eodel and is a wrapper aro!nd the
b!siness logi&( The p!rpose o" .&tion Class is to translate the
-ttpServlet5e'!est to the b!siness logi&( To !se the .&tion, we need to
S!b&lass and overwrite the e+e&!te() ethod( In the .&tion Class all
the databaseFb!siness pro&essing are done( It is advisable to per"or all
the database related st!""s in the .&tion Class( The .&tionServlet
(&oad) passes the paraeteri/ed &lass to .&tion Gor !sing the
e+e&!te() ethod( The ret!rn t#pe o" the e+e&!te ethod is
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
VD
WELCOME TO J2EE NOTES
.&tionGorward whi&h is !sed b# the Str!ts Graewor@ to "orward the
re'!est to the "ile as per the val!e o" the ret!rned .&tionGorward ob*e&t(
Write co)e o" any %ction Class/
-ere is the &ode o" .&tion Class that ret!rns the .&tionGorward ob*e&t(
8( iport *ava+(servlet(http(-ttpServlet5e'!est:
?( iport *ava+(servlet(http(-ttpServlet5esponse:
D( iport org(apa&he(str!ts(a&tion(.&tion:
I( iport org(apa&he(str!ts(a&tion(.&tionGor:
M( iport org(apa&he(str!ts(a&tion(.&tionGorward:
Q( iport org(apa&he(str!ts(a&tion(.&tionEapping:
R(
V( p!bli& &lass Test.&tion e+tends .&tion
W( ;
8O( p!bli& .&tionGorward e+e&!te(
88( .&tionEapping apping,
8?( .&tionGor "or,
8D( -ttpServlet5e'!est re'!est,
8I( -ttpServlet5esponse response) throws 6+&eption
8M( ;
8Q( ret!rn apping("indGorward(Z=test.&tionZ=):
8R( >
8V( >
What is %ction@orm/
.n .&tionGor is a Java7ean that e+tends
org(apa&he(str!ts(a&tion(.&tionGor( .&tionGor aintains the
session state "or web appli&ation and the .&tionGor ob*e&t is
a!toati&all# pop!lated on the server side with data entered "ro a
"or on the &lient side(
What is Str!ts Vali)ator @rame+or#/
Str!ts Graewor@ provides the "!n&tionalit# to validate the "or
data( It &an be !se to validate the data on the !sers browser as well as
on the server side( Str!ts Graewor@ eits the *ava s&ripts and it &an
be !sed validate the "or data on the &lient browser( Server side
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
VI
WELCOME TO J2EE NOTES
validation o" "or &an be a&&oplished b# s!b &lassing #o!r Gro
7ean with %#naJalidatorGor &lass( The Jalidator "raewor@ was
developed b# %avid Winter"eldt as third)part# add)on to Str!ts( 0ow
the Jalidator "raewor@ is a part o" Ja@arta Coons pro*e&t and it
&an be !sed with or witho!t Str!ts( The Jalidator "raewor@ &oes
integrated with the Str!ts Graewor@ and &an be !sed witho!t doing
an# e+tra settings(
3ive the :etails o" YML "iles !se) in Vali)ator @rame+or#/
The Jalidator Graewor@ !ses two ^EL &on"ig!ration "iles
validator)r!les(+l and validation(+l( The validator)r!les(+l
de"ines the standard validation ro!tines, these are re!sable and !sed
in validation(+l( to de"ine the "or spe&i"i& validations( The
validation(+l de"ines the validations applied to a "or bean(
Ho+ yo! +ill )isplay vali)ation "ail errors on =sp pa$e/ The
"ollowing tag displa#s all the errorsK
Chtml*errorsHE
-ow #o! will enable "ront)end validation based on the +l in
validation(+l? The Chtml*=avascriptE tag to allow "ront)end
validation based on the +l in validation(+l( Gor e+aple the &odeK
Chtml*=avascript "ormNameNPlo$on@ormP
)ynamicJavascriptNPtr!eP staticJavascriptNPtr!eP HE generates
the &lient side *ava s&ript "or the "or AlogonGorB as de"ined in the
validation(+l "ile( The Chtml*=avascriptE when added in the *sp
"ile generates the &lient site validation s&ript(
EJ Enterprise Java eans Enterprise Java eans
%$en)a
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
VM
WELCOME TO J2EE NOTES
What is an 6J7
7ean 7asi&s
Coponent Contra&t
7ean Jarieties
$Session 7eans
$6ntit# 7eans
$Eessage %riven 7eans
What is an 6J7 ?
7ean is a &oponent
. server)side &oponent
Contains b!siness logi& that operates on soe teporar# data or
peranent database
Is &!stoi/able to the target environent
Is re)!sable
Is tr!l# plat"or)independent
&o, #hat is an +3<;
5ead#)to)!se Java &oponent
$7eing Java iplies portabilit#, inter)operabilit#
Can be assebled into a distrib!ted !lti)tier appli&ation
-andles threading, transa&tions
Eanages state and reso!r&es
Sipli"ies the developent o" &ople+ enterprise appli&ations
ene"its K
P!re Java iplies portabilit#
$e+&hange &oponents witho!t giving awa# the so!r&e(
Provides interoperabilit#
$asseble &oponents "ro an#where, &an all wor@ together(
Operational ene"its "rom EJ
Transa&tion anageent servi&e
%istrib!ted Transa&tion s!pport
Portabilit#
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
VQ
WELCOME TO J2EE NOTES
S&alabilit#
Integration with C357. possible
S!pport "ro !ltiple vendors
=hat -oes +3< ,eally -efine;
Coponent ar&hite&t!re
Spe&i"i&ation to write &oponents !sing Java
Spe&i"i&ation to A&oponent server developersB
Contra&t between developer roles in a &oponents)based appli&ation
pro*e&t
The basis o" &oponents !sed in distrib!ted transa&tion)oriented
enterprise appli&ations(
The Coponenti/ed .ppli&ation K
.ppli&ation now &onsists o" several re)!sable &oponents(
'nstances o" &oponents &reated at r!n)tie "or a &lient(
Coon ob*e&t "or all instan&es o" the &oponent, !s!all# &alled the
Ga&tor# 3b*e&t
$6J7 &alls it A-oe 3b*e&tB
Coon pla&e where &lient &an lo&ate this Home Ob=ect
3b*e&ts lo&ated "ro a reote &lient thro!gh JN:' (Java 0aing and
%ire&tor# Inter"a&e) servi&e(
%pplication Server provi)es K
J0%I based naing servi&e
Ipleentation o" 7ean, -oe and 5eote
Coplete Li"e C#&le Eanageent
5eso!r&e pooling ) beans, db &onne&tions, threads(((
3b*e&t persisten&e
Transa&tion anageent
Se&!red a&&ess to beans
S&alabilit# and availabilit#
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
VR
WELCOME TO J2EE NOTES
E!: Core o" #EE
$rchitecture

Java Client
The %rchitect!re Scenario
5pplication ,esponsi"ilities
)Create individ!al b!siness and web &oponents(
).sseble these &oponents into an appli&ation(
)%eplo# appli&ation on an appli&ation server(
)5!n appli&ation on target environent(
EJ %rchitect!re Roles * .ppointed "or 5esponsibilities
Si+ roles in appli&ation developent and deplo#ent li"e &#&le
$7ean Provider
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
VV
J?66 Server
3&P
&er%lets
7ea
ns
ri
HTML
client
http
7!siness logi&
7!siness logi&
6J7 Coponent
server
6J7 Coponent
server
WELCOME TO J2EE NOTES
$.ppli&ation .ssebler
$Server Provider
$Container Provider
$%eplo#er
$S#ste .dinistrator
6a&h role per"ored b# a di""erent part#(
Prod!&t o" one role &opatible with another(
Creating the 7ean Instan&e
Loo@ !p "or the -oe 3b*e&t thro!gh J0%I
Get the re"eren&e
Call create8K9 metho)
The server generates the &ode "or reote a&&ess !sing RM' (5eote
Eethod Invo&ation)(
The 5EI &ode in the "or o" st!b and s@eletonK K
$establishes &onne&tion,
$arshalsF!narshals
$pla&es reote ethod &alls
ean 'nstance
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
VW
Client

7ean Instan&e &reated
7ean
Coponent Server
WELCOME TO J2EE NOTES
Component Contract *
Client)view &ontra&t
Coponent &ontra&t
6J7)*ar "ile


Client(vie+ contract *
Contra&t between &lient and &ontainer
4ni"or appli&ation developent odel "or greater re)!se o"
&oponents
Jiew sharing b# lo&al and reote progras
The Client &an beK
$another 6J7 deplo#ed in sae or another &ontainer
$a Java progra, an applet or a Servlet
$apped to non)Java &lients li@e C357. &lients
Component contract *
7etween an 6J7 and the &ontainer it is hosted b#
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
WO
Client
Client Jiew
Contra&t
Container
Component Server
Bean Instance
Component
Contract
Bean class files! interfaces
Bean class files! interfaces
Deployment "escriptor
Deployment "escriptor
E#B$%ar
WELCOME TO J2EE NOTES
This &ontra&t needs responsibilities to be shared b#K
$the bean provider
$the &ontainer provider
ean provi)erZs responsibility *
Ipleent b!siness ethods in the bean
Ipleent e2"'reate, e2"Post'reate and e2",emo%e ethods, and
e2">ind ethod (in the &ase o" bean anaged persisten&e)
%e"ine hoe and reote inter"a&es o" the bean
Ipleent &ontainer &allba&@s de"ined in the 2a%a(.e2".&ession
bean inter"a&e
$optionall# the 2a%a(.e2".&ession&ynchroni?ation inter"a&e
Ipleent &ontainer &allba&@s de"ined in 2a%a(.e2".+ntity<ean
inter"a&es "or entities
.void prograing pra&ti&es that inter"ere with &ontainers r!ntie
anageent o" bean instan&es
Container provi)erZs responsibility *
%elegate &lient ethod invo&ations to the b!siness ethods
Invo@e appropriate ethods d!ring an 6J7 ob*e&t &reation, reoval
and loo@!p
Provide &lasses that ipleent the hoe and reote inter"a&es o" the
bean
Invo@e 2a%a(.e2".&ession<ean inter"a&e and
&ession&ynchroni?ation inter"a&e &allba&@s at appropriate ties
Invo@e 2a%a(.e2".+ntity<ean inter"a&e "or entities and &allba&@s at
appropriate ties
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
W8
Bean provi"er&s
responsi'ilities
Container provi"er&s
responsi'ilities
WELCOME TO J2EE NOTES
Ipleent persisten&e "or entit# beans with &ontainer anaged
persisten&e
Provide 2a%a(.e2".&ession'onte(t and 2a%a(.e2".+ntity'onte(t "or
session and entit# bean instan&es, obtain the in"oration "ro &ontainer
Provide J0%I &onte+t with the beans environent to the bean
instan&es
Eanage transa&tion, se&!rit# and e+&eption "or beans
E=b(=ar "ile
Standard "orat !sed b# 6J7 tools "or pa&@aging (assebling) beans
along with de&larative in"oration
Contra&t between bean provider and appli&ation assebler, and
between appli&ation assebler and appli&ation deplo#er
The "ile in&l!desK
$Java &lass "iles o" the beans alo
Ginall#, the 7ig Pi&t!re
ean Varieties
Three Types of <eansK
Session 7eans ) Short lived and last d!ring a session(
6ntit# 7eans ) Long lived and persist thro!gho!t(
Eessage %riven 7eans $ .s#n&hrono!s Eessage Cons!ers
.s#n&hrono!s(
Session eans
. session ob*e&t is a non)persistent ob*e&t that ipleents soe
b!siness logi& r!nning on the server(
6+e&!tes on behal" o" a single &lient(
Can be transa&tion aware(
%oes not represent dire&tl# shared data in the database, altho!gh it a#
a&&ess and !pdate s!&h data(
Is relativel# short)lived(
Is reoved when the 6J7 &ontainer &rashes( The &lient has to re)
establish a new session ob*e&t to &ontin!e &op!tation
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
W?
WELCOME TO J2EE NOTES
Types o" Session eans
There are two t#pes o" session beansK
$ Stateless
$ State"!l
Eessage Cons!ers
'lient@s %ie# of a &ession <ean 4
. &lient a&&esses a session ob*e&t thro!gh the session beans 5eote
Inter"a&e or Lo&al Inter"a&e(
6a&h session ob*e&t has an identit# whi&h, in general, does not s!rvive
a &rash
Locating a session "ean@s home interface
,emote Home interface
Conte+t initialConte+t < new InitialConte+t():
Cart-oe &art-oe < (Cart-oe)
*ava+(ri(Portable5eote3b*e&t(narrow(initialConte+t(loo@!p(A*avaK&o
pFenvFe*bF&artB), Cart-oe(&lass):
Local Home $nterface
Conte+t initialConte+t < new InitialConte+t():
Cart-oe &art-oe < (Cart-oe)
initialConte+t(loo@!p(A*avaK&opFenvFe*bF&artB):
JN:' K !sed to lo&ate 5eote 3b*e&ts &reated b# bean(
portableRemoteOb=ect Class K It !ses an 3b*e&t ret!rn b# Loo@!p( )(
narro+8 9 )U Call the &reate( ) o" -oeInter"a&e(
'ntialConte,t Class K
Loo#!p8 9 )U Sear&hes and lo&ate the distrib!ted 3b*e&ts(
&ession <ean@s Local Home $nterface 4
ob*e&t that ipleents is &alled a session 6J7Lo&al-oe ob*e&t(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
WD
WELCOME TO J2EE NOTES
Create a new session ob*e&t(
5eove a session ob*e&t(
&ession <ean@s ,emote Home $nterface
ob*e&t that ipleents is &alled a session 6J7-oe ob*e&t(
Create a session ob*e&t
5eove a session ob*e&t
&ession <ean@s Local $nterface
Instan&es o" a session beans reote inter"a&e are &alled session
6J73b*e&ts
b!siness logi& ethods o" the ob*e&t(
&ession <ean@s Local Home $nterface
instan&es o" a session beans lo&al inter"a&e are &alled session
6J7Lo&al3b*e&ts
b!siness logi& ethods o" the ob*e&t
Creating an 6J7 3b*e&t
-oe Inter"a&e de"ines one or ore create(! ethods
.rg!ents o" the create ethods are t#pi&all# !sed to initiali/e the
state o" the &reated session ob*e&t
p!bli& inter"a&e Cart-oe e+tends *ava+(e*b(6J7-oe
;
Cart &reate(String &!stoer0ae, String a&&o!nt)
throws 5eote6+&eption, 7ad.&&o!nt6+&eption,
Create6+&eption:
>
&art-oe(&reate(AJohnB, ARMOQB):
+3<A"2ect or +3<LocalA"2ect
Client never dire&tl# a&&esses instan&es o" a Session 7eans &lass
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
WI
WELCOME TO J2EE NOTES
Client !ses Session 7eans 5eote Inter"a&e or 5eote -oe
Inter"a&e to a&&ess its instan&e
The &lass that ipleents the Session 7eans 5eote Inter"a&e or
5eote -oe Inter"a&e is provided b# the &ontainer(
&ession A"2ect $dentity
Session 3b*e&ts are eant to be private reso!r&es o" the &lient that
&reated the
Session 3b*e&ts, "ro the &lients perspe&tive, appear anon#o!s
Session 7eans -oe Inter"a&e !st not de"ine "inder ethods
&ession A"2ect $dentity
State"!l Session eans *
$. state"!l session ob*e&t has a !ni'!e identit# that is assigned b# the
&ontainer at the tie o" &reation(
$. &lient &an deterine i" two ob*e&t re"eren&es re"er to the sae
session ob*e&t b# invo@ing the is$dentical(+3<A"2ect other+3<A"2ect!
ethod on one o" the re"eren&es(
Stateless Session eans *
$.ll session ob*e&ts o" the sae stateless session bean, within the sae
hoe have the sae ob*e&t identit# assigned b# the &ontainer(
$is$dentical(+3<A"2ect other+3<A"2ect! ethod alwa#s ret!rns tr!e(
Container Responsibilities *
Eanages the li"e&#&le o" session bean instan&es(
0oti"ies instan&es when bean a&tion a# be ne&essar# (
Provides ne&essar# servi&es to ens!re session bean ipleentation is
s&alable and &an s!pport several &lients(
%ctivation an) &assivation *
Session bean &ontainer a# teporaril# trans"er state o" an idle state"!l
session bean instan&e to soe "or o" se&ondar# storage(
Trans"er "ro wor@ing set to se&ondar# storage is &alled instance
passivation-
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
WM
WELCOME TO J2EE NOTES
Trans"er ba&@ "ro the se&ondar# storage to the instan&e variables is
&alled instance activation-
Entity eans
Long Live 6ntit# 7eansP
. &oponent that represents an ob*e&t)oriented view o" soe entities
stored in a persistent storage li@e a database or an enterprise appli&ation(
Gro its &reation !ntil its destr!&tion, an entit# ob*e&t lives in a
&ontainer(
Transparent to the &lient, the Container provides se&!rit#, &on&!rren&#,
transa&tions, persisten&e, and other servi&es to s!pport the 6ntit# 7eans
"!n&tioning
$Cainer Eanaged Persisten&e vers!s 7ean Eanaged Persisten&e
E!ltiple &lients &an a&&ess an entit# ob*e&t &on&!rrentl#
Container hosting the 6ntit# 7ean s#n&hroni/es a&&ess to the entit#
ob*e&ts state !sing transa&tions
6a&h entit# ob*e&t has an identit# whi&h !s!all# s!rvives a transa&tion
&rash
3b*e&t identit# is ipleented b# the &ontainer with help "ro the
enterprise bean &lass
E!ltiple enterprise beans &an be deplo#ed in a Container
Remote Clients *
.&&esses an entit# bean thro!gh the entit# beans reote and reote
hoe inter"a&es
Ipleents 6J73b*e&t and 6J7-oe Inter"a&es
Lo&ation Independent
Potentiall# 6+pensive, 0etwor@ Laten&#
4se"!l "or &oarse grained &oponent a&&ess
Local Clients *
Lo&al &lient is a &lient that is &ollo&ated with the entit# bean and whi&h
a# be tightl# &o!pled to the bean(
Ipleents 6J7Lo&al3b*e&t and 6J7Lo&al-oe Inter"a&es
Sae JJE
6nterprise bean &an)not be deplo#ed on a node di""erent "ro that o" its
&lient $ 5estri&ts distrib!tion o" &oponents(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
WQ
WELCOME TO J2EE NOTES
7etter s!pports "ine)grained &oponent a&&ess
Locatin$ the Entity ean *
Lo&ation o" 6J7 Container is !s!all# transparent to Client
Client lo&ates 6ntit# 7eans -oe Inter"a&e !sing J0%I
B+(ample
Conte+t initialConte+t < new InitialConte+t():
.&&o!nt-oe a&&o!nt-oe < (.&&o!nt-oe)
initialConte+t(loo@!p(A*avaK&opFenvFe*bFa&&o!ntsB):
+ntity <ean@s ,emote Home $nterface
Container provides the ipleentation o" the 5eote -oe Inter"a&e
"or ea&h 6ntit# 7ean deplo#ed in the &ontainer
Container a@es the 5eote -oe Inter"a&e o" all 6ntit# 7eans
deplo#ed in it a&&essible to Clients thro!gh J0%I
The ob*e&t that ipleents an 6ntit# 7eans 5eote -oe Inter"a&e is
&alled an +3<Home ob*e&t
+ntity@ <ean@s ,emote Home $nterface
Create new entit# ob*e&ts within the hoe
Gind e+isting entit# ob*e&ts within the hoe
5eove an entit# ob*e&t "ro the hoe
Create Metho)s *
6ntit# 7eans 5eote -oe Inter"a&e &an de"ine !ltiple create(!
ethods, ea&h de"ining a wa# o" &reating an entit# ob*e&t
.rg!ents o" create(! initiali/e the state o" the entit# ob*e&t
5et!rn t#pe o" a create(! ethod is 6ntit# 7eans 5eote Inter"a&e
The thro#s &la!se o" ever# create(! ethod in&l!des the
2a%a.rmi.,emote+(ception and 2a%a(.e2".'reate+(ception
"inder Metho)s
6ntit# 7eans -oe Inter"a&e de"ines an# finder ethods
0ae o" ea&h finder ethod starts with the pre"i+ A"indB
.rg!ents o" a finder ethod are !sed b# the 6ntit# 7ean
ipleentation to lo&ate re'!ested entit# ob*e&ts
5et!rn t#pe o" a finder ethod !st be the 6ntit# 7eans 5eote
Inter"a&e, or a &olle&tion o" 5eote Inter"a&es
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
WR
WELCOME TO J2EE NOTES
The thro#s &la!se o" ever# finder ethod in&l!des the
2a%a.rmi.,emote+(ception and 2a%a(.e2".>inder+(ception
Entity eanZs Remote 'nter"ace
Client a&&esses an entit# ob*e&t thro!gh 6ntit# 7eans 5eote Inter"a&e
6ntit# beans 5eote Inter"a&e !st e+tend 2a%a(.e2".+3<A"2ect
inter"a&e
5eote Inter"a&e de"ines b!siness ethods whi&h are &allable b#
&lients
The &ontainer provides the ipleentation o" the ethods de"ined in
the 2a%a(.e2".+3<A"2ect inter"a&e
3nl# b!siness ethods are delegated to the instan&es o" the enterprise
bean &lass
Entity eanZs Local Home 'nter"ace
!st e+tend the *ava+(e*b(6J7Lo&al-oe inter"a&e
6a&h ethod !st be one o" theK
$Create ethods
$Gind ethods
$-oe ethods
Entity eanZs Local 'nter"ace
Lo&al &lient &an a&&ess an entit# ob*e&t thro!gh the entit# beans lo&al
inter"a&e(
!st e+tend the *ava+(e*b(6J7Lo&al3b*e&t inter"a&e(
de"ines the b!siness ethods &allable b# lo&al &lients(
&ersistence Mana$ement
%ata a&&ess proto&ol "or trans"erring state o" the entit# between the
6ntit# 7ean instan&es and the database is re"erred to as ob*e&t
persisten&e
There are two wa#s to anage this persisten&e d!ring an appli&ations
li"etie
<ean-managed
'ontainer-managed
ean Mana$e) &ersistence *
6ntit# 7ean provider writes database a&&ess &alls dire&tl# into the
enterprise bean &lass
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
WV
WELCOME TO J2EE NOTES
Container Eanaged Persisten&e
7ean Provider need not write database &alls in the bean
Container providers tools generate database a&&ess &alls at deplo#ent
tie
%)vanta$e* 6ntit# 7ean &an be ostl# independent "ro the data
so!r&e in whi&h the entit# is stored
:isa)vanta$e* Sophisti&ated tools are needed at deplo#ent tie to
ap 6ntit# 7ean "ields to data so!r&e
6J7 NL
0eed "or standardi/ing '!eries
Wh# not SNL?
6J7 NLK 6J7 N!er# Lang!age
$Spe&i"i&ation lang!age
$7ased on the CEP %ata Eodel (.bstra&t Persisten&e S&hea)
$Copiled to a target lang!ageK SNL
6J7 NL 6+aple
S6L6CT 37J6CT(l) Gro 3rder7ean o, in(o(lineItes) l

S6L6CT l(LI06IT6E[I% G53E LI06IT6E6J7T.7L6 l,


35%656J7T.7L6 o W-656 (l(35%65[I% < o(35%65[I% )
S6L6CT 37J6CT(o) G53E 3rder7ean o W-656
o(&reditCard(e+pires < HODFOMH=

S6L6CT o(35%65[I% G53E C56%ITC.5%6J7T.7L6 a[8,


35%656J7T.7L6 o W-656 ((a[8(6^PI56S<HODFOMH .0%
o(C56%ITC.5%[I% < a[8(C56%ITC.5%[I% ))
EJ [L E,ample
S6L6CT &(address
G53E C!stoer7eanS&hea &
W-656 &(i%<?8 .0% &("irst0ae<??
S6L6CT .%%56SS(I%
G53E .%%56SS, C4ST3E65
W-656 C4ST3E65(C4ST3E65I%<?
.0% C4ST3E65(GI5ST0.E6<?
.0% C4ST3E65(C4ST3E65I% <
.%%56SS(C4ST3E65I%
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
WW
WELCOME TO J2EE NOTES
EJ [L* :eployment :escriptor
S'!er#U
Sdes&riptionUEethod "inds large ordersSFdes&riptionU
S'!er#)ethodU
Sethod)naeU"ind.llC!stoersSFethod)naeU
Sethod)parasFU
SF'!er#)ethodU
Se*b)'lUS6L6CT 37J6CT(&) G53E C!stoer7eanS&hea
&SFe*b)'lU
SF'!er#U
Home !siness Metho)s
Eethods in the -oe Inter"a&e
Ipleentation provided b# 7ean Provider with at&hing
e*b-oeSethodU in the 7ean
6+posed to the Client Jiew
0ot spe&i"i& to an# 7ean instan&e
&elect Methods
%e"ined as abstra&t ethod in the 7ean &lass
$e*bSele&tSethodU
Spe&ial t#pe o" a '!er# ethod
Spe&i"ied !sing a 6J7 NL stateent
0ot e+posed to the Client Jiew
4s!all# &alled "ro a b!siness ethod
E,ample o" EJ .-. CM& ean
p!bli& &lass .&&o!nt7ean ipleents 6ntit#7ean ;
FF 7ean Instan&e Jariables
p!blic lon$ acco!ntGn!mberQ
p!blic =ava-lan$-Strin$ c!stomerGnameQ
p!blic )o!ble balanceQ
FF 7!siness Eethods
p!bli& void &redit ( do!ble ao!nt ) ;
balan&e \< ao!nt:
>
p!bli& void debit ( do!ble ao!nt ) ;
balan&e )< ao!nt:
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8OO
WELCOME TO J2EE NOTES
>
>
E,ample o" EJ 2-; CM& ean
p!bli& abstract &lass .&&o!nt7ean ipleents 6ntit#7ean ;
FF Virt!al @iel)s
p!bli& abstract long get.&&o!nt[n!ber():
p!bli& abstract void set.&&o!nt[n!ber(long a&&o!nt[n!ber):

p!bli& abstract *ava(lang(String getC!stoer[nae():
p!bli& abstract void setC!stoer[nae(String &!stoer[nae):
p!bli& abstract do!ble get7alan&e():
p!bli& abstract void set7alan&e(do!ble balan&e):
FF 7!siness Eethod
p!bli& void &redit ( do!ble ao!nt ) ;
do!ble balan&e < $etalance89Q
balan&e \< ao!nt:
setalance8balance9Q
> >
%bstract Schema * :eployment :escriptor
Sabstra&t)s&hea)naeUC!stoer7eanS&heaSFabstra&t)s&hea)
naeU
S&p)"ieldU
Sdes&riptionUid o" the &!stoerSFdes&riptionU
S"ield)naeUi%SF"ield)naeU
SF&p)"ieldU
S&p)"ieldU
Sdes&riptionUGirst nae o" the &!stoerSFdes&riptionU
S"ield)naeU"irst0aeSF"ield)naeU
SF&p)"ieldU
S&p)"ieldU
Sdes&riptionULast nae o" the &!stoerSFdes&riptionU
S"ield)naeUlast0aeSF"ield)naeU
SF&p)"ieldU
Spri@e#)"ieldUi%SFpri@e#)"ieldU
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8O8
WELCOME TO J2EE NOTES
Container Mana$e) Relationships*
Container Eanaged 5elationships S&r)"ieldU
7ean)7ean, 7ean)%ependent, %ependent)%ependent
%e"ined !sing .bstra&t .&&essor Eethods
4nidire&tional or 7i)dire&tional
$LineIte d Prod!&t
$St!dent de Co!rse
Car)inality
$3ne to 3ne
$3ne to Ean#
$Ean# to 3ne
$Ean# to Ean#
E,ample Entity ean* Or)er
p!bli& abstra&t 3rder7ean e+tends 6ntit# 7ean ;
FF Jirt!al Gileds S&p)"ieldsU
p!bli& abstra&t Long get3rderI%():
p!bli& abstra&t void set3rderI%(Long orderI%):
FF Jirt!al Gields S&r)"ieldsU
p!bli& abstra&t .ddress getShiping.ddress():
p!bli& abstra&t void setShiping.ddress (.ddress address):
p!bli& abstra&t Colle&tion getLineItes():
p!bli& abstra&t void setLineItes (Colle&tion lineItes):
>
E,ample Entity ean* &ro)!ct
p!bli& abstra&t 3rder7ean e+tends 6ntit# 7ean ;
FF Jirt!al Gields S&p)"ieldU
p!bli& abstra&t Long getProd!&tI%():
p!bli& abstra&t void setProd!&tI%(Long orderI%):
FF Jirt!al Gields S&p)"ieldU
p!bli& abstra&t String getProd!&tCategor#():
p!bli& abstra&t void setProd!&tCategor# (String &ategor#):
FF 03 $ 5elationship Gields
>
Relationships* :eployment :escriptor
Se*b)relationU
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8O?
WELCOME TO J2EE NOTES
Sdes&riptionU306)T3)306K C!stoer and
.ddressSFdes&riptionU
Se*b)relation)naeUC!stoer).ddressSFe*b)relation)naeU
Se*b)relationship)roleU
Se*b)relationship)role)naeU &!stoer has one addresss
SFe*b)relationship)role)naeU
S!ltipli&it#UoneSF!ltipli&it#U
Srelationship)role)so!r&eU
Se*b)naeUC!stoer7eanSFe*b)naeU
SFrelationship)role)so!r&eU
S&r)"ieldU
S&r)"ield)naeUaddressSF&r)"ield)naeU
SF&r)"ieldU
SFe*b)relationship)roleU
Se*b)relationship)roleU
Se*b)relationship)role)naeU.ddress belong to the C!stoer
SFe*b)relationship)role)naeU
S!ltipli&it#UoneSF!ltipli&it#U
S&as&ade)deleteFU
Srelationship)role)so!r&eU
Se*b)naeU.ddress7eanSFe*b)naeU
SFrelationship)role)so!r&eU
SFe*b)relationship)roleU
SFe*b)relationU
EJ
What is the )i""erence bet+een normal Java ob=ect an) EJ
Java Ob=ectKitHs a re!sable &oponet
EJ*is re!sable and deplo#able &oponent whi&h &an be deplo#ed in
an# &ontainer
6J7 K is a distrib!ted &oponent !sed to develop b!siness appli&ations(
Container provides r!ntie environent "or 6J7s(
6J7 is an Java ob*e&t ipleented a&&ording 6J7 spe&i"i&ation(
%eplo#abilit# is a "eat!re(
What is the )i""erence bet+een Javaean an) EJ
Java eans * is intra)pro&ess &oponent,
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8OD
WELCOME TO J2EE NOTES
Java7eans is parti&!larl# well)s!ited "or as#n&hrono!s, intra)
appli&ation &o!ni&ations aong so"tware
EJ K is an Inter)Pro&ess &oponent
What is EJ /
6nterprise Java 7ean is a spe&i"i&ation "or server)side
s&alable,transa&tional and !lti)!ser se&!re enterprise)level
appli&ations( It provides a &onsistant &oponent ar&hite&t!re "or
&reating distrib!ted n)tier iddleware(
6nterprise Java7eans (6J7) is a te&hnolog# that based on J?66
plat"or(
6J7s are server)side &oponents( 6J7 are !sed to develop the
distrib!ted, transa&tional and se&!re appli&ations based on Java
te&hnolog#(
What is Session ean- What are the vario!s types o" Session ean
SessioneansK The# are !s!all# asso&iated with one &lient( 6a&h session
bean is &reated and destro#ed b# the parti&!lar 6J7 &lient that is
asso&iated with it( These beans do not s!rvive a"ter s#ste sh!tdown(
These Session 7eans are o" two t#pesK

State"!l Session eansKThe# aintain &onversational state between
s!bse'!est &alls b# a &lient
b) State"!l Session 7eans K These beans have internal states( The# &an
be stored (get-andle()) and restored (get6J73b*e&t()) a&ross &lient
sessions(Sin&e the# &an be persisten&e, the# are also &alled as
Persisten&e Session 7eans(
Stateless Session eanKConsider this as a servlet e'!ivalent in 6J7( It is
*!st !sed to servi&e &lients regardless o" state and does not aintain an#
state(
a) Stateless Session 7eans K These beans do not have internal States(
The# need not be passivated( The# &an be pooled into servi&e !ltiple
&lients(
What is the )i""erence bet+een State"!l session bean an) Stateless
session bean
State"!l*
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8OI
WELCOME TO J2EE NOTES
State"!l s(7eans have the passivated and .&tive state whi&h the
Stateless bean does not have(
State"!l beans are also Persistent session beans( The# are designed to
servi&e b!siness pro&esses that span !ltiple ethod re'!ests or
transa&tions(
State"!l session beans reebers the previo!s re'!ests and reponses(
State"!l session beans does not have pooling &on&ept(
State"!l Session 7eans &an retain their state on behave o" an individ!al
&lient(
State"!l Session 7eans &an be passivated and re!ses the "or an#
&lients(
State"!l Session 7ean has higher per"oran&e over stateless sessiob
bean as the# are pooled b# the appli&ation server(
Stateless*
Stateless Session 7eans are designed to servi&e b!siness pro&ess that
last onl# "or a single ethod &all or re'!est(
Stateless session beans do not reeber the previo!s re'!est and
responses(
Stattless session bean instan&es are pooled(
Stateless Session 7eans donot aintain states(
Stateless Session 7eans, &lient spe&i"i& data has to be p!shed to the
bean "or ea&h ethod invo&ation whi&h res!lt in in&rease o" networ@
tra""i&(
What is the li"e cycle o" State"!l session bean
State"!l Session 7ean has three states( %oes not e+ists, Eethod 5ead#
and Passivated states(
Li@e Stateless beans, when the State"!l Session 7ean hasnt been
instantiated #et (so it is not an instan&e in eor#) is it in the %oes not
e+ists state(
3n&e a &ontainer &reates one or ore instan&es o" a State"!l
Session 7ean it sets the in a Eethod 5ead# state( In this state it &an
serve re'!ests "ro its &lients( Li@e Stateless Session 7eans, a new
instan&e is &reated (Class(newInstan&e()), the &onte+t is passed
(setSessionConte+t()) and "inall# the bean is &reated with e*bCreate()(
%!ring the li"e o" a State"!l Session 7ean, there are periods o"
ina&tivit#( In these periods, the &ontainer &an set the bean to the
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8OM
WELCOME TO J2EE NOTES
Passivate state( This happens thro!gh the e*bPassivate() ethod( Gro
the Passivate state the bean &an be oved ba&@ to the Eethod 5ead#
state, via e*b.&tivate() ethod, or &an go dire&tl# to the %oes 0ot 6+ists
state with e*b5eove()(
What is the li"e cycle o" Stateless session bean
Stateless session bean has onl# two statesK %oes 0ot 6+ists and Eethod
5ead# Pool(
. bean has not #et instantiated (so it is not an instan&e in
eor#) when it is in the %oes 0ot 6+ists state(
When the 6J7 &ontainer needs one or ore beans, it &reates and
set the in the Eethod 5ead# Pool state( This happens thro!gh the
&reation o" a new instan&e (Class(newInstan&e()), then it is set its
&onte+t (setSessionConte+t()) and "inall# &alls the e*bCreate() ethod(
The e*b5eove() ethod is &alled to ove a bean "ro the
Eethod 5ead# Pool ba&@ to %oes 0ot 6+ists state(
What are the call bac# metho)s in Session bean
Session bean &allba&@ ethods di""er whether it is Stateless or state"!l
Session bean( -ere the# are(
Stateless Session ean *(
8( setSessionConte+t()
?( e*bCreate()
D( e*b5eove()
State"!l Session ean *(
8( setSessionConte+t()
?( e*bCreate()
D( e*bPassivate()
I( e*b.&tivate()
M( e*b5eove()
When yo! +ill chose State"!l session bean an) Stateless session
bean
State"!l session bean is !sed when we need to aintain the &lient state (
6+aple o" state"!ll session is Shoping &art site where we need to
aintain the &lient state (
stateless session bean will not have a &lient state it will be in pool(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8OQ
WELCOME TO J2EE NOTES
To aintain the state o" the bean we pre"er state"!l session bean and
e+aple is to get ini stateent in
.TE we need sessions to be aintained(
What is Entity ean- What are the vario!s types o" Entity ean
6ntit# bean represents the real data whi&h is stored in the persistent
storage li@e %atabase or "ile s#ste( Gor e+aple, There is a table in
%atabase &alled Credit[&ard( This table &ontains
&redit[&ard[no,"irst[nae, last[nae, ssn as &ol!s and there are 8OO
rows in the table( -ere ea&h row is represented b# one instan&e o" the
entit# bean and it is "o!nd b# an !ni'!e @e# (priar# @e#)
&redit[&ard[no(
There are two t#pes o" entit# beans(
8) Container Eanaged Persisten&e(CEP)
?) 7ean Eanaged Presisten&e(7EP)
What is the )i""erence bet+een CM& an) M&
CEP eans Container Eanaged Persisten&e( When we write CEP
bean , we dont need to write an# J%7C &ode to &onne&t to %atabase(
The &ontainer will ta@e &are o" &onne&tion o!r enitt# beans "ields with
database( The Container anages the persisten&e o" the bean(
.bsol!tel# no database a&&ess &ode is written inside the bean &lass(
7EP eans 7ean Eanaged Persisten&e( When we write 7EP
bean, it is prograer responsiblit# to write J%7C &ode to &onne&t to
%atabase(
What is the li"ecycle o" Entity ean
The "ollowing steps des&ribe the li"e &#&le o" an entit# bean instan&e
.n entit# bean instan&es li"e starts when the &ontainer &reates the
instan&e !sing newInstan&e and then initialises it !sing
set6ntit#Conte+t(
The instan&e enters the pool o" available instan&es( 6a&h entit# bean
has its own pool( While the instan&e is in the available pool, the instan&e
is not asso&iated with an# parti&!lar entit# ob*e&t identit#( .n# o" these
pooled instan&es a# be !sed to e+e&!te "inder (e*bGind) or hoe
(e*b-oe) ethods(
.n instan&e transitions "ro the pooled state to the read# state when
the &ontainer sele&ts that instan&e to servi&e a &lient &all to an entit#
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8OR
WELCOME TO J2EE NOTES
ob*e&t( There are two possible transitions "ro the pooled to the read#
stateK thro!gh the &reation o" an entit# (e*bCreate and e*bPostCreate) or
thro!gh the a&tivation o" an entit# (e*b.&tivate)(
When an entit# bean instan&e is in the read# state, the instan&e is
asso&iated with a spe&i"i& entit# ob*e&t identit#( While the instan&e is in
the read# state, the &ontainer &an s#n&hroni/e the instan&e with its
representation in the !nderl#ing data so!r&e whenever it deterines the
need to !sing e*bLoad and e*bStore ethods( 7!siness ethods &an also
be invo@ed /ero or ore ties on an instan&e( .n e*bSele&t ethod &an
be &alled b# a b!siness ethod, e*bLoad or e*bStore ethod(
The &ontainer &an &hoose to passivate an entit# bean instan&e
within a transa&tion( To passivate an instan&e, the &ontainer "irst invo@es
the e*bStore ethod to allow the instan&e to prepare itsel" "or the
s#n&hroni/ation o" the database state with the instan&es state, and then
the &ontainer invo@es the e*bPassivate ethod to ret!rn the instan&e to
the pooled state(
There are three possible transitions "ro the read# to the pooled stateK
thro!gh the e*bPassivate ethod, thro!gh the e*b5eove ethod (when
the entit# is reoved), and be&a!se o" a transa&tion rollba&@ "or
e*bCreate, e*bPostCreate,or e*b5eove(
The &ontainer &an reove an instan&e in the pool b# &alling the
!nset6ntit#Conte+t() ethod on the instan&e(
What are the call bac# metho)s in Entity bean
Entity ean*
8( set6ntit#Conte+t()
?( e*bCreate()
D( e*bPostCreate()
I( e*b.&tivate()
M( e*bPassivate()
Q( e*b5eove()
R( !nset6ntit#Conte+t()
When yo! +ill chose CM& an) M&
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8OV
WELCOME TO J2EE NOTES
M&
) 7ean anaged persisten&e
) %eveloper has to write persisten&e &ode "or e*bLoad(),e*bStore() "or
entit# beans
) Sho!ld "ollow this approa&h onl# when its bare ne&essit# to write #o!r
own persisten&e logi&(4s!all# &ontainer anaged persisten&e is '!ite
s!""i&ient and less error prone(
CM&
) Container anaged persisten&e
) %eveloper aps the bean "ields with the database "ields in the
deplo#ent des&riptors(
) %eveloper need not provide persisten&e logi& (J%7C) within the bean
&lass(
) Containiner anages the bean "ield to %7 "ield s#n&hroni/ation(
The point is onl# 60TIT2 beans &an have theier pesristen&e
e&hanis as CEP or 7EP( Session beans, whi&h !s!all# &ontains
wor@"low or b!siness logi& sho!ld never have persisten&e &ode(In&ase
#o! &hoose to write persisten&e within #o!r session bean, its !se"!ll to
note that the persisten&e is anaged b# the &ontainer 7EP(Session
beans &annot be CEP and its not possibel to provide "ield apping "or
session bean(
7EPs are !&h harder to develop and aintain than CEPs(.ll
other things are being e'!al,&hoose CEPs over 7EPs "or p!re
aintainabilit#(
There are liitations in the t#pes o" the data so!r&es that a# be
s!pported "or CEPs b# a &ontainer provide(S!pport "or non J%7C t#pe
data so!r&es,s!&h as CICS,are not s!pported b# the &!rrent CEP
apping and invo&ation s&hea(There"ore a&&essing these wo!ld
re'!ire a 7EP(
Cople+ '!eries ight not be possible with the basi& 6J7NL "or
CEPs(So pre"er 7EPs "or &ople+ '!eries(
I" relations between entit# beans are established then CEPs a#
be ne&essar#(CE5 has abilit# to de"ine anage relationships between
entit# beans(
Container will t#r to optii/e the SNL &ode "or the CEPs,so
the# a# be s&alable entit# beans than the 7EPs(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8OW
WELCOME TO J2EE NOTES
7EPs a# be inappropriate "or larger and ore per"oran&e
sesitive appli&ations(
What are a)vanta$es an) )isa)vanta$es o" CM& an) M&
CM&* Container anaged persisten&e
.dvantagesK
8)6as# to develop and aintain(
?)5elationships &an be aintained between di""erent entities(
D)3ptii/ation o" SNL &ode will be done(
I)Larger and ore per"oran&e appli&ations &an be done(
%isadvantagesK
8)Will not s!pport "or soe nonJ%7C data so!r&es,i(e,CICS(
?)Cople+ '!eries &annot be developed with 6J7NL(
M&** 7ean anaged persisten&e
.dvantagesK
8)S!pport "or nonJ%7C data so!r&es(
?)Cople+ '!eries &an be b!ild(
%isadvantagesK
8)-ard to develop and aintain(
?)We &annot aintain the relationships between di""erent entities(
D)3ptii/ation o" SNL &ode &annot be done b# the &ontainer,be&a!se
bean it sel" &ontains the &ode(
I)0ot appropriate "or larger and &ople+ appli&ations(
What is )i""erence bet+een EJ .-. an) EJ 2-;
6J7 ?(O adds the lo&al beans, whi&h are a&&essible onl# "ro within the
JJE where beans are r!nning in(
In 6J7 8(8, we had to ipleent reote &lient views "or all these beans,
even i" we had no reote &lients(
What is Messa$e :riven ean
Eessage %riven 7ean (E%7) is an enterprise bean whi&h r!ns inside
the 6J7 &ontainer and it a&ts as Listener "or the JES as#n&hrono!s
essage ( It does not have -oe and 5eote inter"a&e as Session or
6ntit# bean( It is &alled b# &ontainer when &ontainer re&eives JES
as#n&hrono!s essage( E%7 has to ipleent EessageListener whi&h
has a ethod onEessage(Eessage sg)( When the &ontainer &alls the
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
88O
WELCOME TO J2EE NOTES
E%7 it passes the essage to onEesage() ethod and then E%7
pro&ess that essage(
What is the li"e cycle o" M:
The li"etie o" an E%7 instan&e is &ontrolled b# the &ontainer( 3nl#
two states e+istK %oes not e+ist and 5ead# , as ill!strated in the
"ollowing "ig!reK
The li"e o" an E%7 instan&e starts when the &ontainer invo@es
newInstan&e() on the E%7 &lass to &reate a new instan&e( 0e+t, the
&ontainer &alls setEessage%rivenConte+t() "ollowed b# e*bCreate() on
the instan&e( The bean then enters the 5ead# state and is read# to
&ons!e essages(
When a essage arrives "or the bean, the &ontainer invo@es the
onEessage() ethod o" one o" the available instan&es, passing a
Eessage ob*e&t in arg!ent( Eessage s &an be &ons!ed and pro&essed
&on&!rrentl# b# !sing !ltiple instan&es o" the sae t#pe(
The &ontainer invo@es e*b5eove() on the bean instan&e when it
no longer needs the instan&e( The bean instan&e &an per"or &lean !p
operations here(
What is local inter"ace- Ho+ val!es +ill be passe)
I" Client and 6J7 &lasses are in the sae a&hine ( Sae JJE) then we
&an !se Lo&al linter"a&e instead o" 5eote inter"a&e( Sin&e Client and
6J7 are in sae JJE, val!es are passed b# re"eran&e(
What is the )i""erence bet+een local inter"ace an) remote inter"ace
We &an des&ribe the "ollowing &oon r!les "or &hoosing whether to
!se reote &lient view or lo&al &lient viewK
When #o! will potentiall# !se a distrib!ted environent (i" #o!r
enterprise bean sho!ld be independent o" its deplo#ent pla&e), #o!
sho!ld obvio!sl# &hoose reote &lient view(
4se reote &lient view when #o! need to be s!re that paraeters
passed between #o!r 6J7 and the &lient (andFor other enterprise beans)
sho!ld be passed =b# val!e= instead o" =b# re"eren&e(= With pass)b#)
val!e, the bean will have its own &op# o" the data, &opletel# separated
"ro the &op# o" the data at the &lient( With lo&al &lient view, #o! &an
do pass)b#)re"eren&e, whi&h eans #o!r bean, as well as the &lient, will
wor@ dire&tl# with one &op# o" the data( .n# &hanges ade b# the bean
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
888
WELCOME TO J2EE NOTES
will be seen b# the &lient and vi&e versa( Pass)b#)re"eren&e eliinates
tieFs#ste e+penses "or &op#ing data variables, whi&h provides a
per"oran&e advantage(
I" #o! &reate an entit# bean, #o! need to reeber that it is
!s!all# !sed with a lo&al &lient view( I" #o!r entit# bean needs to
provide a&&ess to a &lient o!tside o" the e+isting JJE (i(e(, a reote
&lient), #o! t#pi&all# !se a session bean with a reote &lient view( This
is the so)&alled Session Ga&ade pattern, the goal o" whi&h is that the
session bean provides the reote &lient a&&ess to the entit# bean(
I" #o! want to !se &ontainer)anaged relationship (CE5) in #o!r
enterprise bean, #o! !st e+pose lo&al inter"a&es, and th!s !se lo&al
&lient view( This is entioned in the 6J7 spe&i"i&ation(
6nterprise beans that are tightl# &o!pled logi&all# are good
&andidates "or !sing lo&al &lient view( In other words, i" one enterprise
bean is alwa#s asso&iated with another, it is per"e&tl# appropriate to &o)
lo&ate the (i(e(, deplo# the both in one JJE) and organi/e the
thro!gh a lo&al inter"a&e(
What is EJ [!ery Lan$!a$e
6J7 NL is soewat siilar to SNL( 7!t e*b 'l is !sed to retrieve data
"ro bean ob*e&ts where as s'l is !sed to retrieve data "ro tables(
What is %C':
%C': is releated to transa&tions( It is an a&ron#a o" .toi&,
Consistent, Isolation and %!rable( Transa&tion !st "ollowing the above
"o!r properties to be a better one
%tomicK It eans a transa&tion !st e+e&!te all or nothing at all(
ConsistentK Consisten&# is a transa&tional &hara&teristi& that !st be
en"or&ed b# both the transa&tional s#ste and the appli&ation developer
'solationK Transaation !st be allowed to r!n itsel"t witho!t the
inter"eren&e o" the other pro&ess or transa&tions(
:!rableK %!rablit# eans that all the data &hanges that ade b# the
transa&tion !st be written in soe t#pe o" ph#si&al storage be"ore the
transa&tion is s!&&ess"!ll# &opleted( This ens!res that transa&itons are
not lost even i" the s#ste &rashes(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
88?
WELCOME TO J2EE NOTES
What are the vario!s isolation levels in a transaction an)
)i""erences bet+een them
There are three isolation levels in Transa&tion( The# are
8( %irt# reads ?(0on repeatable reads D( Phanto reads(
:irrty Rea)s* I" transa&tion . !pdates a re&ord in database "ollowed
b# the transa&tion 7 reading the re&ord then the transa&tion . per"ors
a rollba&@ on its !pdate operation, the res!lt that transa&tion 7 had read
is invalid as it has been rolled ba&@ b# transa&tion .(

NonRepeatable Rea)s KI" transa&tion . reads a re&ord, "ollowed b#
transa&tion 7 !pdating the sae re&ord, then transa&tion . reads the
sae re&ord a se&ond tie, transa&tion . has read two di""erent val!es
"or the sae re&ord(
&hantom Rea)s *I" transa&tion . per"ors a '!er# on the database
with a parti&!lar sear&h &riteria (W-656 &la!se), "ollowed b#
transa&tion 7 &reating new re&ords that satis"# the sear&h &riteria,
"ollowed b# transa&tion . repeating its '!er#, transa&tion . sees new,
phanto re&ords in the res!lts o" the se&ond '!er#(
What are the vario!s transaction attrib!tes an) )i""erences bet+een
them
There are si+ transa&tion attrib!tes that are s!pported in 6J7(
8( 5e'!ired ) T8)))T8
O)))T8
?( 5e'!ires0ew $ T8)))T?
O)))T8
D( Eandator# ) T8)))T8
O)))6rror
I( S!pports ) T8)))T8
O)))O
M( 0otS!pported ) T8)))O
O)))O
Q( 0ever ) T8)))6rror
O)))O
What is the )i""erence bet+een activation an) passivation
.&tivation and Passivation is appili&able "or onl# State"!l session bean
and 6ntit# bean(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
88D
WELCOME TO J2EE NOTES
When 7ean instan&e is not !sed "or a while b# &lient then 6J7
Container reoves it "ro eor# and p!ts it in se&ondar# storage
(o"ten dis@) so that the eor# &an be re!sed( This is &alled
Passivation(
When Client &alls the bean instan&e again then Container ta@es the
passivated bean "ro se&ondar# storage and p!ts it in eor# to serve
the &lient re'!est( This is &alled .&tivation(
What is 'nstance poolin$
pooling o" instan&es(
in stateless session beans and 6ntit# 7eans server aintains a pool o"
instan&es(whenever server got a re'!est "ro &lient, it ta@es one instan&e
"ro the pool and serves the &lient re'!est(
What is the )i""erence bet+een HTT&Session an) State"!l Session
ean
Gro a logi&al point o" view, a ServletFJSP session is siilar to an 6J7
session( 4sing a session, in "a&t, a &lient &an &onne&t to a server and
aintain his state(
7!t, is iportant to !nderstand, that the session is aintained in
di""erent wa#s and, in theor#, "or di""erent s&opes(
. session in a Servlet, is aintained b# the Servlet Container
thro!gh the -ttpSession ob*e&t, that is a&'!ired thro!gh the re'!est
ob*e&t( 2o! &annot reall# instantiate a new -ttpSession ob*e&t, and it
does not &ontains an# b!siness logi&, b!t is ore o" a pla&e where to
store ob*e&ts(
. session in 6J7 is aintained !sing the Session7eans( 2o!
design beans that &an &ontain b!siness logi&, and that &an be !sed b# the
&lients( 2o! have two di""erent session beansK State"!l and Stateless(
The "irst one is soehow &onne&ted with a single &lient( It aintains the
state "or that &lient, &an be !sed onl# b# that &lient and when the &lient
=dies= then the session bean is =lost=(
. Stateless Session 7ean does not aintain an# state and there is
no g!arantee that the sae &lient will !se the sae stateless bean, even
"or two &alls one a"ter the other( The li"e&#&le o" a Stateless Session 6J7
is slightl# di""erent "ro the one o" a State"!l Session 6J7( Is 6J7
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
88I
WELCOME TO J2EE NOTES
Containers responsabilit# to ta@e &are o" @nowing e+a&tl# how to tra&@
ea&h session and redire&t the re'!est "ro a &lient to the &orre&t instan&e
o" a Session 7ean( The wa# this is done is vendor dependant, and is part
o" the &ontra&t(
What is the )i""erence bet+een "in) an) select metho)s in EJ
sele&t ethod is not there in 6J7s
. sele&t ethod is siilar to a "inder ethod "or 6ntit# 7eans, the# both
!se 6J7)NL to de"ine the seanti&s o" the ethod(
The# di""er in that an e*bSele&t ethod(s) are not e+posed to the &lient
and the e*bSele&t ethod(s) &an ret!rn val!es that are de"ined as &p)
t#pes or &r)t#pes(
What are the optional cla!ses in EJ [L
Three optional &la!ses are available in 6J7 Nl(
8( S6L6CT
?( G53E
D( W-656
The 6J7 NL !st alwa#s &ontain S6L6CT and G53E &la!ses(
The W-656 &la!se is optional(
The G53E &la!se provides de&larations "or the identi"i&ation variables
based on abstra&t s&hea nae, "or navigating thro!gh the s&hea( The
S6L6CT &la!se !ses these identi"i&ation variables to de"ine the ret!rn
t#pe o" the '!er#, and the W-656 &la!se de"ines the &onditional '!er#(
What is han)le in EJ
To get hold the session state o" the State"!l Session bean(
. handle is an abstra&tion o" a networ@ re"eren&e to an 6J7 ob*e&t( .
handle is intended to be !sed as a =rob!st= persistent re"eren&e to an
6J7 ob*e&t(
What is the )i""erence bet+een JN:' conte,t< 'nitial conte,t< session
conte,t an) e=b conte,t
JN:' Conte,t Provides a e&hanis to loo@!p reso!r&es on the
networ@
'nitial Conte,t &onstr!&tor provides the initial &onte+t(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
88M
WELCOME TO J2EE NOTES
Session Conte,t has all the in"oration a session bean wo!ld re'!ire
"ro the &ontainer
Entity Conte,t has all the in"oration that an 6ntit# bean wo!ld need
"ro a &ontainer
E=b Conte,t &ontains the in"oration that is &oon to both the
session and entit# bean(
What is the )i""erence bet+een sessionconte,t an) entityconte,t
Session Conte+t Contains in"oration that a Session 7ean wo!ld
re'!ire "ro the &ontainer
6ntit# Conte+t &ontains the in"oration that an 6ntit# 7ean wo!ld
re'!ire "ro a &ontainer
What is the )i""erence bet+een E%R< J%R an) W%R "ile
In J?66 appli&ation od!les are pa&@aged as 6.5, J.5 and W.5
based on their "!n&tionalit#
J%R* Java %rchive @ile
6J7 od!les whi&h &ontains enterprise *ava beans &lass "iles and 6J7
deplo#ent des&riptor are pa&@ed as J.5 "iles with (*ar e+tenstion
W%R * Web %rchive @ile
Web od!les whi&h &ontains Servlet &lass "iles,JSP GIles,s!pporting
"iles, GIG and -TEL "iles are pa&@aged as J.5 "ile with (war( web
a&hive) e+tension
E%R * Enterprise @ile
.ll above "iles((*ar and (war) are pa&@aged as J.5 "ile with (ear
( enterprise ar&hive) e+tension and deplo#ed into .ppli&ation Server(
What is )eployment )escriptor
%eplo#ent %es&riptor is a ^EL do&!ent with (+l e+tenion( It
basi&all# des&ripes the deplo#ent settings o" an appli&ation or od!le
or the &oponent( .t r!ntie J?66 server reads the deplo#ent
des&riptor and !nderstands it and then a&ts !pon the &oponent or
od!le based the in"oration entioned in des&riptor(
Gor e+aple 6J7 od!le has a deplo#ent des&riptor e*b)*ar(+l
where we ention whether it is session or entit# or esage driven bean
and where is the hoe, reore and 7ean &lasses are lo&ated and what
t#pe o" transa&tion et&( In a siple word, witho!t deplo#ent des&ritor
the Container ( 6J7FServletFJSP &ontainer) will not @now what to do
with that od!le(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
88Q
WELCOME TO J2EE NOTES
%eplo#ent %es&riptor is a "ile lo&ated in the W67)I0G dire&tor# that
&ontrols the behavio!r o" Servlets and JSP(
The "ile is &alled Web(+l and &ontains
+l-eader
Web(+l %3CT2P6 Sevlet nae
Web)appeleents )))))) Servlet Class
Init)par
Servlet Con"i$!ration *
Sweb)appU
SServletU
SServlet)naeU.dinSFServlet)naeU
SServlet)ClassU&o(ds(.dinServletSFServlet)&lassU
SFServletU
Cinit-paramD
Cparam-%alueD C/param-%alueD
Cparam-nameD admin.comC/param-nameD
C/init-paramD
SServlet)appingU
SServlet)naeU.dinSFServlet)naeU
S!rl)patternUF.dinSF!rl)patternU
SFServlet)appingU
SFweb)appU
EJ :eployment )escriptor *
6*b)*ar(+l
E6T.)I0G
Weblogi&)e*b)*ar(+l
Se*b)*arU
Senterprise)beanU
SFSessionU
Se*b)naeUState"!l"ina&ial&al&!SFe*b)naeU
ShoeU"in&al(state"!l("in&al&SFhoeU
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
88R
WELCOME TO J2EE NOTES
SreoteU "in&al(state"!l("in&al& SFreoteU
Se*b)ClassU "in&al(state"!l("in&al&6J7 Se*b)ClassU
Ssession)t#peU State"!l SFsession)t#peU
Stransa&tion)t#peU Container SFtransa&tion)t#peU
SFSessionU
SFenterprise)beanU
Sassebl#)des&riptorU
S&ontainer)transa&tionU
SethodU
Se*b)naeU State"!l"ina&ial&al&! SFe*b)naeU
Sethod)naeU 9 SFethod)naeU
SFethodU
Stransa&tion)attrib!teU s!pports SFtransa&tion)attrib!teU
SF&ontainer)transa&tionU
Sassebl#)des&riptorU
Se*b)*arU
+eblo$ic(e=b(=ar-,ml
Sweblogi&)e*b)*arU
Sweblogi&)enterprise)beanU
Se*b)naeU State"!l"ina&ial&al&! SFe*b)naeU
S*ndi)naeU state"!l"ina&al& SF*ndi)naeU
SFweblogi&)enterprise)beanU
SFweblogi&)e*b)*arU
What is CMR
CE5 ) Container Eanaged 5elationships allows the developer to
de&lare vario!s t#pes o" relationships between the entit# beans
What is the )i""erence bet+een CM& .-. an) CM& 2-;
CE5 and s!b &lassing o" the CEP bean b# the &ontainer
What is the )i""erence bet+een optimistic loc#in$ an) pessimistic
loc#in$
Aptimistic locing ass!es that no one wo!ld read or &hange the data
while &hanges are being b# a bean
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
88V
WELCOME TO J2EE NOTES
Pessimistic locing wo!ld rather lo&@ down the data so that no one &an
a&&ess it
What is la>y loa)in$
La/# loading is a &hara&teristi& o" an appli&ation when the a&t!al
loading and instantiation o" a &lass is dela#ed !ntil the point *!st be"ore
the instan&e is a&t!all# !sed( The goal is to onl# dedi&ate eor#
reso!r&es when ne&essar# b# onl# loading and instantiating an ob*e&t at
the point when it is absol!tel# needed(
Tools s!&h as 6&lipse have pop!lari/ed the la/#)loading
approa&h as the# !se the "a&ilit# to &ontrol the load and initiali/ation o"
heav#weight pl!g)ins( This gives the do!ble bon!s o" speeding !p the
initial load tie "or the appli&ation, as not all pl!g)ins are loaded
straightawa#: ens!ring e""i&ien&# as onl# the pl!g)ins that are !sed are
loaded at all(
's :ecorator an EJ )esi$n pattern
0o , I" i throw a &!sto .ppli&ation6+&eption "ro a b!siness
ethod in 6ntit# bean whi&h is parti&ipating in a transa&tion, wo!ld the
transa&tion be rolled ba&@ b# &ontainer( %oes &ontainer rolls ba&@
transa&tion onl# in &ase o" S#ste6+&eptions
2es, the rollba&@ will o&&!r Gor de&larative transa&tions, &ontainer will
rollba&@ on s#ste6+&eption( Container has no wa# to @now whether a
spei&i"i& appli&ation e+&eption is serio!s eno!gh to roll ba&@ the
parti&ipated transa&tion( 4se set5ollba&@3nl#() to doo the transa&tion(
What are simple r!les that a &rimary #ey class has to "ollo+
Ipleent the e'!als and hash&ode ethods(
What is abstract schema
CEP !ses abstra&t s&hea to ap to the ph#si&al database
What is re(entrant- 's session beans reentrant- 's entity beans
reentrant
5e)entrant eans where 7ean . &alls ethods o" 7ean 7 and then 7ean
7 t!rns aro!nd and &alls ethods o" 7ean .( The above all within a
single thread o" &ontrol( This is also &alled as loopba&@(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
88W
WELCOME TO J2EE NOTES
6ntit# beans are the onl# one bean that is reentrant( 0either
Session bean nor Eessage %riven 7ean are reentrant( When 6ntit# bean,
we have to de&lare in the deplo#ent des&riptor whether it is reentrant
( tr!e or "alse)(
Wh# an onEessage &all in Eessage)driven bean is alwa#s a
seperate transa&tion
The E%7 is stateless and inherentl# ea&h essage is !ni'!e with
respe&t to the E%7(
6a&h essage needs to be pro&essed independentl#( -en&e the need "or
separate transa&tions
:oes State"!l Session bean s!pport instance poolin$
.ll 7eans s!pport Instan&e Pooling
state"!ll session bean does not aintain instan&e pooling,
stateless session beans and entity beans &an aintain instan&e pooling
Why )oes EJ nee)s t+o inter"aces8Home an) Remote 'nter"ace9
-oe is to provide Loo@!p "ro J0%I while 5eote is to provide
3b*e&t Instantiated
Can ' invo#e R!ntime-$c89 in an EJ
0o
Can a Session ean be )e"ine) +itho!t e=bCreate89 metho)
0o
Why are e=b%ctivate89 an) e=b &assivate89 incl!)e) "or stateless
session bean even tho!$h they are never re?!ire) as it is
nonconversational bean
To have a &onsistent inter"a&e, so that there is no di""erent inter"a&e
that #o! need to ipleent "or State"!l Session 7ean and Stateless
Session 7ean( 7oth Stateless and State"!l Session 7ean ipleent
*ava+(e*b(Session7ean and this wo!ld not be possible i" stateless session
bean is to reove e*b.&tivate and e*bPassivate "ro the inter"a&e( 2o!
&o!ld arg!e that the two (state"!l and stateless) are so di""erent that the#
sho!ld have their own inter"a&e b!t S!n did not thin@ so( The# ade
both session beans ipleent the sae inter"a&e and provided
deplo#ent des&riptor to denote whi&h one is it that #o! are deplo#ing(
With 6J7 8(8 spe&s, wh# is !nsetSessionConte+t() not
provided in Session 7eans, li@e !nset6ntit#Conte+t() in 6ntit# 7eans
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8?O
WELCOME TO J2EE NOTES
This was the answer Provided b# soe one((( .&&ording to
Eohan this one is not &orre&t( Please see EohanHs repl# below and ore
in the &oents se&tion(
e*b5eove() is &alled "or session beans ever# tie the &ontainer
destro#es the bean( So #o! &an !se this ethod to do the st!"" #o!
t#pi&all# wo!ld do in !nset6ntit#Conte+t()( Gor entit# beans
e*b5eove() is onl# &alled i" the !ser e+pli&itl# deletes the bean( I thin@
that is the reason wh# the engineers at S40 invented the
!nset6ntit#Conte+t() "or this @ind o" bean(
What is the )i""erence bet+een e=bStore89 an) e=bLoa)89
When the 6J7 &ontainer needs to s#n&hroni/e the instan&e variables
o" an entit# bean with the &orresponding val!es stored in a database, it
invo@es the e*bLoad and e*bStore ethods( The e*bLoad ethod
re"reshes the instan&e variables "ro the database, and the e*bStore
ethod writes the variables to the database( The &lient &annot &all
e*bLoad and e*bStore(
What is the )i""erence bet+een e=bCreate89 an) e=b&ostCreate89
Session and Eessage %riven 7ean will have onl# e*bCreate() ethod
and no e*bPostCreate() ethod( 6ntit# bean will have both e*bCreate()
and e*bPostCreate() ethods(
The e*bPostCreate ethod ret!rns void, and it has the sae
inp!t paraeters as the e*bCreate ethod( I" we want to set a
relationship "ield to initiali/e the bean instan&e, we sho!ld do so in the
e*bPostCreate ethod( we &annot set a relationship "ield in the e*bCreate
ethod(
The e*bPostCreate() allows the bean to do an# post)&reate
pro&essing be"ore it begins serving &lient re'!ests( Gor ever# e*bCreate()
there !st be a at&hing (at&hing arg!ents) e*bPostCreate() ethod(
's stateless Sessiob bean create89 metho) contains any parameters
0o( This ethod !st not &ontain an# inp!t paraeters and &annot be
overloaded as well(
Ho+ can i retrieve "rom insi)e my ean8Stateless session an) Entity
CM&9 the !ser name +hich i am servin$ 8the !ser name o" !ser =!st
lo$$e) in my +eb application9
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8?8
WELCOME TO J2EE NOTES
Inside an 6J7 #o! a# retrieve the =Caller= nae, that is the login id
b# invo@ingK sessionConte+t(getCallerIdentit#()(get0ae() where
sessionConte+t is the instan&e o" =SessionConte+t= (setSessionConte+t)
passed to the Session 7ean, or the instan&e o" =6ntit#Conte+t=
(set6ntit#Conte+t) passed to the 6ntit# 7ean(
What is EJ architect!re8components9
6J7 .r&hite&t!re &onsists o" K
a) 6J7 Server
b) 6J7 &ontainers that r!n on these servers,
&) -oe 3b*e&ts, 5eote 6J7 3b*e&ts and 6nterprise 7eans that r!n
within these &ontainers,
d) 6J7 Clients and
e) .!+illar# s#stes li@e J0%I (Java 0aing and %ire&tor# Inter"a&e),
JTS(Java Transa&tion Servi&e) and se&!rit# servi&es(
I" # session bean with single ethod insert re&ord into ?
entit# beans, how &an I @now that the pro&ess is done in sae
transa&tion (the attrib!tes "or these beans are 5e'!ired)
I" #o!r ethod in the session bean is alread# r!nning !nder a transa&tion
the &alls to an# other bean whi&h have been deplo#ed with trans)
attrib!te H5e'!iredH will be e+e&!ted within the sae transa&tion &onte+t(
So i" #o!r session bean is !sing &ontainer)anaged
transa&tions and #o!r ethod is deplo#ed with H5e'!iredH,
H5e'!ires0ewH or HEandator#H, #o! &an sa"el# ass!e that the &alls to
#o!r entit# beans are handled !nder sae transa&tion( I" #o!Hre not
r!nning in a transa&tion, a separate transa&tion will be set !p "or ea&h
&all to #o!r entit# beans(
I" #o!r session bean is !sing bean)anaged transa&tions, #o! &an ens!re
that the &alls are handled in the sae transa&tion b# K
*ava+(transa&tion(4serTransa&tion tran< n!ll:
tr#;
tran<&t+(get4serTransa&tion():
tran(begin():
#7ean-oe8(&reate((((():
#7ean-oe?(&reate(((():
tran(&oit():
>&at&h(((();>
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8??
WELCOME TO J2EE NOTES
2o! a# want to &he&@ i" #o!Hre alread# r!nning in a transa&tion b#
&alling tran(getStat!s()(
's there a +ay to $et the ori$inal e,ception ob=ect "rom insi)e a
neste) or +rappe) E,ception 8"or e,ample an EJE,ception or
RemoteE,ception9
.bsol!tel# #es, b!t the wa# to do that depends on the 6+&eption,
sin&e there are no standards "or that( Soe e+aplesK fWhen #o! have
an *ava+(e*b(6J76+&eption, #o! &an !se the getCa!sed7#6+&eption()
that ret!rns a *ava(lang(6+&eption( f. *ava(ri(5eote6+&eption there is
a p!bli& "ield &alled detail o" t#pe *ava(lang(Throwable fWith a
*ava(s'l(SNL6+&eption #o! need to !se the ethod get0e+t6+&eption()
to get the &hained *ava(s'l(SNL6+&eption( fWhen #o! have an
*ava(lang(re"le&t(Invo&ationtarget6+&eption, #o! &an get the thrown
target *ava(lang(Throwable !sing the getTarget6+&eption() ethod(
Can !n)e"ine) primary #eys are possible +ith Entity beans/'" so<
+hat type is )e"ine)/
2es,!nde"ined priar# @e#s are possible with 6ntit# 7eans(The t#pe
is de"ined as *ava(lang(3b*e&t(
When t+o entity beans are sai) to be i)entical/Which metho) is
!se) to compare i)entical or not/
Two 6ntit# 7eans are said to be Identi&al,i" the# have the sae
hoe inte"a&e and their priar# @e#s are the sae(To test "or this ,#o!
!st !se the &oponent inte"a&eHs isIdenti&al() ethod(
Why CM& beans are abstract classes/
We have to provide abstra&t data to ob*e&t apping that aps the
"ields in o!r bean to a batabase, and abstra&t ethods ethods that
&orelate these "ields(
's instance poolin$ necessary "or entity beans/
3ne o" the "!ndaental &on&epts o" 6ntit# 7eans is that the# are
the pooled ob*e&ts(Instan&e pooling is the servi&e o" the &ontainer that
allows the &ontainer to re!se bean instan&es,as opposed to &reating new
ones ever# tie a re'!est "or a bean is ade(This is a per"oan&e
optii/atio done b# the &ontainer(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8?D
WELCOME TO J2EE NOTES
What is the )i""erence bH+ sen)Re)irect89 an) C=sp* "or+ar)E/
sendredire&t will happen on &lint side L re'!est , rsponse will be
newl# &reated, "or "orward a&tion it is server side a&tion L re'!est,
response is passed L not odi"ied or destro#ed(
Ho+ the abstract classes in CM& are converte) into concrete
classes/
6J7?(O allows developer to &reate onl# abstra&t &lasses and at the
tie o" deplo#eent the &ontainer &reates &on&rete &lasses o" the
abstra&t( It is eas# "or &ontainer to read abstra&t &lasses and
appropriatel# generate &on&rete &lasses(
[!estions
8). developer s!&&ess"!ll# &reating and tests a state"!l bean "ollowing
deplo#ent, interittent
=0!llpointer6+&eption= begin to o&&!r, parti&!larl# when the server is
hardl# loaded( What ost li@el# to
related proble(
a) setSessionConte+t b) e*bCreate &) e*bPassivate d)
be"oreCopletion e) e*bLoad
?)? e+aple ipleentations os Pro+# are 5EI L 6Jb
D)I" an 5EI paraeter ipleents *ava(ri(5eote, how is it passed
=on)the)wire?=
Choi&e 8 K It &an never be passed(
Choi&e ? K It is passed b# val!e(
Choi&e D K It &annot be passed be&a!se it ipleents
*ava(ri(5eote(
Choi&e I K It &annot be passed !nless it .LS3 ipleents
*ava(io(Seriali/able(
Choi&e M K It is passed b# re"eren&e( ans)?
I)p!bli& s#n&hroni/ed void t+Test(int i) ;
S#ste(o!t(println(=Integer isK = \ i): >
What is the o!t&oe o" attepting to &opile and e+e&!te the ethod
above, ass!ing it is ipleented
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8?I
WELCOME TO J2EE NOTES
in a state"!l session bean?
Choi&e 8 K 5!n)tie error when bean is &reated
Choi&e ? K The ethod will r!n, violating the 6J7 spe&i"i&ation(
Choi&e D K Copile)tie error "or bean ipleentation &lass
Choi&e I K Copile)tie error "or reote inter"a&e
Choi&e M K 5!n)tie error when the ethod is e+e&!ted
ans)?

M)What is the C357. naing servi&e e'!ivalent o" J0%I?
Choi&e 8 K Inter"a&e %e"inition Lang!age
Choi&e ? K C3S 0aing
Choi&e D K Lightweight %ire&tor# .&&ess Proto&ol
Choi&e I K Interoperable Inter)3rb Proto&ol
Choi&e M K Cop!ter 0aing Servi&e ans)?
Q)InitialConte+t i& < new InitialConte+t():
Test-oe th < (Test-oe)
i&(loo@!p(=test7eanFTest7ean=):
Test5eote bean. < th(&reate():
Test5eote bean7 < th(&reate():
Test5eote beanC < th(&reate():
beanC(reove():
Test5eote bean% < th(&reate():
Test5eote bean6 < th(&reate():
beanC < th(&reate():
Given the above &ode, &ontainer passivates whi&h bean instan&e "irst
i" the &ontainer liited the bean pool si/e to "o!r beans and !sed a
=least)re&entl#)!sed= algorith to passivate?
Choi&e 8 K 7ean .
Choi&e ? K 7ean 7
Choi&e D K 7ean C
Choi&e I K 7ean %
Choi&e M K 7ean 6

R)Whi&h one o" the "ollowing phenoena is 03T addressed b# read)
&onsisten&#?
.(Phanto read b(Cache) rea) &(%irt# read d(0on)repeatable
read e(@!>>y rea) ans9b,e
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8?M
WELCOME TO J2EE NOTES

V)Whi&h one o" the "ollowing ethods is generall# &alled in both
e*bLoad() and e*bStore()?
a get6J73b*e&t() b get-andle() & reove() d get6J7-oe() e
$et&rimary\ey89 ans)e
W)p!bli& void e*bCreate(int i) ;
S#ste(o!t(println(=e*bCreate(i)=): >
Given a &!rrentl# wor@ing stateless session bean, what will be the
o!t&oe !pon deplo#ing and e+e&!ting the bean i" #o! added the
above !ni'!e ethod to the ipleentation &lass o" a stateless session
bean (and ade no other &hanges)?
a Copile tie error d!ring st!bFs@eleton generation
b Copile tie error "or hoe inter"a&e
& Code will &opile witho!t errors(
d Copile tie error "or reote inter"a&e
e Copile tie error "or bean ipleentation ans)a

8O)Given the above &ode in #o!r stateless session bean b!siness
ethod ipleentation, and the transa&tion is &ontainer)anaged
with a Transa&tion .ttrib!te o" T^[S4PP35TS, whi&h one o" the
"ollowing is the "irst error generated?
a 6rror when &opiling hoe inter"a&e
b 6rror while generating st!bs and s@eletons
& 0!llPointer6+&eption d!ring deplo#ent
d 5!ntie error
e Copile)tie error "or the bean ipleentation ans)b

88)Whi&h one o" the "ollowing is the res!lt o" attepting to deplo# a
stateless session bean and e+e&!te one o" the ethod E when the bean
ipleentation &ontains the ethod E 03T de"ined in the reote
inter"a&e?
a Copile tie error "or reote inter"a&e
b Copile tie error "or bean ipleentation
& Copile tie error d!ring st!bFs@eleton generation
d Code will &opile witho!t errors(
e Copile tie error "or hoe inter"a&e ans)d
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8?Q
WELCOME TO J2EE NOTES
8?)Whi&h one o" the "ollowing &hara&teristi&s is 03T tr!e o" 5EI and
6nterprise Java 7eans?
a The# !st e+e&!te within the &on"ines o" a Java virt!al a&hine
(JJE)(
b The# seriali/e ob*e&ts "or distrib!tion(
& The# re'!ire (&lass "iles to generate st!bs and s@eletons(
d The# do not re'!ire I%L(
e The# spe&i"# the !se o" the II3P wire proto&ol "or distrib!tion(
ans)a
8D( Whi&h one o" the "ollowing is the res!lt o" attepting to deplo# a
stateless session bean and e+e&!te one o" the ethod E when the bean
ipleentation &ontains the ethod E 03T de"ined in the reote
inter"a&e?
a Copile tie error "or reote inter"a&e
b Copile tie error "or bean ipleentation
& Copile tie error d!ring st!bFs@eleton generation
d Code will &opile witho!t errors(
e Copile tie error "or hoe inter"a&e
.1- I" a !ni'!e &onstraint "or priar# @e#s is not enabled in a database,
!ltiple rows o" data with the sae priar# @e# &o!ld e+ist in a table(
6ntit# beans that represent the data "ro the table des&ribed above are
li@el# to throw whi&h e+&eption?
a 0oS!&h6ntit#6+&eption
b Ginder6+&eption
& 3b*e&t0otGo!nd6+&eption
d 5eove6+&eption
e 0!llPointer6+&eption
8M( . developer needs to deplo# an 6nterprise Java 7ean, spe&i"i&all#
an entit# bean, b!t is !ns!re i" the bean &ontainer is able to &reate and
provide a transa&tion &onte+t( Whi&h attrib!te below will allow
s!&&ess"!l deplo#ent o" the bean?
a 7eanEanaged
b 5e'!ires0ew
& Eandator#
d 5e'!ired
e S!pports
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8?R
WELCOME TO J2EE NOTES
8Q( What is the o!t&oe o" attepting to &opile and e+e&!te the
ethod above, ass!ing it is ipleented in a state"!l session bean?
a Copile)tie error "or reote inter"a&e
b 5!n)tie error when bean is &reated
& Copile)tie error "or bean ipleentation &lass
d The ethod will r!n, violating the 6J7 spe&i"i&ation(
e 5!n)tie error when the ethod is e+e&!ted
8R( Whi&h one o" the "ollowing is the res!lt o" attepting to deplo# a
stateless session bean and e+e&!te one o" the ethod E when the bean
ipleentation &ontains the ethod E 03T de"ined in the reote
inter"a&e?
a Copile tie error "or reote inter"a&e
b Copile tie error "or bean ipleentation
& Copile tie error d!ring st!bFs@eleton generation
d Code will &opile witho!t errors(
e Copile tie error "or hoe inter"a&e
8V( I" a !ni'!e &onstraint "or priar# @e#s is not enabled in a database,
!ltiple rows o" data with the sae priar# @e# &o!ld e+ist in a table(
6ntit# beans that represent the data "ro the table des&ribed above are
li@el# to throw whi&h e+&eption?
a 0oS!&h6ntit#6+&eption
b Ginder6+&eption
& 3b*e&t0otGo!nd6+&eption
d 5eove6+&eption
e 0!llPointer6+&eption
8W( There are two 6nterprise Java 7eans, . and 7( . ethod in =.=
naed =.= begins e+e&!tion, reads a val!e v "ro the database and
sets a variable =^= to val!e v, whi&h is one h!ndred( =.= adds "i"t# to
the variable ^ and !pdates the database with the new val!e o" ^( =.=
&alls =7=, whi&h is a ethod in 7( =7= begins e+e&!ting( =7= reads
an additional val!e "ro the database( 7ased on the val!e, =7=
deterines that a b!siness r!le has been violated and aborts the
transa&tion( Control is ret!rned to =.=(5e'!ireentK I" =7= aborts
the transa&tion, it is iperative that the original val!e be read "ro the
database and stored in variable ^(
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8?V
WELCOME TO J2EE NOTES
Given the s&enario above, whi&h Transa&tion .ttrib!tes will ost li@el#
eet the re'!ireents stated?
a .)5e'!ires0ew, 7)Eandator#
b .)Eandator#, 7)5e'!ires0ew
& .)5e'!ires0ew, 7)S!pports
d .)0otS!pported, 7)5e'!ires0ew
e .)5e'!ires0ew, 7)5e'!ires0ew
?O() I" an 5EI paraeter ipleents *ava(ri(5eote, how is it passed
=on)the)wire?=
Choi&e 8 K It &an never be passed(
Choi&e ? K It is passed b# val!e(
Choi&e D K It &annot be passed be&a!se it ipleents *ava(ri(5eote(
Choi&e I K (Corre&t) It &annot be passed !nless it .LS3 ipleents
*ava(io(Seriali/able(
Choi&e M K It is passed b# re"eren&e(
?8() p!bli& s#n&hroni/ed void t+Test(int i) ;
S#ste(o!t(println(=Integer isK = \ i): >
What is the o!t&oe o" attepting to &opile and e+e&!te the ethod
above, ass!ing it is ipleented in a state"!l session bean?
Choi&e 8 K 5!n)tie error when bean is &reated
Choi&e ? K The ethod will r!n, violating the 6J7 spe&i"i&ation(
Choi&e D K (Corre&t) Copile)tie error "or bean ipleentation &lass
Choi&e I K Copile)tie error "or reote inter"a&e
Choi&e M K 5!n)tie error when the ethod is e+e&!ted
??() What is the C357. naing servi&e e'!ivalent o" J0%I?
Choi&e 8 K Inter"a&e %e"inition Lang!age
Choi&e ? K (Corre&t) C3S 0aing
Choi&e D K Lightweight %ire&tor# .&&ess Proto&ol
Choi&e I K Interoperable Inter)3rb Proto&ol
Choi&e M K Cop!ter 0aing Servi&e
InitialConte+t i& < new InitialConte+t():
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8?W
WELCOME TO J2EE NOTES
Test-oe th < (Test-oe)
i&(loo@!p(=test7eanFTest7ean=):
Test5eote bean. < th(&reate():
Test5eote bean7 < th(&reate():
Test5eote beanC < th(&reate():
beanC(reove():
Test5eote bean% < th(&reate():
Test5eote bean6 < th(&reate():
beanC < th(&reate():
Given the above &ode, &ontainer passivates whi&h bean instan&e "irst i"
the &ontainer liited the bean pool si/e to "o!r beans and !sed a =least)
re&entl#)!sed= algorith to passivate?
Choi&e 8
7ean .
Choi&e ?
7ean 7
Choi&e D
7ean C
Choi&e I (Corre&t, Sin&e onl# State"!ll session bean and 6ntit# 7ean
&an be passivated, and 6ntit#bean &an not &all as th(&reate() norall#, I
ta@e it as state"!ll session bean)
7ean %
Choi&e M
7ean 6
)))))))))))))))))))))))))
Whi&h one o" the "ollowing phenoena is 03T addressedb# read)
&onsisten&#?
a Phanto read (Corre&t)
b Ca&hed read
& %irt# read
d 0on)repeatable read
e G!//# read
))))))))))))))))))))))))))
Whi&h one o" the "ollowing ethods is generall# &alled
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8DO
WELCOME TO J2EE NOTES
in both
e*bLoad() and e*bStore()?
a get6J73b*e&t()
b get-andle()
& reove()
d get6J7-oe()
e getPriar#1e#() (Corre&t)
T-.01I0G 234
C56.T6% 72 .(1.5T-I162.0
8D8