You are on page 1of 48

Web Application Architecture (based J2EE 1.

4 Tutorial)

Disclaimer & Ackno led!ments

Even though Sang Shin is a full-time employee of Sun Microsystems, the contents here are created as his own personal endeavor and thus does not reflect any official stance of Sun Microsystems. Sun Microsystems is not responsible for any inaccuracies in the contents. Acknowledgments

The slides of this presentation are made from !eb Application" section of #$EE %.& tutorial written by Sun Microsystems

A!enda

!eb application, components and !eb container Technologies used in !eb application !eb application development and deployment steps !eb Application Archive '(.!A) file*

(.!A) directory structure !E+-,-. subdirectory !eb application deployment descriptor 'web.0ml file*

/onfiguring !eb application

Web Application & Web "omponents & Web "ontainer


4

Web "omponents & "ontainer


Applet "ontainer Applet J2%E
,TT*( ,TT*%

Web "ontainer J%* %er-let


'&$($$)*

EJ+ "ontainer '&$ EJ+


'&$($$)*

JD+"

App "lient "ontainer App ,TT*( "lient ,TT*%

JA.

JA.

J2%E

'&$
'&$($$)*

J2%E

JD+"

J#D$

J&%

J2%E

Database 5

JD+"

J#D$

J&%

JTA

J#D$

J&%

JTA

Ja-a&ail

Ja-a&ail

Web "omponents & "ontainer

!eb components are in the form of either Servlet or #S1 'along with #ava+ean2s and custom tags* !eb components run in a !eb container

Tomcat and )esin are popular web containers All #$EE compliant app servers 'Sun #ava System App Server* provide web containers

!eb container provides system services to !eb components

)e3uest dispatching, security, and life cycle management


6

Web Application & "omponents

!eb Application is a deployable package


!eb components 'Servlets and #S12s* Static resource files such as images 4elper classes 5ibraries 6eployment descriptor 'web.0ml file* A hierarchy of directories and files 'unpacked form* or (.!A) file reflecting the same hierarchy 'packed form*
7

!eb Application can be represented as


Technolo!ies /sed $n Web Application

Web 'e0uest ,andlin!

Ja-a Web Application Technolo!ies

10

Web Application De-elopment and Deplo1ment %teps (usin! hello2 e2ample under J2EE 1.4 tutorial)
11

Web Application De-elopment and Deplo1ment %teps


%.!rite 'and compile* the !eb component code 'Servlet or #S1* and helper classes referenced by the web component code $./reate any static resources 'for e0ample, images or 4TM5 pages* 7./reate deployment descriptor 'web.0ml* &.+uild the !eb application '(.war file or deployment-ready directory* 8.6eploy the web application into a !eb container

!eb clients are now ready to access them via 9)5

12

1. Write and compile the Web component code


/reate development tree structure !rite either servlet code or #S1 pages along with related helper code /reate build.0ml for Ant-based build 'and other application development life-cycle management* process ,6E 'i.e. -et+eans* handles all these chores
13

De-elopment Tree %tructure

:eep !eb application source separate from compiled files

facilitate iterative development

)oot directory 'e0ample from hello$ sample code from #$EE %.& tutorial*

build.0ml; Ant build file src; #ava source of servlets and #ava+eans components web; #S1 pages and 4TM5 pages, images
14

E2ample3 hello2 Tree %tructure (be4ore 5ant build6 command)

4ello$ src<servlets =reetingServlet.>ava )esponseServlet.>ava web !E+-,-. web.0ml duke.waving.gif build.0ml


15

2. "reate an1 static resources

4TM5 pages

/ustom pages 5ogin pages Error pages

,mage files that are used by 4TM5 pages or #S1 pages

E0ample; duke.waving.gif

16

7. "reate deplo1ment descriptor ( eb.2ml)

6eployment descriptor contains deployment time runtime instructions to the !eb container

9)5 that the client uses to access the web component

Every web application has to have it

17

4. +uild the Web application

Either (.!A) file or unpacked form of (.!A) file +uild process is made of

create build directory 'if it is not present* and its subdirectories compile #ava code into build<!E+-,-.<classes directory

#ava classes reside under .<!E+-,-.<classes directory

copy web.0ml file into build<!E+-,-. directory copy image files into build directory

18

E2ample3 hello2 Tree %tructure (a4ter 5asant build6 command)

4ello% src web build.0ml build !E+-,-. classes =reetingServlet.class )esponseServlet.class web.0ml duke.waving.gif

19

8. Deplo1 Web application

6eploy the application over deployment platform such as Sun #ava System App Server or Tomcat 7 ways to deploy to Sun #ava System App server

asadmin deploy --port &?&? --host localhost @ passwordfile Ac;B>$eetutorial%&Be0amplesBcommonBadminpassword.t0tA --user admin hello$.war 'asant deploy-war* App server admin console -et+eans
20

9. *er4orm "lient Access to Web Application

.rom a browser, go to 9)5 of the !eb application

