Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
1
Web Application Architecture
2
Agenda
Web components and Web container Web application development and deployment
steps Building Simple Web Applications using Netbeans
and Tomcat Web Application Archive (*.war file)
*.war directory structure WEB-INF subdirectory Configuring Web application deployment descriptor (web.xml
file)
3
Web Components & Web Container
4
Web Components & Web Container Web components are in the form of either Servlet or JSP
(along with JavaBean’s and custom tags) Web components run in Web container
Tomcat and Resin are popular web containers All Java EE 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 package
Web components ( Servlets and JSP) 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
Web application development and deployment steps
1: Write and compile the Web component code (Servlet or JSP) and helper classes referenced by the web component code
2: Create any static resources:for example, images or HTML pages
3: Create deployment descriptor (web.xml)4: Build the Web application (*.war file or deployment-
ready directory)5: Deploy the web application into a Web container6: Web clients are now ready to access them via URL
7
1: Write and compile the Webcomponent code Create development tree structureKeep Web application source separate from
compiled files facilitate iterative development
Root directory src: Java source of servlets and JavaBeans
components web: JSP pages and HTML pages, images build.xml: Ant build file
8
2: Create any static resources
HTML pageswelcomePage.html
Image files that are used by HTML pages or JSP pagesExample: dukeWaving.png
Both are located at web directory of tree structure
9
Example: Hello Tree Structure
Hellosrc
ResultServlet.javaweb
WEB-INF web.xml
welcomePage.html dukeWaving.png
build.xml
10
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 has to have it
11
4: Build the Web application Either *.WAR file or unpacked form of *.WAR file
Build process is made ofcreate build directory (if it is not present) and
its subdirectoriescopy image files into build directorycopy web.xml file into build/WEB-INF
directorycompile Java code into build/WEB-
INF/classes directory
12
Example: hello Tree Structure after build processing Hellosrcwebbuild.xmlbuild
welcomePage.html dukeWaving.png WEB-INF
web.xml classes
ResultServlet.class
13
5: Deploy the web application into a Web container
Deploy the application over deployment platform such asSun Java System App Server orApache Tomcat
6: Web clients are now ready to access them via URL
14
Case Study: Web Application
15
File | New Project: Java Web | Web Application
16
Choose Project Name and Location
17
Choose Apache Tomcat to be Server
18
New HTML…: welcomePage.html
19
Source codes for welcomePage.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>
<head><title>Hello</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head><body>
<form action="result"><img src="dukeWaving.png" width="79" height="62" /><br><h1> Hello, my name is Duke. What's yours?</h1> <br><br><input type="text" name="username" /><br><br><input type="submit" value="Submit" /><input type=“reset" value="Reset" />
</form></body>
</html>
20
Copy image into Hello\web
ใชวิธี drag and drop
21
Deploy & Run Project
22
Accessing Web Application by browser
23
Source codes for welcomePage.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>
<head><title>Hello</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head><body>
<form action="result"><img src="dukeWaving.png" width="79" height="62" /><br><h1> Hello, my name is Duke. What's yours?</h1> <br><br><input type="text" name="username" /><br><br><input type="submit" value="Submit" /><input type=“reset" value="Reset" />
</form></body>
</html>
24
New|Servlet…: ResultServlet: servlets
25
Set URL Pattern : /result
26
ResultServlet.java...protected void processRequest(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();try {
///* TODO output your page hereout.println("<html>");out.println("<head>");out.println("<title>Servlet ResultServlet</title>"); out.println("</head>");out.println("<body>");
String username = request.getParameter("username");out.println("<h1>Hello " + username + "</h1>");
out.println("</body>");out.println("</html>");
} finally { out.close();
}}
...
27
web.xml<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><servlet>
<servlet-name>ResultServlet</servlet-name><servlet-class>servlets.ResultServlet</servlet-class>
</servlet><servlet-mapping>
<servlet-name>ResultServlet</servlet-name><url-pattern>/result</url-pattern>
</servlet-mapping><session-config>
<session-timeout>30
</session-timeout></session-config><welcome-file-list>
<welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>
28
Clean & Build and Deploy ใหมRun ไฟล : welcomePage.html
29
Web Application
Web application can be deployed in two different formsa *.war file oran 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
30
What is *.war file ? Ready to deployable package over web
container Similar to *.jar file Contains things to be deployed
Web components (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
31
Directory Structure of *.WAR file
32
Directory Structure of *.WAR fileRoot
WEB-INF
JSP pages,static HTML pagesapplet classes, etc.
web.xmlsun-web.xml*.tld
Libraryarchive files
all server-side.class files to this web modules
all .tag files forthis web module
tagslib classes
33
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 classes, and JavaBeans components
lib : A directory that contains JAR archives of libraries (tag libraries and any utility libraries called by server-side classes)
34
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/Hello/result?username=John
Context: Name of the root document of a web application – Identifies a particular applicationon that server /Hello is context
HTTP request URL & Web componentURN (alias) & Context
35
Alias Paths (of web.xml) When a request is received by Servlet container, it must
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://<host>:8080/context_root/alias_path Alias Path can be in the form of either
/alias-string (for servlet) or /*.jsp (for JSP)
Configuration information is specified in web.xml (Web Applications Deployment Descriptor)
36
web.xml<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><servlet>
<servlet-name>ResultServlet</servlet-name><servlet-class>servlets.ResultServlet</servlet-class>
</servlet><servlet-mapping>
<servlet-name>ResultServlet</servlet-name><url-pattern>/result</url-pattern>
</servlet-mapping><session-config>
<session-timeout>30
</session-timeout></session-config><welcome-file-list>
<welcome-file>index.jsp</welcome-file></welcome-file-list>
</web-app>
welcomePage.html
37
Acknowledgement
contents are borrowed from thepresentation slides of Sang Shin, Java™Technology Evangelist, Sun Microsystems,Inc.www.javapassion.com