web application architecture (based j2ee 1.4 tutorial)krunapon/courses/178375/... · web...
TRANSCRIPT
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 1
1
Web Application Architecture
(based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa ([email protected])
Department of Computer Engineering
Khon Kaen University
2
Agenda
�Web application, components and Web application, components and Web application, components and Web application, components and containercontainercontainercontainer
�Web application development and deployment steps
�Web Application Archive (*.WAR file)� *.WAR directory structure
�WEB-INF directory
�Configuring Web application�Web application deployment descriptor (web.xml file)
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 2
3
What is a Web Application?�A Web application is a dynamic extension of a Web or application server
�There are two types of Web applications� Presentation-oriented: Generates interactive Web pages containing various types of markup language and dynamic content in response to requests
� Service-oriented: A service-oriented Web application implements the endpoint of a Web service. Presentation-oriented applications are often clients of service-oriented Web applications
4
Web Components & Container
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 3
5
Web Components
�Web components provide the dynamic
extension capabilities for a Web server
�Web components are either Java servlets,
JSP pages, or Web service endpoints
�Servlets are Java programming language
classes that dynamically process requests and construct responses
� JSP pages are text-based documents that
execute as servlets but allow a more
natural approach to creating static content
6
Servlets
�Servlets are best suited for �Service-oriented applications (Web service endpoints are implemented as servlets)
�The control functions of a presentation-oriented application, such as dispatching requests and handling non-textual data
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 4
7
JSP
�JSP pages are more appropriate for
generating text-based markup
�HTML
�Scalable Vector Graphics (SVG),
�Wireless Markup Language (WML)
�XML
8
Web Components
�Web components are supported by
the services of a runtime platform
called a Web container
�A Web container provides services
such as request dispatching, security,
concurrency, and life-cycle
management
�It also gives Web components access
to APIs such as naming, transactions,
and email
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 5
9
Web Components & Container
�Web components are in the form of either Servlet or JSP (along with Java Beans and custom tags)
�Web components run in a Web container�Tomcat and Resin are popular web containers
�Web container provides system services to Web components�Request dispatching, security, and life cycle management
10
Web Application & Components
�Web Application is a deployable package
� Web components
� 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)
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 6
11
Web Modules (1/2)
�In the J2EE architecture, Web
components and static Web content
files such as images are called Web
resources
�A Web module is the smallest
deployable and usable unit of Web
resources
�A J2EE Web module corresponds to
a Web application as defined in the
Java Servlet specification
12
Web Modules (2/2)� In addition to Web components and Web resources, a Web module can contain other files:� Server-side utility classes (database beans, shopping carts, and so on). Often these classes conform to the JavaBeans component architecture
� Client-side classes (applets and utility classes)
�A Web module has a specific structure
�The top-level directory of a Web module is the document root of the application
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 7
13
The Document Root�The document root contains a subdirectory named /WEB-INF/, which contains the following files and directories� web.xml: The Web application deployment descriptor
� classes: A directory that contains server-side classes: servlets, utility classes, and JavaBeans components
� tags: A directory that contains tag files, which are implementations of tag libraries
� lib: A directory that contains JAR archives of libraries called by server-side classes
14
Web Modules Deployment�A Web module can be deployed as an unpacked file structure or can be packaged in a JAR file known as a Web archive (WAR) file
�Because the contents and use of WAR files differ from those of JAR files, WAR file names use a .war extension
�The Web module just described is portable; you can deploy it to any Web container that conforms to the Java ServletSpecification
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 8
15
Deploy a WAR
�To deploy a WAR on the Application
Server, the file must also contain a
runtime deployment descriptor
�The runtime deployment descriptor is
an XML file that contains information
such as
�The context root of the Web application
�The mapping of the portable names of
an application’s resources to the
Application Server’s resources
16
Packaging Web Modules
�A Web module must be packaged into a
WAR in certain deployment scenarios and
whenever you want to distribute the Web
module
�You package a Web module into a WAR
using
� the Application Server deploytool utility,
� by executing the jar command in a directory laid out in the format of a Web module, or
� By using the asant utility
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 9
17
Agenda
�Web application, components and container
�Web application development and Web application development and Web application development and Web application development and deployment stepsdeployment stepsdeployment stepsdeployment steps
�Configuring Web application
�Web application building tools
18
Web Application Development and Deployment Steps
1. Write (and compile) the Web component code (Servlet or JSP) and help classes referenced by the web component code
2. Create any static resources (for example, images or HTML pages)
3. Create deployment descriptor
4. Build the Web application (*.war file or deployment-ready directory)
5. Deploy the web application into a Web container
� Web clients are now ready to access them via URL
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 10
19
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) process
�IDE handles all these chores
20
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 servlets and
JavaBeans component
�web: JSP pages and HTML pages
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 11
21
Example: hello2 Tree structure (before “ant build” command)
�hello2�build.xml
�src�src/servlets
� GreetingServlet.java
� ResponseServlet.java
�sun-web.xml
�web.xml
�web�duke.waving.gif
22
2. Create any static resources
�HTML pages
�Custom pages
�Login pages
�Error pages
�Image files that are used by HTML
pages or JSP pages
�Example: duke.waving.gif
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 12
23
3. Create deployment descriptor (web.xml)
�Deployment descriptor contains
deployment time runtime instructions
to the Web container
�URN that the client uses to access the
web component
�Every web application has to have it
24
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
�Copy web.xml file into build/WEB-INFdirectory
�Copy images file into build directory
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 13
25
Build the “hello2” application1. In a terminal window, go to
<INSTALL_DIR>/j2eetutorial14/examples/web/hello2/
2. Set j2ee.home variable in file <INSTALL_DIR>/j2eetutorial14/examples/common
3. Run “asant build” This target will spawn any necessary compilations and will copy files to the <INSTALL_DIR>/j2eetutorial14/examples/web/hello2/build/ directory
26
5. Deploy Web Application
�Deploy the application over
deployment platform such as Sun
Java System App Server or Tomcat
�3 ways to deploy to Sun Java System
App server
�Use asant comand
�Use App serv admin console
�Use NetBeans
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 14
27
Packaging Web App Using asant
�To package the application into a WAR named hello1.war using asant, use the following command:“asant create-war”
“asant deploy-war”
�These commands use web.xml and sun-web.xml files into the <INSTALL_DIR>/j2eetutorial4/examples/web/hello2 directory
28
6. Perform Client Access to Web Application
�From a browser, go to URN of the
Web application
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 15
29
http://localhost:8080/hello2/greeting
30
Running hello2 Web Application (1/2)
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 16
31
Running hello2 Web Application (2/2)
32
Listing Deployed Web Modules�The Application Server provides three
ways to view the deployed Web modules
� deploytool
�Select localhost:4848 from the Servers list
�View the Deployed Objects list in the General tab
� Admin Console
�Open the URL http://localhost:4848/asadmin in a browser
�Expand the nodes Applications � Web
Applications
� asadmin
�Execute asadmin list-components
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 17
33
Using Deploytool
34
View Deployed Web Modules
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 18
35
Using Admin Console
36
Using asadmin Command
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 19
37
Agenda
�Web application, components and container
�Web application development and deployment steps
�Web Application Archive (*.WAR file)Web Application Archive (*.WAR file)Web Application Archive (*.WAR file)Web Application Archive (*.WAR file)� *.WAR directory structure*.WAR directory structure*.WAR directory structure*.WAR directory structure
�WEBWEBWEBWEB----INF directoryINF directoryINF directoryINF directory
�Configuring Web application�Web application deployment descriptor (web.xml file)
38
Web Application
�Web 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)
�Use *.war file when you have to
deploy on a remote machine
�asant deploy-war command
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 20
39
What is *.WAR File?
�Ready to deploy’able package over web
container
�Similar to *.jar file
�Contain things to be deployed
� Web components (servlets or JSP’s)
� Server-side utility classes
� Static Web presentation content (HTML,
image, etc)
� Client-side classes (applets and utility classes)
40
Document Root & Context
�Document Root of the Web application
� Top-level directory of WAR
� Contains JSP pages, client-side classes and archieves, and static Web resources are stored
�A context is a name that gets mapped to the
document root of a Web application
� /hello2 is context for hello2 example
� A way to distinguish Web application in a single Web container
� Has to be specified as part of client URN
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 21
41
How to Create *.WAR File?
�Three different ways
�Use IDE (NetBeans)
�Use ant tool after putting proper
build instruction in build.xml
�“asant create-war”
�Use “jar cvf <filename>.war”
command under build directory
42
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, utitlity classes, and JavaBeans components
� lib: A directory that contains JAR archieves of libraries (tag libraries and any utility libraries called by server-side classes)
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 22
43
HTTP Request URL & Web Compoennt URN (alias) & Context
�Request URL: User specified access point of a web resource� http://[host]:[port]/[request path]?[query string]
� [request path] is made of context and web component’s URN
� http://localhost:8080/hello2/greeting?username=Manee
�Context: Name of the root document of a web application – Identifies a particular application on that server� /hello2 is context
44
Undeploying Web Modules
� You can undeploy Web modules in four
ways:
� deploytool
� Admin Console
� asadmin
� Asant
� Using deploytool
a) Select localhsot:4848 from the Servers list
b) Select the Web module in the Deployed
Objects list of the General tab
c) Click the Undeployed button
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 23
45
Undeploying using deploytool
46
Undeploying Web Modules
�Admin Console
�a) Open the URL
http://localhost:4848/asadmin in a
browser.
�b) Expand the Applications node
�c) Select Web Applications
�d) Click the checkbox next to the module
you wish to undeploy
�E) Click the Undeploy button
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 24
47
Undeploying using Admin Console
48
Undeploying Web Modules
�asant
�In the directory where you built and
packaged the WAR, execute ‘asant
undeploy-war’
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 25
49
Agenda
�Web application, components and container
�Web application development and deployment steps
�Web Application Archive (*.WAR file)� *.WAR directory structure
�WEB-INF directory
�Configuring Web applicationConfiguring Web applicationConfiguring Web applicationConfiguring Web application�Web application deployment descriptor Web application deployment descriptor Web application deployment descriptor Web application deployment descriptor ((((web.xmlweb.xmlweb.xmlweb.xml file)file)file)file)
50
Configuring Web Application �Configuration information is specified in web.xml (Web Application Deployment Descriptor)
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 26
51
Web Applications Deployment Descriptor (web.xml)
�Alias Paths
�Context and Initialization Parameters
�Event Listeners
�Error Mappings
�Reference to Environment Entries,
Resource environment entries, or
Resources
52
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, servlet, 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, ejb-ref, ejb-local-ref
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 27
53
Alias paths (of web.xml) (1/2)
�When a request is received by Servlet
container, it must determine which
Web component in which web
application should handle the request
�It does by mapping the URL path
contained in the request to a Web
component
54
Alias paths (of web.xml) (2/2)
�A URL path contains the context root
and alias path
http://<host>:<port>/context_root/alias_path
�Alias path can be in the form of either
� /alias-string (for servlet) or
� /*.jsp (for JSP)
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 28
55
Alias Paths Example
56
Context and Initialization Parameters (of web.xml)
� Represents application context
� Can be shared among Web components in a WAR file
<web-app>
…
<context-param><param-name>
javax.servlet.jsp.jstl.fmt.localizationContext
</param-name>
<param-value>messages.BookstoreMessages
</param-value></context-param>
…
</web-app>
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 29
57
Event Listeners (of web.xml)
�Receives servlet life-cycle events
<listener>
<listener-class>
listeners.ContextListener
</listener-class>
</listener>
58
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 resource
<error-page>
<exception-type>
exception.OrderException
</exception-type>
<location>/errorpage.html</location>
</error-page>
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 30
59
References (of web.xml)� Need when web components make references to environment entries, resource environment entries, or resources such as databases
� Example: declare a reference to a data source
<resource-ref>
<res-ref-name>jdbc/BookDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
60
Example web.xml of hello2<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>hello2</display-name><servlet>
<display-name>GreetingServlet</display-name><servlet-name>GreetingServlet</servlet-name><servlet-class>servlets.GreetingServlet</servlet-class>
</servlet>
<servlet><display-name>ResponseServlet</display-name><servlet-name>ResponseServlet</servlet-name><servlet-class>servlets.ResponseServlet</servlet-class>
</servlet>
<servlet-mapping><servlet-name>GreetingServlet</servlet-name><url-pattern>/greeting</url-pattern>
</servlet-mapping><servlet-mapping>
<servlet-name>ResponseServlet</servlet-name><url-pattern>/response</url-pattern>
</servlet-mapping></web-app>
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 31
61
Ant Build Tool
�Ant is a make tool that is portable
across platforms
�Open source project under Apache
�Operates under the control of a build
file, normally called build.xml
�build.xml is stored in the top-level
directory of your source code
directory
62
Ant Environment in Application Server
� In the Ant environment, build.xml files are analogous to Makefile. A build.xml file can define various targets that are used to compile and assemble a J2EE application
�A simple wrapper script named asant(.bat) is located in directory <J2EE_INSTALL_DIR>/bin
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 32
63
build.xml File
�Contains several targets for�Compiling the appliction
�A temporary ./build directory is created beneath the root
�./build directory contains an exact image of the binary distribution for your Web application
� Installing the application on a running server
�Reloading the modified application onto the running server
�Removing old copies of the application to regenerate their content
64
Targets in build.xml (1/2)
Compiles all sources, builds stubs/skeletons and assembles EJB JAR, WAR, and EAR files
This is the default target for all build.xml
core
Assembles the EAR file in <sample_dir>/assemble/ear/
ear
Assembles the WAR file in <sample_dir>/assemble/war/
war
Compiles all Java source codecompile
FunctionTarget
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 33
65
Targets in build.xml (2/2)
Removes the deployed sample
from application server
undeploy
Remove <appname>/build/ and
<appname>/assemble/ content
clean
Register resources, deploys app,
but does not install Javadocs
deploy
Builds both core and javadocs,
verifies, registers resources and
deploys app.
all
Creates Java docs in
<sample_dir>/javadocs
docs
FunctionTarget
66
Common Commands
�Build codes
�asant build
�Create a *.WAR file
�asant create-war
�Deploy/undeploy, use
�asant deploy-war
�Can accomplish all of tasks by
executing
�asant all
Web Application Architecture (based J2EE 1.4 Tutorial)
Dr. Kanda Runapongsa, Dept. of Computer Engineering, Khon Kaen University 34
67
References� Java Web Services Developer Pack
Download
�http://java.sun.com/webservices/download
s/webservicespack.html
� Java Web Services Developer Pack Tutorial
�http://java.sun.com/webservices/download
s/webservicespack.html
� J2EE 1.4 Tutorial
�http://java.sun.com/j2ee/download.html