21

http3((localhost3:;:;(hello1(!reetin!

22

'unnin! Web Application

23

Web Application Archi-e (<.WA')


24

Web Application

!eb application can be deployed in two different forms


a (.war file or an unpacked directory laid out in the same format as a (.war file 'build directory*

9se (.war file when you have to deploy on a remote machine

asant deploy-war command

25

What is <.WA' 4ile=

)eady to deploy2able package over web container Similar to (.>ar file /ontains things to be deployed

!eb components 'servlets or #S12s* Server-side utility classes Static !eb presentation content '4TM5, image, etc* /lient-side classes 'applets and utility classes*
26

)eflects contents in build directory

Document 'oot & "onte2t

6ocument )oot of the !eb application


Top-level directory of !A) /ontains #S1 pages, client-side classes and archives, and static !eb resources are stored Also contains !E+-,-. directory

A conte0t is a name that gets mapped to the document root of a !eb application

<hello% is conte0t for hello% e0ample 6istinguishes a !eb application in a single !eb container 4as to be specified as part of client 9)5 27

Director1 %tructure o4 <.WA' 4ile

28

Director1 %tructure o4 <.WA' 4ile

29

,o

to "reate <.WA' 4ile=


9se ,6E '-et+eans* 9se ant tool after putting proper build instruction in build.0ml file asant create-war" 'under #$EE %.& tutorial* 9se >ar cvf CfilenameD.war ." command under build directory

7 different ways

30

E2ample3 "reatin! hello2. ar -ia 5asant create> ar6 command


/;B>$eetutorial%&Be0amplesBwebBhello$Dasant create-war +uildfile; build.0ml ... create-war; EechoF /reating the !A).... EdeleteF 6eleting; /;B>$eetutorial%&Be0amplesBwebBhello$BassembleBwarBhello$.war EdeleteF 6eleting directory /;B>$eetutorial%&Be0amplesBwebBhello$BassembleBwarB!E+-,-. EcopyF /opying % file to /;B>$eetutorial%&Be0amplesBwebBhello$BassembleBwarB!E+-,-. EcopyF /opying $ files to /;B>$eetutorial%&Be0amplesBwebBhello$BassembleBwarB!E+-,-.Bclasses EwarF +uilding war; /;B>$eetutorial%&Be0amplesBwebBhello$BassembleBwarBhello$.war EcopyF /opying % file to /;B>$eetutorial%&Be0amplesBwebBhello$ 31

E2ample3 "reatin! hello2. ar -ia ?ar command


/;B>$eetutorial%&Be0amplesBwebBhello$BbuildD>ar cvf hello$.war . added manifest adding; duke.waving.gif'in G %7H8* 'outG %$I8*'deflated HJ* adding; servlets<'in G H* 'outG H*'stored HJ* adding; servlets<=reetingServlet.class'in G %K?H* 'outG ??L*'deflated &LJ* adding; servlets<)esponseServlet.class'in G %HIH* 'outG 8L$*'deflated &LJ* /;B>$eetutorial%&Be0amplesBwebBhello$BbuildD>ar 0vf hello$.war created; META-,-.< e0tracted; META-,-.<MA-,.EST.M. e0tracted; duke.waving.gif created; servlets< e0tracted; servlets<=reetingServlet.class e0tracted; servlets<)esponseServlet.class
32

WE+>$#. Director1

Subdirectory of 6ocument root /ontains


web.0ml ; !eb application deployment descriptor #S1 tag library descriptor files /lasses ; A directory that contains server-side classes; servlets, utility classes, and #ava+eans components lib ; A directory that contains #A) archives of libraries 'tag libraries and any utility libraries called by server-side classes*
33

,TT* re0uest /'@ & Web component /'@ (alias) & "onte2t

)e3uest 9)5; 9ser specified access point of a web resource


http;<<EhostF;EportF<Ere3uest pathFME3uery stringF Ere3uest pathF is made of conte0t and web component2s 9)5 http;<<localhost;?H?H<hello%<greetingMusernameGMonica

/onte0t; -ame of the root document of a web application @ ,dentifies a particular application on that server

<hello% is conte0t

34

"on4i!urin! Web Application -ia eb.2ml


35

"on4i!urin! Web Application

/onfiguration information is specified in web.0ml '!eb Applications 6eployment 6escriptor*

36

Web Applications Deplo1ment Descriptor ( eb.2ml)


1rolog Alias 1aths /onte0t and ,nitialiNation 1arameters Event 5isteners .ilter Mappings Error Mappings )eference to Environment Entries, )esource environment entries, or )esources
37

Web Applications Deplo1ment Descriptor ( eb.2ml)


/ase sensitive Order sensitive 'in the following order*


icon, display-name, description, distributable conte0t-param, filter, filter-mapping listener, servet, servlet-mapping, session-config mime-mapping, welcome-file-list error-page, taglib, resource-env-ref, resource-ref security-constraint, login-config, security-role env-entry, e>b-ref, e>b-local-ref
38

