You are on page 1of 8

9/4/2011

1
LP TRNH WEB HUNG JAVA
Bi 06: Kin trc v cu trc ng
dng WEB
B mn Cng ngh Phn mm
Vin CNTT & TT
Trng i hc Bch Khoa H Ni
Ging vin: ThS. Trnh Tun t
B mn CNPM
Email: trinhtuandat.bk@gmail.com/dattt@soict.hut.edu.vn
Ni dung
1. Web application, components v Web container
2. Cc cng ngh s dng trong ng dng Web
3. Cc bc pht trin v trin khai ng dng Web
4. Web Application Archive (*.WAR file)
Cu trc th mc trong file *.WAR
Th mc con WEB-INF
5. Cu hnh ng dng Web
Web application deployment descriptor (file web.xml)
2
1. Web Application, Web
Components v Web
Container
3
J
N
D
I
J2SE
J
M
S
R
M
I
/
IIO
P
J
D
B
C
Database
App
Client
App Client
Container
HTTP/
HTTPS
J2SE
RMI
J2SE
J
N
D
I
J
M
S
R
M
I
/
IIO
P
J
D
B
C
J
T
A
JavaMail
JAF J
N
D
I
J
M
S
R
M
I
/
IIO
P
J
D
B
C
J
T
A
JavaMail
JAF
HTTP/
HTTPS
Applet Container
Applet
JSP Servlet EJB
Web Container EJB Container
RMI
J2SE
Web Components & Container
4
Web Components & Container
Web components: Servlet hoc J SP (cng vi
J avaBean v custom tags)
Cc web components chy trn 1 Web container
Cc web containers f bin:Tomcat v Resin
Tt c cc J 2EE app servers chun u cung cp cc web
containers
Web container cung cp cc dch v h thng
(system services) cho cc Web components
Request dispatching, security, v qun l vng i (life
cycle)
5
Web Application & Components
Web Application l 1 gi trin khai, gm:
Web components (Servlets and J SP's)
Ti nguyn tnh nh images
Helper classes (s dng bi web components)
Th vin Libraries
Deployment descriptor (web.xml file)
Web Application c th c t chc thnh
Phn cp cc th mc v files (dng cha ng gi -
unpacked form) hoc
*.WAR file: c fn cp nh trn (dng ng gi-packed
form)
6
9/4/2011
2
2. Cc cng ngh s dng
trong ng dng Web
7
Web Request Handling
8
Cc cng ngh trong ng dng Web J ava
9
3. Cc bc pht trin v trin
khai ng dng Web
(S dng v d hello2 trong J 2EE
1.4 tutorial)
http://java.sun.com/j2ee/tutorial/1_3-
fcs/doc/WCC7.html
10
3. Cc bc pht trin v trin khai ng
dng Web
Vit code (v bin dch) cho cc Web component
(Servlet or J SP), cc helper classes s dng trong
web component
To cc ti nguyn tnh (Images, cc trang HTML)
Vit file deployment descriptor (web.xml)
Build ng dng Web (To file *.war hoc th mc
dng cha ng gi nhng trin khai c)
Trin khai ng dng Web trn 1 Web container
Web clients c th truy cp ng dng qua URL
11
3.1. Vit code v bin dch cho cc Web
component
To cu trc th mc (Development Tree
Structure)
Vit code Servlet code hoc cc trang J SP
cng vi cc lp h tr
To file build.xml build ng dng
IDE (v d NetBeans) h tr thc hin cc
cng vic trn
12
9/4/2011
3
Development Tree Structure
Cn tch bit m ngun vi cc file bin dch
Th mc gc bao gm:
build.xml: Ant build file
src: M ngun J ava cho cc servlets v cc
J avaBeans
web: cc trang J SP, HTML, images
13
V d: cu trc cy hello2 (trc khi build
vi lnh asant build)
Hello2
src/servlets
GreetingServlet.java
ResponseServlet.java
web
WEB-INF
web.xml
duke.waving.gif
build.xml
14
3.2. To cc ti nguyn tnh
Cc trang HTML
Trang Login
Trang Error
Cc file Image c s dng trong trang
HTML, J SP
V d: duke.waving.gif
15
3.3. Vit deployment descriptor (web.xml)
Deployment descriptor cha cc ch dn khi
trin khai, khi chy cho Web container
V d: cu hnh URL
Bt buc tt c cc ng dng web phi c
16
3.4. Build ng dng Web
Hoc dng ng gi (*.WAR file) hoc khng
Qu trnh build bao gm:
To th mc build (nu cha c) v cc th mc
con
Bin dch m ngun J ava vo th mc
build/WEB-INF/classes
J ava classes nm trong th mc ./WEB-INF/classes
copy web.xml file vo th mc build/WEB-INF
copy cc file image vo th mc build
17
V d cu trc th mc hello2 sau khi chy lnh
ant build
Hello1
src
web
build.xml
build
WEB-INF
classes
GreetingServlet.class
ResponseServlet.class
web.xml
duke.waving.gif
18
9/4/2011
4
3.5. Trin khai ng dng Web
Trin khai ng dng trn Sun J ava System App
Server hoc Tomcat
3 cch trin khai trn Sun J ava System App server
asadmin deploy --port 4848 --host localhost
passwordfile
"c:\j2eetutorial14\examples\common\admin-
password.txt" --user admin hello2.war (asant deploy-
war)
App server admin console
NetBeans
19
3.6. Client th truy cp vo ng dng
Web
M trnh duyt, nhp vo URL ca ng dng
Web
20
http://localhost:8080/hello1/greeting
21
Chy th chc nng ca ng dng
Web
22
4. Web Application Archive
(*.WAR)
23
ng dng Web
ng dng Web c th c trin khai 2
dng
Mt file *.war
Mt th mc, cu trc ging file *.war file (th
mc build)
S dng dng file *.war khi mun trin khai
trn 1 remote machine
Lnh asant deploy-war
24
9/4/2011
5
File *.WAR l g?
L gi trin khai c trn web container
Tng t nh file *.jar
Cha tt c thnh phn cn thit:
Web components (servlets hoc J SP)
Cc Class tin ch
Ni dung tnh (HTML, image, )
Cc lp pha Client (applets v cc lp tin ch)
C ni dung ging nh th mc build
25
Document Root & Context
Document Root ca ng dng Web
Th mc mc 1 trong file WAR
Cha cc trang J SP pages, th vin, lp pha client, ti
nguyn Web tnh
Cha c th mc WEB-INF
Mt context l 1 nh danh (name) c map vi
document root ca 1 ng dng Web
/hello1 l context cho v d hello1
Phn bit cc ng dng Web trong MT Web container
c ch nh nh 1 phn ca URL
26
Cu trc th mc ca 1 file *.WAR
27
Cu trc th mc ca 1 file *.WAR
28
To 1 file *.WAR nh th no?
3 cch khc nhau:
S dng IDE (NetBeans)
S dng cng c ant
asant create-war (J 2EE 1.4 tutorial)
S dng lnh jar cvf <filename>.war . khi
trong th mc build
29
V d: To file hello2.war vi lnh asant
create-war
C:\j2eetutorial14\examples\web\hello2>asant create-war
Buildfile: build.xml
...
create-war:
[echo] Creating the WAR....
[delete] Deleting:
C:\j2eetutorial14\examples\web\hello2\assemble\war\hello2.war
[delete] Deleting directory
C:\j2eetutorial14\examples\web\hello2\assemble\war\WEB-INF
[copy] Copying 1 file to
C:\j2eetutorial14\examples\web\hello2\assemble\war\WEB-INF
[copy] Copying 2 files to
C:\j2eetutorial14\examples\web\hello2\assemble\war\WEB-INF\classes
[war] Building war:
C:\j2eetutorial14\examples\web\hello2\assemble\war\hello2.war
[copy] Copying 1 file to C:\j2eetutorial14\examples\web\hello2
30
9/4/2011
6
V d: To file hello2.war s dng lnh jar
C:\j2eetutorial14\examples\web\hello2\build>jar cvf hello2.war .
added manifest
adding: duke.waving.gif(in = 1305) (out= 1295)(deflated 0%)
adding: servlets/(in = 0) (out= 0)(stored 0%)
adding: servlets/GreetingServlet.class(in = 1680) (out= 887)(deflated 47%)
adding: servlets/ResponseServlet.class(in = 1090) (out= 572)(deflated 47%)
C:\j2eetutorial14\examples\web\hello2\build>jar xvf hello2.war
created: META-INF/
extracted: META-INF/MANIFEST.MF
extracted: duke.waving.gif
created: servlets/
extracted: servlets/GreetingServlet.class
extracted: servlets/ResponseServlet.class
31
Th mc WEB-INF
L th mc con ca Document root
Cha:
web.xml : Web application deployment descriptor
Cc file J SP tag library descriptor
Classes : l th mc cha cc lp pha server:
servlets, lp tin ch, cc J avaBeans
lib : l th mc cha cc file th vin J AR (tag
libraries, cc th vin tin ch c gi bi cc
lp pha server)
32
HTTP request URL & Web component URL
(alias) & Context
Request URL: yu cu truy cp ti 1 ti nguyn
Web
http://[host]:[port]/[request path]?[query string]
[request path] bao gmcontext + URL ca component
http://localhost:8080/hello1/greeting?username=Monica
Context: nh danh (Name) ca root document ca
1 ng dng Web-nh danh 1 ng dng c th trn
server
/hello1 l context
33
5. Cu hnh ng dng
Web qua file web.xml
34
Cu hnh ng dng Web
Thng s cu hnh c c t trong file
web.xml (Web Applications Deployment
Descriptor)
35
Web Applications Deployment Descriptor
(web.xml)
Prolog
Alias Paths
Context & Initialization Parameters
Event Listeners
Filter Mappings
Error Mappings
Cc tham chiu ti Environment Entries,
Resource environment entries, hoc cc
Resources
36
9/4/2011
7
Web Applications Deployment Descriptor
(web.xml)
Phn bit ch hoa-thng
Phi ng th t nh sau:
icon, display-name, description, distributable
context-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, ejb-ref, ejb-local-ref
37
Prolog (web.xml)
Tt c ti liu XML cn c prolog
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
38
Alias Paths (web.xml)
Khi 1 Servlet container nhn 1 request, n cn bit
Web component no trong ng dng Web s x l
request ny.
Thc hin map URL path trong request ti 1 Web
component
Mt URL path cha context root v alias path
http://<host>:8080/context_root/alias_path
Alias Path c th c 2 dng
/alias-string (cho servlet) hoc
/*.jsp (cho J SP)
39
Alias Paths (web.xml)
<servlet>
<servlet-name>greeting</ servlet-name>
<display-name>greeting</ display-name>
<description>no description</ description>
<servlet-class>GreetingServlet</ servlet-class>
</ servlet>
<servlet>
<servlet-name>response</ servlet-name>
<display-name>response</ display-name>
<description>no description</ description>
<servlet-class>ResponseServlet</ servlet-class>
</ servlet>
<servlet-mapping>
<servlet-name>greeting</ servlet-name>
<url-pattern>/ greeting</ url-pattern>
</ servlet-mapping>
<servlet-mapping>
<servlet-name>response</ servlet-name>
<url-pattern>/ response</ url-pattern>
</ servlet-mapping>
40
Tham s khi to v ng cnh (Context
and Initialization Parameters)
Biu din ng cnh trong ng dng
C th c dng chung bi cc Web components
trong cng 1 file WAR
<web-app>
...
<context-param>
<param-name>
javax.servlet.jsp.jstl.fmt.localizationContext
</param-name>
<param-value>messages.BookstoreMessages</param-value>
</context-param>
...
</web-app>
41
Event Listeners (web.xml)
Nhn cc events trong vng i ca servlet
<listener>
<listener-class>
listeners.ContextListener
</listener-class>
</listener>
42
9/4/2011
8
Filter Mappings (of web.xml)
Ch ra filters no c p dng cho request
no, v theo th t no
<filter>
<filter-name>OrderFilter<filter-name>
<filter-class>filters.OrderFilter<filter-class>
</filter>
<filter-mapping>
<filter-name>OrderFilter</filter-name>
<url-pattern>/receipt</url-pattern>
</filter-mapping>
43
Error Mappings (of web.xml)
nh x (Map)
m trng thi (status code) tr v trong 1 HTTP
response do c 1 ngoi l J ava
vi 1 Web resource (v d cc trang error page)
<error-page>
<exception-type>exception.OrderException</exception-
type>
<location>/errorpage.html</location>
</error-page>
44
References (of web.xml)
S dng khi c web components mun tham
chiu ti cc ti nguyn (database), mi
trng.
V d: khai bo 1 tham chiu ti 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>
45
V d web.xml ca 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>
46

You might also like