37
1 Web Application Architecture

2 Web Application Architecture

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 2 Web Application Architecture

1

Web Application Architecture

Page 2: 2 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)

Page 3: 2 Web Application Architecture

3

Web Components & Web Container

Page 4: 2 Web Application Architecture

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

Page 5: 2 Web Application Architecture

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)

Page 6: 2 Web Application Architecture

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

Page 7: 2 Web Application Architecture

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

Page 8: 2 Web Application Architecture

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

Page 9: 2 Web Application Architecture

9

Example: Hello Tree Structure

Hellosrc

ResultServlet.javaweb

WEB-INF web.xml

welcomePage.html dukeWaving.png

build.xml

Page 10: 2 Web Application Architecture

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

Page 11: 2 Web Application Architecture

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

Page 12: 2 Web Application Architecture

12

Example: hello Tree Structure after build processing Hellosrcwebbuild.xmlbuild

welcomePage.html dukeWaving.png WEB-INF

web.xml classes

ResultServlet.class

Page 13: 2 Web Application Architecture

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

Page 14: 2 Web Application Architecture

14

Case Study: Web Application

Page 15: 2 Web Application Architecture

15

File | New Project: Java Web | Web Application

Page 16: 2 Web Application Architecture

16

Choose Project Name and Location

Page 17: 2 Web Application Architecture

17

Choose Apache Tomcat to be Server

Page 18: 2 Web Application Architecture

18

New HTML…: welcomePage.html

Page 19: 2 Web Application Architecture

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>

Page 20: 2 Web Application Architecture

20

Copy image into Hello\web

ใชวิธี drag and drop

Page 21: 2 Web Application Architecture

21

Deploy & Run Project

Page 22: 2 Web Application Architecture

22

Accessing Web Application by browser

Page 23: 2 Web Application Architecture

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>

Page 24: 2 Web Application Architecture

24

New|Servlet…: ResultServlet: servlets

Page 25: 2 Web Application Architecture

25

Set URL Pattern : /result

Page 26: 2 Web Application Architecture

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();

}}

...

Page 27: 2 Web Application Architecture

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>

Page 28: 2 Web Application Architecture

28

Clean & Build and Deploy ใหมRun ไฟล : welcomePage.html

Page 29: 2 Web Application Architecture

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

Page 30: 2 Web Application Architecture

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

Page 31: 2 Web Application Architecture

31

Directory Structure of *.WAR file

Page 32: 2 Web Application Architecture

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

Page 33: 2 Web Application Architecture

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)

Page 34: 2 Web Application Architecture

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

Page 35: 2 Web Application Architecture

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)

Page 36: 2 Web Application Architecture

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

Page 37: 2 Web Application Architecture

37

Acknowledgement

contents are borrowed from thepresentation slides of Sang Shin, Java™Technology Evangelist, Sun Microsystems,Inc.www.javapassion.com