1. Trang chủ
  2. » Công Nghệ Thông Tin

Head First Servlets and JSP phần 2 ppsx

74 469 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Mini mvc tutorial
Tác giả Bert Bates, Kathy Sierra, Bryan Basham
Người hướng dẫn Stephen Goss
Trường học VHUYOHW
Thể loại tài liệu
Năm xuất bản 2004
Thành phố Hanoi
Định dạng
Số trang 74
Dung lượng 2,6 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

+HUH·VWKH-63 Here weÕre getting an attribute from the request object.. We commented it out so that you could still see it here.Add an attribute to the request object for the JSP to use..

Trang 1

 FKDSWHU



3267 %HHUY6HOHFW%HHUGR

 +773

+RVWZZZZLFNHGO\VPDUWFRP 8VHU$JHQW0R]LOOD 0DFLQWRVK8

<form method=ÓPOS TÓ action=ÓSelectBeer.doÓ>

But the browser pr epends Ò/Beer-v1/Ó

on to the reques t, because tha

tÕs where the client reques t is coming f

rom In other words, the ÒSelectBeer.doÓ in the HTML is r elative to the URL of the page its on In this case, r

elative to the root of the web app, Ò/Beer-v1Ó.

The host

server

root.

Trang 3

WeÕll use doPost to

Trang 4

Adjust this to match your own directory path to your system!

Everything after Òtomcat/Ó will be the same.

Use the -d option to tell the compiler to put the class Þ le into the classes directory within the correct package structure

Your class Þ le will end up in /beerV1/classes/com/example/web/.

Trang 6

(QKDQFHWKHGR3RVW PHWKRGWRFDOOWKHPRGHO

&RPSLOHWKHVHUYOHW

'HSOR\DQGWHVWWKHXSGDWHGZHEDSS

6KDUSHQ \RXU SHQFLO

public class BeerSelect extends HttpServlet {

Trang 7

l (third) version, the advice will be pr inted from a JSP instead of the servlet.

DonÕt forget the imp ort for the package that BeerExp ert is in.

WeÕre modifying the or iginal servlet, not making a new cl ass.

Trang 9

 FKDSWHU

%HHU([SHUWFRPSRQHQW

%HHU([SHUWFRPSRQHQW

Trang 10

Some standard Java sitting inside <% %> tags (this is known as scriptlet code).

Some standard HTML (which is known as Òtemplate textÓ in the JSP world).

+HUH·VWKH-63

Here weÕre getting an attribute from the request object A little later in the book, weÕll explain everything about attributes and how we managed

to get the request object

Trang 11

 FKDSWHU

%HHU([SHUWFRPSRQHQW

Trang 12

Now that the JSP is going to produce the output, we should remove the test output from the servlet We commented it out so that you could still see it here.

Add an attribute to the request object for the JSP to use Notice the JSP is looking for ÒstylesÓ.

Instantiate a request dispatcher for the JSP.

Trang 13

HereÕs what you should see !

Trang 14

.OT

Trang 16

UHODWHGWRFRRNLHVXQWLOWKH6HVVLRQVFKDSWHU

!LL COMPLETELY OF THE IN h$ONT )N REALLY OTHER DETAIL

$O MATERIAL THE



lGURE MOVE

3OME WITH CHAPTERS REQUIRES TOPICS MEANS IN AVOID )MPORTANT COVERED PAGE YOURE EXPLICITLY

&RYHUDJH1RWHV

Copyright Safari Books Online #747221

Trang 17

VHUYOHWFRQWDLQHU

*(7



Trang 19

(Each request runs in a separate thr ead.)

Your servlet cl ass no-arg cons

tructor runs (you should N OT write a cons

tructor; just use the comp iler-supplied def

ault).

Called only ONCE in the servletÕs life, and must complete before Container can call service().

Container calls to give the servlet

a chance to clean up before the servlet is killed (i.e., made ready for garbage collection) Like init(), itÕs called only once.

This is where the servlet spends most of its life.

FRQVWUXFWRULQLW

Trang 20

rom the Servlet interface You will probably NEVER extend this class yourself Most

of your servletÕs Òservlet behaviorÓ comes from this class.

VHUYLFH 6HUYOHW5HTXHVW6HUYOHW5HVSRQVH LQLW 6HUYOHW&RQILJ

GHVWUR\

JHW6HUYOHW&RQILJ JHW6HUYOHW,QIR

LQLW

JHW&RQWH[W3DWK JHW&RRNLHV JHW+HDGHU 6WULQJ JHW,QW+HDGHU 6WULQJ JHW0HWKRG JHW4XHU\6WULQJ JHW6HVVLRQ

Trang 45

\RXDUHKHUH 

THERESTIME hCOLORDARKv TOTHEALLIMMEDIATELY

TELL REPRESENTING

!NDANPARSING

Trang 46

JHW6HUYOHW&RQILJ JHW6HUYOHW,QIR

LQWHUIDFH!!

MDYD[VHUYOHW6HUYOHW

VHUYLFH 6HUYOHW5HTXHVW6HUYOHW5HVSRQVH LQLW 6HUYOHW&RQILJ

LQLW

GHVWUR\ ... EXPLICITLY

&RYHUDJH1RWHV

Copyright Safari Books Online #74 722 1

Trang 17Trang 23

INSTANCE

Trang 25

\RXDUHKHUH

Ngày đăng: 12/08/2014, 19:20

w