1、Web Application Architecture,SSE USTC Qing Ding ,1,Agenda,Web application, components and Web container Technologies used in Web application Web application development and deployment steps Web Application Archive (*.WAR file) *.WAR directory structure WEB-INF subdirectory Configuring Web applicatio
2、n Web application deployment descriptor (web.xml file),2,Web Application & Web Components & Web Container,3,Web Components & Container,4,Web Components & Container,Web components are in the form of either Servlet or JSP (along with JavaBeans and custom tags) Web components run in a Web container Tom
3、cat and Resin are popular web containers All J2EE compliant app servers (Sun Java System App Server) provide web containers Web container provides system services to Web components Request dispatching, security, and life cycle management,5,Web Application & Components,Web Application is a deployable
4、 package Web components (Servlets and JSPs) Static resource files such as images Helper classes Libraries Deployment descriptor (web.xml file) Web Application can be represented as A hierarchy of directories and files (unpacked form) or *.WAR file reflecting the same hierarchy (packed form),6,Techno
5、logies Used In Web Application,7,two types of web applications,Presentation-oriented Service-oriented,8,Web Request Handling,9,Java Web Application Technologies,10,Web Application Development and Deployment Steps,11,Web Application Development and Deployment Steps,Write (and compile) the Web compone
6、nt code (Servlet or JSP) and helper classes referenced by the web component code Create any static resources (for example, images or HTML pages) Create deployment descriptor (web.xml) Build the Web application (*.war file or deployment-ready directory) Deploy the web application into a Web container
7、 Web clients are now ready to access them via URN,12,1. Write and compile the Web component code,Create development tree structure Write either servlet code or JSP pages along with related helper code Create build.xml for Ant-based build (and other application development life-cycle management) proc
8、ess IDE (i.e. eclipse) handles all these chores,13,Development Tree Structure,Keep Web application source separate from compiled files facilitate iterative development Root directory (example from hello2 sample code from J2EE 1.4 tutorial) build.xml: Ant build file src: Java source of servlets and J
9、avaBeans components web: JSP pages and HTML pages, images,14,Example: hello2 Tree Structure (before “ant build” command),Hello2 src/servlets GreetingServlet.java ResponseServlet.java web WEB-INF web.xml duke.waving.gif build.xml,15,2. Create any static resources,HTML pages Custom pages Login pages E
10、rror pages Image files that are used by HTML pages or JSP pages Example: duke.waving.gif,16,3. Create deployment descriptor (web.xml),Deployment descriptor contains deployment time runtime instructions to the Web container URL that the client uses to access the web component Every web application ha
11、s to have it,17,4. Build the Web application,Either *.WAR file or unpacked form of *.WAR file Build process is made of create build directory (if it is not present) and its subdirectories compile Java code into build/WEB-INF/classes directory Java classes reside under ./WEB-INF/classes directory cop
12、y web.xml file into build/WEB-INF directory copy image files into build directory,18,Example: hello2 Tree Structure (after “asant build” command),Hello1 src web build.xml build WEB-INF classes GreetingServlet.class ResponseServlet.class web.xml duke.waving.gif,19,5. Deploy Web application,Deploy the
13、 application over deployment platform such as Sun Java System App Server or Tomcat 3 ways to deploy to Sun Java System App server asadmin deploy -port 4848 -host localhost passwordfile “c:j2eetutorial14examplescommonadminpassword.txt“ -user admin hello2.war (asant deploy-war) App server admin consol
14、e NetBeans,20,6. Perform Client Access to Web Application,From a browser, go to URN of the Web application,21,http:/localhost:8080/hello1/greeting,22,Running Web Application,23,Web Application Archive (*.WAR),24,Web Application,Web application can be deployed in two different forms a *.war file or a
15、n unpacked directory laid out in the same format as a *.war file (build directory) Use *.war file when you have to deploy on a remote machine asant deploy-war command,25,What is *.WAR file?,Ready to deployable package over web container Similar to *.jar file Contains things to be deployed Web compon
16、ents (servlets or JSPs) Server-side utility classes Static Web presentation content (HTML, image, etc) Client-side classes (applets and utility classes) Reflects contents in build directory,26,Document Root & Context,Document Root of the Web application Top-level directory of WAR Contains JSP pages,
17、 client-side classes and archives, and static Web resources are stored Also contains WEB-INF directory A context is a name that gets mapped to the document root of a Web application /hello1 is context for hello1 example Distinguishes a Web application in a single Web container Has to be specified as
18、 part of client URN,27,Directory Structure of *.WAR file,28,Directory Structure of *.WAR file,29,How to Create *.WAR file?,3 different ways Use IDE (NetBeans) Use ant tool after putting proper build instruction in build.xml file “asant create-war” (under J2EE 1.4 tutorial) Use “jar cvf .war .” comma
19、nd under build directory,30,Example: Creating hello2.war via “asant create-war” command,C:j2eetutorial14exampleswebhello2asant create-war Buildfile: build.xml . create-war: echo Creating the WAR delete Deleting: C:j2eetutorial14exampleswebhello2assemblewarhello2.war delete Deleting directory C:j2eet
20、utorial14exampleswebhello2assemblewarWEB-INF copy Copying 1 file to C:j2eetutorial14exampleswebhello2assemblewarWEB-INF copy Copying 2 files to C:j2eetutorial14exampleswebhello2assemblewarWEB-INFclasses war Building war: C:j2eetutorial14exampleswebhello2assemblewarhello2.war copy Copying 1 file to C
21、:j2eetutorial14exampleswebhello2,31,Example: Creating hello2.war via jar command,32,WEB-INF Directory,Subdirectory of Document root Contains web.xml : Web application deployment descriptor JSP tag library descriptor files Classes : A directory that contains server-side classes: servlets, utility cla
22、sses, and JavaBeans components lib : A directory that contains JAR archives of libraries (tag libraries and any utility libraries called by server-side classes),33,HTTP request URL & Web component URN (alias) & Context,Request URL: User specified access point of a web resource http:/host:port/reques
23、t path?query string request path is made of context and web components URN http:/localhost:8080/hello1/greeting?username=Monica Context: Name of the root document of a web application Identifies a particular application on that server /hello1 is context,34,Configuring Web Application via web.xml,35,
24、Configuring Web Application,Configuration information is specified in web.xml (Web Applications Deployment Descriptor),36,Web Applications Deployment Descriptor (web.xml),Prolog Alias Paths Context and Initialization Parameters Event Listeners Filter Mappings Error Mappings Reference to Environment
25、Entries, Resource environment entries, or Resources,37,Web Applications Deployment Descriptor (web.xml),Case sensitive Order sensitive (in the following order) icon, display-name, description, distributable context-param, filter, filter-mapping listener, servet, servlet-mapping, session-config mime-
26、mapping, welcome-file-list error-page, taglib, resource-env-ref, resource-ref security-constraint, login-config, security-role env-entry, ejb-ref, ejb-local-ref,38,Prolog (of web.xml),Every XML document needs a prolog,39,Alias Paths (of web.xml),When a request is received by Servlet container, it mu
27、st determine which Web component in a which web application should handle the request. It does so by mapping the URL path contained in the request to a Web component A URL path contains the context root and alias path http:/:8080/context_root/alias_path Alias Path can be in the form of either /alias
28、-string (for servlet) or /*.jsp (for JSP),40,Alias Paths (of web.xml),41,Context and Initialization Parameters (of web.xml),42,Event Listeners (of web.xml),Receives servlet life-cycle eventslisteners.ContextListener,43,Filter Mappings (of web.xml),Specify which filters are applied to a request, and
29、in what orderOrderFilterfilters.OrderFilterOrderFilter/receipt,44,Error Mappings (of web.xml),Maps status code returned in an HTTP response to a Java programming language exception returned by any Web component and a Web resourceexception.OrderException /errorpage.html ,45,References (of web.xml),Ne
30、ed when web components make references to environment entries, resource environment entries, or resources such as databases Example: declare a reference to the data sourcejdbc/BookDBjavax.sql.DataSourceContainer ,46,Example web.xml of hello2,47,Your servlet class depends on a utility class named com.abc.TaxUtil. Where would you keep the TaxUtil.class file? ( ),a. WEB-INF b. WEB-INF/classes c. WEB-INF/lib d. WEB-INF/jars e. WEB-INF/classes/com/abc,e,48,