Académique Documents
Professionnel Documents
Culture Documents
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
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