*rolo! (o4

eb.2ml)

Every PM5 document needs a prolog

CM0ml versionGA%.HA encodingGA,SO-??8I-%AMD CQ6O/TR1E web-app 19+5,/ A-<<Sun Microsystems, ,nc.<<6T6 !eb Application $.7<<E-A Ahttp;<<>ava.sun.com<dtd<webappS$S7.dtdAD

39

Alias *aths (o4

eb.2ml)

!hen a re3uest is received by Servlet container, it must determine which !eb component in a which web application should handle the re3uest. ,t does so by mapping the 9)5 path contained in the re3uest to a !eb component A 9)5 path contains the conte0t root and alias path

http;<<ChostD;?H?H<conte0tSroot<aliasSpath <alias-string 'for servlet* or <(.>sp 'for #S1*


40

Alias 1ath can be in the form of either


Alias *aths (o4

eb.2ml)

Aser-letB Aser-let>nameB!reetin!A(ser-let>nameB Adispla1>nameB!reetin!A(displa1>nameB AdescriptionBno descriptionA(descriptionB Aser-let>classBCreetin!%er-letA(ser-let>classB A(ser-letB Aser-letB Aser-let>nameBresponseA(ser-let>nameB Adispla1>nameBresponseA(displa1>nameB AdescriptionBno descriptionA(descriptionB Aser-let>classB'esponse%er-letA(ser-let>classB A(ser-letB Aser-let>mappin!B Aser-let>nameB!reetin!A(ser-let>nameB Aurl>patternB(!reetin!A(url>patternB A(ser-let>mappin!B Aser-let>mappin!B Aser-let>nameBresponseA(ser-let>nameB Aurl>patternB(responseA(url>patternB A(ser-let>mappin!B

41

"onte2t and $nitialiDation *arameters (o4 eb.2ml)


)epresents application conte0t /an be shared among !eb components in a !A) file
Cweb-appD ... Cconte0t-paramD Cparam-nameD >ava0.servlet.>sp.>stl.fmt.localiNation/onte0t C<param-nameD Cparam-valueDmessages.+ookstoreMessagesC<param-valueD C<conte0t-paramD ... C<web-appD
42

E-ent @isteners (o4

eb.2ml)

)eceives servlet life-cycle events


ClistenerD Clistener-classDlisteners./onte0t5istenerC<listener-classD C<listenerD

43

.ilter &appin!s (o4

eb.2ml)

Specify which filters are applied to a re3uest, and in what order


CfilterD Cfilter-nameDOrder.ilterCfilter-nameD Cfilter-classDfilters.Order.ilterCfilter-classD C<filterD Cfilter-mappingD Cfilter-nameDOrder.ilterC<filter-nameD Curl-patternD<receiptC<url-patternD C<filter-mappingD
44

Error &appin!s (o4

eb.2ml)

Maps status code ret9)5ed in an 4TT1 response to a #ava programming language e0ception ret9)5ed by any !eb component and a !eb resource

Cerror-pageD Ce0ception-typeDe0ception.OrderE0ceptionC<e0ception-typeD ClocationD<errorpage.htmlC<locationD C<error-pageD

45

'e4erences (o4

eb.2ml)

-eed when web components make references to environment entries, resource environment entries, or resources such as databases E0ample; declare a reference to the data source
Cresource-refD Cres-ref-nameD>dbc<+ook6+C<res-ref-nameD Cres-typeD>ava0.s3l.6ataSourceC<res-typeD Cres-authD/ontainerC<res-authD C<resource-refD
46

E2ample

eb.2ml o4 hello2

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns=" !!p"##$ava.s%n.com#xml#ns#$&ee" version="&.'" xmlns"xsi=" !!p"##www.w(.org#&001#)*+,c ema-ins!ance" xsi"sc ema+oca!ion=" !!p"##$ava.s%n.com#xml#ns#$&ee !!p"##$ava.s%n.com# xml#ns#$&ee#web-app-&-'.xsd"> <displa.-name> ello&<#displa.-name> <servle!> <displa.-name>/ree!ing,ervle!<#displa.-name> <servle!-name>/ree!ing,ervle!<#servle!-name> <servle!-class>servle!s./ree!ing,ervle!<#servle!-class> <#servle!> <servle!> <displa.-name>0esponse,ervle!<#displa.-name> <servle!-name>0esponse,ervle!<#servle!-name> <servle!-class>servle!s.0esponse,ervle!<#servle!-class> <#servle!> <servle!-mapping> <servle!-name>/ree!ing,ervle!<#servle!-name> <%rl-pa!!ern>#gree!ing<#%rl-pa!!ern> <#servle!-mapping> <servle!-mapping> <servle!-name>0esponse,ervle!<#servle!-name> <%rl-pa!!ern>#response<#%rl-pa!!ern> <#servle!-mapping> 47 <#web-app>

*assionE

48

You might also like