J2EE_Part1 By Saurabh
-
Upload
saurabh-suman -
Category
Documents
-
view
220 -
download
0
Transcript of J2EE_Part1 By Saurabh
-
7/26/2019 J2EE_Part1 By Saurabh
1/100
J2EE 5: Web Component DevelopmentJ2EE 5: Web Component Development
FaaDoOFaaDoOEngineEngineers.comers.comFaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
2/100
ers.comers.com
Introductions
Your name
Location [home, office]
Job role
How long with[industr, !ractice area, communit]
"omething uni#ue about ourself
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
3/100
Course objectives
$!on com!leting this course, ou should be able to%
&ro'ide an o'er'iew of the J(EE
)uild a "er'let
Discuss the basic conce!ts of H**&
Discuss "er'let +&
Discuss the "er'let life ccle
Discuss E-ce!tions and Errors
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
4/100
Day 1 coverage
Ja'a "er'let *echnolog
odule /% J(EE O'er'iew
odule (% )uilding a "er'let
odule 0% H**& )asics
odule 1% "er'let +&
odule 2% "er'let Life 3cle
odule 4% "er'let Ob5ects
odule 6% E-ce!tions and Errors
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
5/100
Houseeeping
)rea7s
8ashrooms
9o !agers or cell !hones
&artici!ation
:uestions
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
6/100
!odule 1: J2EE overvie"
FaaDoOEngineers.com
FaaDoOFaaDoOEngineEngine
ers.comers.com
-
7/26/2019 J2EE_Part1 By Saurabh
7/100
+fter com!leting this module, ou should be able to e-!lain%
)asic conce!ts of J(EE
E'olution of Enter!rise +!!lication Framewor7
Enter!rise a!!lications O!en and "tandard solution
&latform ;alue
J(EE /.1 and JEE 2 +&s and technologies
3ontainers and 3om!onents J(EE De'elo!ment roles
De!loment Descri!tor
+!!lication ser'er !roducts
!odule objectives
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
8/100
#asic concepts
W$at is t$e J2EE%
+n o!en and standard based !latform for de'elo!ing, de!loing, and
managing n3*"?
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
9/100
Evolution o& Enterprise 'pplication (rame"or
"ingle tier
*wo tier
*hree tier
@&3 based
= @emote ob5ect based
= *hree tier >H*L browser and 8eb ser'er?
&ro!rietar a!!lication ser'er
"tandard a!!lication ser'er
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
10/100
Enterprise applications
*hings that ma7e u! an enter!rise a!!lication
= &resentation logic
= )usiness logic
= Data access logic >and data model?
= "stem ser'ices
*he e'olution of enter!rise a!!lication framewor7 reflects
= How fle-ibl ou want to ma7e changes
= 8here the sstem ser'ices are coming from
To SME:
Is there a need to provide a brief description of each Enterprise app element?
To SME:
Is there a need to provide a brief description of each Enterprise app element?
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
11/100
)pen and standard solution
$se Acom!onent and containerA model in which container !ro'ides sstem
ser'ices in a well
-
7/26/2019 J2EE_Part1 By Saurabh
12/100
*lat&orm value
*lat&orm value to Developers
3an use an J(EE im!lementation for de'elo!ment and de!loment
$se !roduction
-
7/26/2019 J2EE_Part1 By Saurabh
13/100
*lat&orm value +continued,
*lat&orm value to -endors
;endors wor7 together on s!ecifications and then com!ete in im!lementations
in the areas of scalabilit, !erformance, reliabilit, a'ailabilit, management and
de'elo!ment tools, and so on
Freedom to inno'ate while maintaining the !ortabilit of a!!lications
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
14/100
*lat&orm value +continued,
*lat&orm value to #usiness Customers
+!!lication !ortabilit
an im!lementation choices are !ossible based on 'arious re#uirements
&rice >free to highsingle 3&$ to clustered model?, reliabilit,!erformance, tools, and more
)est of breed of a!!lications and !latforms
Large de'elo!er !ool
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
15/100
-
7/26/2019 J2EE_Part1 By Saurabh
16/100
JEE 5 '*Is and tec$nologies
J+B
-
7/26/2019 J2EE_Part1 By Saurabh
17/100
Containers and components
3ontainers do their wor7 in'isibl
9o com!licated +&s
3ontainers im!lement J(EE
Loo7 the same to com!onents
;endors ma7ing the containers ha'e great freedom to inno'ate
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
18/100
J2EE development roles
3om!onent !ro'ider
)ean !ro'ider
+!!lication assembler
De!loer &latform !ro'ider
3ontainer !ro'ider
*ools !ro'ider
"stem administrator
To SME:
Is there a need to provide a brief description of each role in the instructor notes?
To SME:
Is there a need to provide a brief description of each role in the instructor notes?
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
19/100
J2EE development roles
0$e application component provider
= is t$e company or person "$o creates Web components enterprise beans applets orapplication clients &or use in J2EE applications
+n enter!rise bean !ro'ide%
= 8rites and com!iles the source code
= creates the de!loment descri!tor or uses annotations
= )undles the .class files and de!loment descri!tor into an EJ) J+@ file
*he a!!lication assembler
= is the !erson or !eo!le who recei'es a!!lication com!onent J+@ files from com!onent!ro'iders and assembles them into a J(EE a!!lication E+@ file.
De!loer and +dministrator
= *he a!!lication de!loer and administrator is the !erson who configures and de!los the J(EEa!!lication, administers the com!uting and networ7ing infrastructure where J(EE a!!licationsrun, and o'ersees the runtime en'ironment. Duties include such things as setting transactioncontrols and securit attributes and s!ecifing connections to databases.
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
20/100
J2EE development roles
&latform &ro'ider
= O!erating "stem ;endor 'iG>8indows8eb"!here = ), 8ebLogic = )E+, etc? *ool &ro'ider
= ntegrated De'elo!ment Editor>DE? !ro'ider >@+D =), 9et)eans< O!en "ource?
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
21/100
Deployment descriptor
i'es the container instructions on how to manage and control beha'iors of
the J(EE com!onents
*ransaction
"ecurit
&ersistence
+llows declarati'e customiGation >as o!!osed to !rogramming customiGation?
BL file
Enables !ortabilit of code
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
22/100
'pplication server
+n application serveris a software engine that deli'ers a!!lications to client
com!uters or de'ices. oreo'er, an a!!lication ser'er handles most, if not all,
of the business logic and data access of the a!!lication
Features%
= +!!lication ser'er !roducts t!icall bundle middle"areto enable a!!lications to
intercommunicate with de!endent a!!lications, li7e 8eb ser'ers, database
management sstems and chart !rograms.
= &ortals are a common a!!lication ser'er mechanism b which a single !oint of entr
is !ro'ided to multi!le de'ices.
= EJ) are hosted in an +!!lication "er'er. "ome common e-am!le of +!!lication"er'er are 8eb"!here +!!lication "er'er >)?, J)oss >@edHat?, *omcat >+!ache?,
"un+!!"er'er> "un icrosstems?, 8ebLogic "er'er >)E+?.
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
23/100
'ny uestions%
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
24/100
!odule 2: #uilding a 3ervlet
FaaDoOEngineers.com
FaaDoOFaaDoOEngineEngine
ers comers com
-
7/26/2019 J2EE_Part1 By Saurabh
25/100
+fter com!leting this module, ou should be able to%
Define a 8eb com!onent
Define a "er'let
Enumerate the benefits using a "er'let Enumerate the ad'antages of using a "er'let o'er
3
Enumerate the features of a "er'let
E-!lain how a "er'let is built
!odule objectives
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
26/100
W$at is a Web Component%
+ web com!onent is a software entit that runs on a web ser'er, !ro'iding it with
the ca!abilities needed for dnamicall handling client re#uests and generating
web !resentation content.
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
27/100
W$at is a 3ervlet%
"er'er side Ja'a !rogram that e-tends the functionalit of a 8eb "er'er
$sed to dnamicall generate H*L documents
3om!arable to%
= 3= 9etsca!e 9"+&
= icrosoft "+&
= +!ache odules
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
28/100
3ervlet bene&its
8ritten in !ure Ja'a
= &latform inde!endent
= 3an ta7e ad'antage of JD)3, EJ), J", Ja'aail, Ja'aDL, @, and more
"er'er inde!endent
"calabilit
= DoesnIt start new !rocess for each re#uest
= 3an run in same ser'er !rocess as H**& ser'er
= ulti
-
7/26/2019 J2EE_Part1 By Saurabh
29/100
3ervlet advantage over C4I
+ "er'let does not run in a se!erate !rocess
+ "er'let stas in memor between re#uests
*here is onl a single instance which answers all re#uests concurrentl
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
30/100
#uilding a 3ervlet using t$e 3ervlet '*I
E-tend Htt!"er'let
3ode "er'letIs life ccle ethods
GenericServletGenericServlet
ServletServlet
HttpServletHttpServlet
LogonServletLogonServlet
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
31/100
sage o& 3ervlet
&rocessing or storing data submitted b an H*L form
&ro'iding dnamic content
anaging state information
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
32/100
(eatures o& 3ervlet
3ecurity
nherits the securit feature !ro'ided b the container
3ession !anagement
aintains the identit and state of an end user across multi!le re#uests
Instance *ersistence
Enhances the !erformance of the ser'er b !re'enting fre#uent dis7 access
*lat&orm and server independent
+'ailable and running on all ma5or web ser'ers and a!!lication ser'ers
FaaDoOEngineers.com
http://../PRP_Material/j2sdkee-beta/doc/api/javax/servlet/GenericServlet.htmlhttp://../PRP_Material/j2sdkee-beta/doc/api/javax/servlet/http/HttpServlet.htmlhttp://../PRP_Material/j2sdkee-beta/doc/api/javax/servlet/http/HttpServlet.htmlhttp://../PRP_Material/j2sdkee-beta/doc/api/javax/servlet/Servlet.htmlhttp://../PRP_Material/j2sdkee-beta/doc/api/javax/servlet/Servlet.htmlhttp://../PRP_Material/j2sdkee-beta/doc/api/javax/servlet/GenericServlet.htmlhttp://../PRP_Material/j2sdkee-beta/doc/api/javax/servlet/GenericServlet.html -
7/26/2019 J2EE_Part1 By Saurabh
33/100
#uilding a 3ervlet
J2EE 'pplication 3erver
Web Container
Web Client
+#ro"ser,
Web Client
+#ro"ser, 3ervlet
@e#uest
@es!onse
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
34/100
J2EE 'pplication 3erver
Web Container
#uilding a 3ervlet +continued,
Loads *he "er'let classLoads *he "er'let class
nstantiates the "er'letnstantiates the "er'let
nitialiGes the "er'let nstancenitialiGes the "er'let nstance
&asses re#uest to the "er'letnstance
&asses re#uest to the "er'letnstance
"ends @es!onse"ends @es!onse
First 3lient
@e#uest
First 3lient
@e#uest
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
35/100
'ny uestions%
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
36/100
!odule 6: H00* #asics
FaaDoOEngineers.com
FaaDoOFaaDoOEngineEngine
ers comers com
-
7/26/2019 J2EE_Part1 By Saurabh
37/100
+fter com!leting this module, ou should be able to%
Define or describe H**&
E-!lain a H**& @e#uest
E-!lain a H**& @es!onse
!odule objectives
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
38/100
Introduction
*he H!erte-t *ransfer &rotocol >H**&? is an a!!lication
-
7/26/2019 J2EE_Part1 By Saurabh
39/100
H00* 8euest
+n H**& re#uest has three !arts%
= + re#uest line
= One or more re#uest headers
= + message
+ re#uest line loo7s li7e%
GET /WelcomeProect/!ome"html !TTP/#"#
1sttoken is the name of the method, in this case it is GET
2ndtoken is the URL, that gives information about the location of the
resource to be gotten.
The last token is the version of HTT to be used.
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
40/100
H00* 8esponse
+n H**& res!onse similarl has three !arts%
= + res!onse line
= One or more res!onse headers
= + message
+ res!onse line loo7s li7e%
!TTP/#"# $%% &'
1sttoken is the HTT version.
2ndtoken is one of the man!predefinedstatus codes.
"rd token is an English descri#tion of the status code.
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
41/100
'ny uestions%
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
42/100
!odule /: 3ervlet '*I
FaaDoOEngineers.com
FaaDoOFaaDoOEngineEngine
ers comers com
-
7/26/2019 J2EE_Part1 By Saurabh
43/100
+fter com!leting this module, ou should be able to%
E-!lain how a "er'let is created
E-!lain how a "er'let is !ac7aged
Discuss the $eb.%ml
!odule objectives
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
44/100
3ervlet creation
"te!s to create standard !ac7aging structure of a web a!!lication, create the
following directories%
+ root director
t contains the static resources, such as, H0!9, J3*and imagefiles.
+ 8E)
-
7/26/2019 J2EE_Part1 By Saurabh
45/100
3ervlet pacaging
J(EE defines a standard !ac7aging structure to !ac7age a ser'let into a J(EE
a!!lication to ma7e it !ortable across different a!!lication ser'ers.
+ standard !ac7aging structure will allow a!!lication ser'ers to easil locate
and load a!!lication files from the standard director structure.
n most of the cases, the standard !ac7aging structure is created b thentegrated De'elo!ment En'ironment in use li7e, R&', (!Ecli#se, )*&',
*un#*erver.
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
46/100
Directory structure o& a Web 'pplication
@oot Director@oot Director
8E)
-
7/26/2019 J2EE_Part1 By Saurabh
47/100
Deployment Descriptor: "eb.ml
0$e "eb.ml consists o&:
BL &rolog information
8eb +!!lication dis!la name
"er'let information such as%= "er'let Dis!la 9ame
= 9ame of the "er'let, when it will be accessed
= "er'let 3lass 9ame
= $@L a!!ings
= 8elcome files
= E t!es
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
48/100
Content o& "eb.ml:
+%ml version-1./0 encoding-UT30 4+$eba## version-2.50 %mlns-htt#6778ava.sun.com7%ml7ns782ee0%mlns6%si-htt#677$$$.$".org72//179(L*chema:nstance%si6schemaLocation-htt#6778ava.sun.com7%ml7ns782ee htt#6778ava.sun.com7%ml7ns782ee7$eba##;2;5.%sd04
+dis#la!name4)elcome#lication+7dis#la!name4+servlet4
+dis#la!name4)elcome*ervlet+7dis#la!name4+servletname4)elcome*ervlet+7servletname4+servletclass4ibm.sam#le.)elcome*ervlet+7servletclass4+7servlet4+$elcomefilelist4+$elcomefile4)elcome*ervlet+7$elcomefile4+7$elcomefilelist4
+servletma##ing4+servletname4)elcome*ervlet+7servletname4+url#attern47)elcome*ervlet+7url#attern4+7servletma##ing4+7$eba##4
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
49/100
1. Creating a H0!9 *age: serIn&o.$tml
+html4
+title4:nformation age+7title4
+bod!4
+h14User :nformation age+7h14
+hr color-e-?4
+form action-*http://localhost:+%,%/Welcome-ser.pplication/Welcome-ser4+#re4
Enter @our irst Aame 6 +in#ut t!#e-te%t0 name-fname04
Enter @our Last Aame 6 +in#ut t!#e-te%t0 name-lname04
+in#ut t!#e-submit0 value-*ubmit04 +in#ut t!#e-reset0 value-Blear04
+7form4
+7#re4+7bod!4
+7html4
8eb3onte-t @oot8eb3onte-t @oot
"er'let +lias 9ame"er'let +lias 9ame
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
50/100
#ackage ibm.sam#leC
im#ort 8ava.io.DC
im#ort 8ava%.servlet.DC
im#ort 8ava%.servlet.htt#.DC
#ublic class )elcomeUser e%tends Htt#*ervlet
#ublic void doGetFHtt#*ervletReuest reuest, Htt#*ervletRes#onse res#onse
thro$s :IE9ce#tion, *ervletE%ce#tion
*tring firstname - request"0etParameter1*fname23
res#onse.setBontentT!#eFte%t7html0C
rint)riter out - res#onse.get)riterFC
out.#rintlnF+h14)elcome J +font color-
-
7/26/2019 J2EE_Part1 By Saurabh
51/100
6. 3ave and deploy t$e 3ervlet
3lic7 on "a'e button in the @+D DE, if it dis!la some red cross mars in
your code t$encorrect it and then sa'e to generate a .class file.
De!lo the web module containing Welcomeser"er'let. @ight clic7 on the
Welcomeser.javaand select @un
-
7/26/2019 J2EE_Part1 By Saurabh
52/100
/. 'ccess t$e H0!9 page in t$e Web #ro"ser
htt!%localhost%KCC8elcome$ser+!!lication$sernfo.html
O!en the web browser *!e the $@L here &ress EnterM 7e
O!en the web browser *!e the $@L here &ress EnterM 7e
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
53/100
/. 'ccess t$e H0!9 page in t$e Web #ro"ser +continued,
htt!%localhost%KCC8elcome$ser+!!lication8elcome$serNfname)lnamendia
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
54/100
'ny uestions%
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
55/100
!odule 5: 3ervlet li&e cycle
FaaDoOEngineers.com
FaaDoOFaaDoOEngineEngine
ers comers.com
-
7/26/2019 J2EE_Part1 By Saurabh
56/100
+fter com!leting this module, ou should be able to%
Discuss the lifeccle of a "er'let
Discuss the initFmethod
Discuss the serviceF method
Discuss the destro!Fmethod
!odule objectives
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
57/100
9i&ecycle o& a 3ervlet
Servlet
init( )doGet( )
HTTP
Serv
er
HTTP
Serv
er
Servlets Container
destroy( )
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
58/100
9i&ecycle o& a 3ervlet +continued,
Servlet
init( )doGet( )
Servlets Container
destroy( )
Thread
Thread
Thread
HTTP
Serv
er
HTTP
Serv
er
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
59/100
0$e init12met$od
#ublic void initF*ervletBonfig thro$s *ervletE%ce#tion
*his method is for the initialiGation of the ser'let.
One can establish database connection in this method which could be sharedb e'er client.
*his method is called onl once during the life of a ser'let.
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
60/100
0$e service12met$od
#ublic void Service(ServletRequest, ServletResponse)thro$s
*ervletE%ce#tion, :IE%ce#tion
*his method is called once !er client.
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
61/100
0$e destro412met$od
#ublic void destroy()
3alled to destro the ser'let and release the resources
$suall called b the "er'let 3ontainer
3alled once during the life of a ser'let
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
62/100
9i&ecycle o& a 3ervlet +continued,
;ote:
De!ending on how ou register our
ser'let with the web ser'er, it can be
created either when a client first
references a $@L that corres!onds tothe ser'let, or when the web ser'er is
first started.
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
63/100
'ny uestions%
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
64/100
!odule
-
7/26/2019 J2EE_Part1 By Saurabh
65/100
+fter com!leting this module, ou should be able to discuss%
"er'let 3onte-t
"co!e of "er'let ob5ects
"er'let nterface methods "ession anagement
$sing the H**& "ession nterface
!odule objectives
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
66/100
3ervlet object
Enables sharing information among collaborating web com!onents 'ia
attributes maintained in sco!e ob5ects.
= +ttributes are name ob5ect !airs.
+ttributes maintained in the "co!e ob5ects are accessed with
= get+ttribute>? and set+ttribute>? method
"co!e ob5ects )elongs to
8eb3onte-t >"er'let3onte-t? 8ava%.servlet.*ervletBonte%t
"ession 8ava%.servlet.htt#.Htt#*ession
@e#uest 8ava%.servlet.*ervletReuest
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
67/100
3ervlet Contet
t is shared b all ser'lets and J"& !ages within a "eb applicationM.
*here is one "er'let3onte-t ob5ect !er web a!!licationM !er J;.
"et and get conte-ta!!lication
-
7/26/2019 J2EE_Part1 By Saurabh
68/100
3cope o& 3ervlet objects
'pplication
"er'let3onte-t
3lient /
3lient (
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
69/100
3ervlet Inter&ace met$ods
3ervlet Inter&ace !et$ods
!et$od ;ame Description
!ublic "er'let3onfigget"er'let3onfig>?
@eturns a "er'let3onfig ob5ect thatcontains configuration information, suchas initialiGation !arameters, to initialiGe aser'let.
!ublic "tring get"er'letnfo>?t returns a string that containsinformation about the ser'let, such asauthor, 'ersion, and co!right.
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
70/100
3ervlet Contet met$ods +continued,
3ervlet Contet !et$ods
!et$od ;ame Description
!ublic 'oid set+ttribute>"tring s,Ob5ect ob5?
)inds the ob5ect with a name and storesthe name'alue !air as an attribute of the"er'let3onte-t ob5ect.
!ublic Ob5ect get+ttribute>"tringattribute9ame?
@eturns the ob5ect stored in the"er'let3onte-t ob5ect with name !assedas a !arameter.
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
71/100
3cope o& 3ervlet objects +continued,
Ho" to get t$e 3ervletcontet )bject:
*ervletBonte%t conte%t - 0etServlet5onfi012"0etServlet5onte)t123
IR
*ervletBonte%t conte%tC#ublic void initF*ervletBonfig cfg
conte%t - cf0"0etServlet5onte)t123
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
72/100
3ession !anagement
3o $o" toimplement it=
3o $o" toimplement it=
echanism to maintain client state across a series of re#uests from a same user
or originating from the browser o'er some !eriod of time.
E-am!le% Online "ho!!ing 3art.
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
73/100
Implementation
Login"creen
Dis!la*rouserList"er'let
3hoice&rocessing"er'let
Final"er'let
+cce!t $ser9ame and &assword and on submissioncall Display0rouser9ist3ervlet .
*his ser'let dis!las the list of trouser a'ailable in thestoc7 and on submission call a "er'let 7nown asC$oice*rocessing3ervletfor further !rocessing.
*his ser'let 7ee! trac7 of the user selection of thetrouser and forward the selected trouser list to the(inal3ervletfor )illing the user.
*his ser'let calculate the total bill for the user based onthe *rouser selected >*rouser selection informationretrie'ed from C$oice*rocessing3ervlet?
>5 FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
74/100
9ogin *age ? 9ogin.$tml
PH*L
P**LEHa!!;isit Online "ho!!ing &ortalP**LE
P)ODY
PFO@ +3*O9 @$ttp:AAlocal$ost:BA3ession3ervlet*rojectADisplay0rouser9ist3ervlet@align3E9*E@M
$sername% P9&$* *Y&E *EB* 9+E AuserA align3E9*E@P)@&assword% P9&$* *Y&E &+""8O@D 9+E A!asswordA align3E9*E@P)@
P9&$* *Y&E "$)* ;+L$E ALoginA align3E9*E@
PFO@
P)ODY
PH*L
>< FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
75/100
3ervlet 71: Display0rouser9ist3ervlet.java
!ac7age ibm.sam!le.loginQ
im!ort 5a'a.io.RQ
im!ort 5a'a-.ser'let.RQ
im!ort 5a'a-.ser'let.htt!.RQ
!ublic class Display0rouser9ist3ervlete-tends Http3ervlet
S
!ublic 'oid do4et>Htt!"er'let@e#uest re#, Htt!"er'let@es!onse res? throws "er'letE-ce!tion, OE-ce!tion
S
"tring username re#.get&arameter>AuserA?Q
"tring !assword re#.get&arameter>A!asswordA?Q
int counter CQ
&rint8riter out res.get8riter>?Q
out.!rintln> username T AU 8elcome to Ha!!;isit Online "ho!!ing.P)@A?Q
>> FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
76/100
counter CQ
R 3reate a session for the user and store the 'alue of username. R
Htt!"ession session re#.get"ession>true?Q
session.set+ttribute>AuserA, username?Q
V
else
S
out.!rintln>A"orrU n'alid username and !asswordA?Q
counter /Q
V
if >counter C?
S
R Dis!la the contents to the user. R
out.!rintln>APH*LP)ODYA?Q
out.!rintln>APH@A?Q
out.!rintln>APFO@ +3*O9 $ttp:AAlocal$ost:BA3ession3ervlet*rojectAC$oice*rocessing3ervlet !E0H)D*)30A?Q
3ervlet 71: Display0rouser9ist3ervlet.java
> FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
77/100
out.!rintln>AP*+)LE 8D*H2CCA?Qout.!rintln>AP*@P*H*E 9OP*H P*H)rands of *rousers P*H P*H)$YP*H P*@ A?Q
out.!rintln>AP*@P*D / P*DP*D &eterEngland P*D P*D P9&$* 9+E c/ *Y&E 3HE3W)OB ;+L$E &eterEngland P*D P*@ A?Q
out.!rintln>AP*@P*D ( P*DP*D oustache P*D P*D P9&$* 9+E c( *Y&E 3HE3W)OB ;+L$E oustache P*D P*@ A?Q
out.!rintln>AP*@P*D 0 P*DP*D +llen "oll P*D P*D P9&$* 9+E c0 *Y&E 3HE3W)OB;+L$E +llen "ollP*D P*@ A?Q
out.!rintln>AP*@P*D 1 P*DP*D Xodiac P*D P*D P9&$* 9+E c1 *Y&E 3HE3W)OB;+L$E XodiacP*D P*@ A?Q
out.!rintln>AP*+)LEA?Q
out.!rintln>AP9&$* *Y&E "$)* ;+L$E "$)*A?Qout.!rintln>APFO@A?Q
out.!rintln>AP)ODYPH*LA?Q out.close>?Q
V
V
V
3ervlet 71: Display0rouser9ist3ervlet.java
>B FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
78/100
im!ort 5a'a.io.RQ
im!ort 5a'a-.ser'let.RQ
im!ort 5a'a-.ser'let.htt!.RQ
im!ort 5a'a.util.RQ
!ublic class C$oice*rocessing3ervlet e-tends Http3ervlet
S
!ublic 'oid do*ost+Htt!"er'let@e#uest re#uest, Htt!"er'let@es!onse res!onse? throws "er'letE-ce!tion, OE-ce!tion
S
"tring user null Q
"tring item nullQ
int i /Q
Enumeration enum nullQ
&rint8riter out res!onse.get8riter>?Q
Htt!"ession session re#uest.get"ession>true?Q
3ervlet 7 2: C$oice*rocessing3ervlet.java
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
79/100
user >"tring?session.get+ttribute>AuserA?Q
R @etrie'e the name of all the !arameters !assed b the end user. R
enum reuest.get*arameter;ames+,F
while >enum.hasoreElements>??
S
"tring sname >"tring?enum.ne-tElement>?Q
R "tore the names in the session ob5ect. R
session.set'ttribute +@c@Gi sname,F
iTTQ
V
R "tore the counter 'alue in the session ob5ect. R
session.set'ttribute+@counter@ i G@@,F
@e#uestDis!atcher dis! re#uest.get@e#uestDis!atcher>A(inal3ervletA?Q
disp.&or"ard+reuest response,F
V
V
3ervlet 7 2: C$oice*rocessing3ervlet.java
1 FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
80/100
im!ort 5a'a.io.RQ
im!ort 5a'a-.ser'let.RQ
im!ort 5a'a-.ser'let.htt!.RQ
!ublic class (inal3ervlete-tends Http3ervlet
S
!ublic 'oid do*ost>Htt!"er'let@e#uest re#uest, Htt!"er'let@es!onse res!onse? throws "er'letE-ce!tion, OE-ce!tion
S
"tring trcode/ nullQ
"tring trcode( nullQ
"tring trcode0 nullQ
"tring trcode1 nullQ
int !rice/ CQ
int !rice( CQ
int !rice0 CQ
int !rice1 CQ
int total&rice CQ
"tring username AAQ
3ervlet 7 6: (inal3ervlet.java
2 FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
81/100
&rint8riter out res!onse.get8riter>?Q
Htt!"ession session re#uest.get"ession>true?Q
R @etrie'e the 'alue bound to AuserA. R
username >"tring?session.get+ttribute>AuserA?Q
R @etrie'e the 'alue bound to AcounterA.R
"tring counter >"tring?session.get+ttribute>AcounterA?Q
int count nteger.!arsent>counter?Q
for>int i/Q iPcountAcATi?Q
3ervlet 7 6: (inal3ervlet.java
6 FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
82/100
R 3alculate the total !rice of selected items.R
if >>trname[iAc/A??
S
!rice/ 112Q
V
if >>trname[iAc(A??
S
!rice( 42CQ
V
if >>trname[iAc0A??
S
!rice0 242Q
Vif >>trname[iAc1A??
S
!rice1 /(62Q
V
V
3ervlet 7 6: (inal3ervlet.java
/ FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
83/100
R 3alculate the total bill. R
total&rice !rice/ T !rice( T !rice0 T !rice1Q
R Dis!la the total bill that the user has to !a. R
out.!rintln>username T , our bill is @s A T total@ate?Q
out.!rintln>AP)ODYPH*LA?Q
out.close>?Q
V
V
3ervlet 7 6: (inal3ervlet.java
5 FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
84/100
Http3ession
Http3ession !et$ods
!et$od ;ame Description
!ublic 5a'a.lang.Ob5ect
get'ttribute>5a'a.lang."tring name?
!ublic 'oid set'ttribute>5a'a.lang."tring name,
5a'a.lang.Ob5ect 'alue?
!ublic 'oidremove'ttribute>5a'a.lang."tring name?
!ublic 'oid invalidate>?
!ublic 'oidset!aInactiveInterval>int inter'al?
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
85/100
'ny uestions%
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
86/100
!odule >: Eceptions and errors
FaaDoOEngineers.com
FaaDoOFaaDoOEngineEngineers.comers.com
-
7/26/2019 J2EE_Part1 By Saurabh
87/100
+fter com!leting this module, ou should be able to e-!lain%
"er'letE-ce!tion
$na'ailableE-ce!tion
How to send error message to 3lient
Logging errors in "er'lets
!odule objectives
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
88/100
Eceptions and Errors in 3ervlets
+ 3ervletEceptionindicates
that a general e-ce!tion has
occurred.
E-ce!tion
>from 5a'a.lang?
3ervletEception
+&rom java.servlet,
$na'ailableE-ce!tion>from 5a'a-.ser'let?
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
89/100
Eceptions and Errors in 3ervlets +continued,
+n navailableEception is
thrown when a !articular ser'let
is tem!oraril or !ermanentl
una'ailable cannot handle
re#uests, such as if a ser'let is
incorrectl configured, or aresource it de!ends on is
una'ailable.
E-ce!tion
>from 5a'a.lang?
3ervletEception
+&rom java.servlet,
$na'ailableE-ce!tion>from 5a'a-.ser'let?
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
90/100
3end error message to Client
#ublic void sendErrorFint status;code
O@
#ublic void sendErrorFint status;code, *tring message
)oth the methods belong tojava.servlet.$ttp.Http3ervlet8esponse
Inter&ace
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
91/100
9ogging errors
"er'lets ha'e the abilit to write their actions and their errors to a log file using
the log>? method%
*he log>? method aids debugging b !ro'iding a wa to trac7 a ser'letIs
actions.
t also offers a wa to sa'e a com!lete descri!tion of an errors encounteredb the ser'let.
*he descri!tion can be the same as the one gi'en to the client, or it can be
more e-hausti'e and detailed.
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
92/100
9ogging errors +continued,
*he eneric"er'let class !ro'ides a log>? method%
#ublic void Generic*ervlet.logF*tring msg6
logFmsgC
get*ervletBonte%tF.logFe, msgC
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
93/100
'ny uestions%
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
94/100
3ession eercise
FaaDoOEngineers.com
FaaDoOFaaDoOEngineEngineers.comers.com
-
7/26/2019 J2EE_Part1 By Saurabh
95/100
Eercise
8rite a "er'let which will read the database details 'iG. dri'er details and 5dbc
url from the "er'let3onfig and establish a database connection.
8rite a ser'let that will search a Em!loee based on his or her Em!loee D. f
an em!loee e-ists with that D, it dis!las the details of that em!loee.
Otherwise shows no em!loee record e-ist with that name.
3onsider the following em!>table?
Em#
= Em#no FM
= Ename
= *al
= Hiredate
= de#tno FM
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
96/100
3ession summary
FaaDoOEngineers.com
FaaDoOFaaDoOEngineEngineers.comers.com
-
7/26/2019 J2EE_Part1 By Saurabh
97/100
3ummary
+lthough some client re#uests might onl re#uire !rebuilt !ages, there are
certain situations in which web !ages need to be built dnamicall.
"er'let technolog !ro'ides ser'er
-
7/26/2019 J2EE_Part1 By Saurabh
98/100
3ummary +continued,
*he ser'let +& consists of two !ac7ages =java.servletand
java.servlet.$ttp= which !ro'ide all the interfaces and classes needed for
coding ser'lets.
+ll the ser'lets must im!lement "er'let interface either directl or indirectl.
*he eneric "er'let class is used to code generic, !rotocol
-
7/26/2019 J2EE_Part1 By Saurabh
99/100
3ummary +continued,
*he ser'let life ccle is controlled b the web container using a number of
methods, including init service do and destroy.
*here are two e-ce!tion classes associated with Ja'a ser'lets.
3ervletEception indicates that a general e-ce!tion has occurred, and
navailableEceptionindicates that the ser'let is not a'ailable to handle
re#uests.
FaaDoOEngineers.com
-
7/26/2019 J2EE_Part1 By Saurabh
100/100
8e&erences
htt!%www.wd'l.comnternet&rotocolsH**&
htt!%roseindia.net
htt!%www.min#.se!roducts!uretestdochtmlcommonwebtestwebtest0.html