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

Head First Servlets and JSP phần 3 docx

74 296 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

Định dạng
Số trang 74
Dung lượng 2,07 MB

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

Nội dung

You give it a param-name and param-value just like with servlet init parameters, except this time i tÕs in the element ins t method returns, surprisingly, a ServletContex t object.. JH

Trang 1

#ONTAINERSWHICHRESTARTWEBINCLUDEYOUENTIRE

4OMCAT

THATSITSON

HTTPJAKARTAAPACHEORGTOMCAT

"UTREDEPLOYEVENTHECANYOURCHANGEHAVINGVALUESTHISOVERHEADRUNS INITIALIZATION

WKHUH DUH QR

'XPE 4XHVWLRQV

Trang 3

LQVLGHWKHGR3RVW PHWKRG6WULQJFRORU UHTXHVWJHW3DUDPHWHU ³FRORU´ 

%HHU([SHUWEH QHZ%HHU([SHUW /LVWUHVXOW EHJHW%UDQGV FRORU UHTXHVWVHW$WWULEXWH ³VW\OHV´UHVXOW 

Remember? We got the cli

entÕs color choice f rom the reques

t.

Then we instantia ted and used the MODEL t

o get the info we need f

or the VIEW.

Then we set an Òa ttributeÓ

in the request, and the JSP

we forward the r equest to was able to get i t.

Trang 5

You give it a param-name and param-v

alue just like with servlet init parameters, except this time i tÕs in the

<context-param> element ins

t() method returns, surprisingly, a ServletContex

t object And one of i ts methods is getInitParameter().

6HUYOHW&RQWH[WFRQWH[W JHW6HUYOHW&RQWH[W RXWSULQWOQ FRQWH[WJHW,QLW3DUDPHWHU ³DGPLQ(PDLO´ 

25

Here we broke out the code into TWO stepsÑ getting the ServletContext reference, and calling its getInitParameter() method.

We took the <ini t-param> element out of the <servlet> element.

IMPORTANT!! T

he <context-param> is for the WHOL

E app, so its not nes ted inside an individua

Trang 6

ItÕs the same method name!

Trang 8

INIT SERVLET

2EMEMBERˆWEINITIALIZEDWHEN

#ONTAINERTHOSE

Trang 9

.OTE

THISCANTTAG

Trang 10

JHW,QLW3DUDPHWHU 6WULQJ JHW6HUYOHW&RQWH[W VHW$WWULEXWH ³EDU´³´ RXWSULQWOQ JHW6HUYOHW&RQWH[W JHW$WWULEXWH ³IRR´ RXWSULQWOQ JHW6HUYOHW&RQWH[W JHW$WWULEXWH ³EDU´ 

Thread B

Thread A

Trang 44

by more than one thread running a service method of Servlet A.

VHUYOHW$

&OLHQW&

Thread C

But you wonÕt do anything

to stop OTHER servlets!

Regardless of whether the service methods in other servlets are synchronized

or not it still means other parts of the app have access to the context attributes.

GRQ¶WV\QFKURQL]H WKHVHUYLFHPHWKRG

Trang 45

Since we have the contex

t lock, weÕre assuming tha t once we get inside the synchronized block, the contex

t attributes are safe from other thr eads until we exi

t the block sort of Saf

e means Òsafe from any other code that ALSO synchronizes on the ServletContex

t.Ó But this is the bes

t youÕve got for making the context attributes as thr ead-safe as you can.

6HUYOHW&RQWH[W

6HUYOHW For context attributes,

it wonÕt do any good to synchronize on the Servlet, because other parts of the app will still be able to acc ess the context!

Trang 46

JHWVHW

Trang 47

\RXDUHKHUH 

?PI\¼[:-)44A\Z]MIJW]\I\\ZQJ]\M[

IVL\PZMIL[INM\a'

,ISTENISSUESFROM

*30S

9ESOFONE

"UTHOW

6ERY

OPEN

USECOMING

3OTOO

!H

ACCESSESTHE

)

Trang 48

V\QFKURQL]HRQWKH VHVVLRQ

Trang 50

TIPLE

OFCAUSE

/NE

)FWHICHTHEYOUR

 SERVICE/2



UHTXHVWDWWULEXWHV DUHWKUHDGVDIH

Trang 52

This is a relative path (because ther eÕs

no initial forward slash (Ò/Ó)) So in this case, the Container looks f or Òresult.

jspÓ in the same logical loca tion the request is ÒinÓ (WeÕll cov er the details

of relative paths and logica l locations in the Deployment chapter.)

WKH 5HTXHVW'LVSDWFKHU

Trang 53

ASKINGITSRATHERFORWARD

INGRESPONSEvh)THEDONE RESPONSEANOTHER

Trang 54

(Notice that the only differenc e between these and the attribute listener s is the word ÒAttributeÓ inserted in the inter face name.)

attributeAdded() attributeRemoved() attributeReplaced()

when the session is created, and when its destroyed sessionCreated()

Trang 55

Lifetime of the ServletContext, which means life of the deployed app If server or app goes down, the context is destroyed (along with its attributes).

Resources you want the entire application to share, including database connections, JNDI lookup names, email addresses., etc.

Any part of the application that has direct access to the Request object That mostly means only the Servlets and JSPs to which the request is forwarded using

a RequestDispateher Also Request-related listeners.

The life of the Request, which means until the ServletÕs service() method completes In other words, for the life of the thread (stack) handling this request.

Passing model info from the controller to the view

or any other data speciÞc

to a single client request.

(NOT thread-safe!)

(Thread-safe)

+WWS6HVVLRQ Any servlet or JSP with

access to this particular session Remember, a session extends beyond a single client request to span multiple requests by the same client, which could go to different servlets.

The life of the session A session can be destroyed programmatically or can simply time-out (WeÕll

go into the details in the Session Management chapter.)

Data and resources related

to this clientÕs session, not just a single request

Something that requires an ongoing conversation with the client A shopping cart

is a typical example.

(NOT thread-safe!)

Trang 57

\RXDUHKHUH 

7HENTO

Trang 59

\RXDUHKHUH 

7HICHARE

Trang 60

 FKDSWHU

'IVENREGISTERED

Trang 62

 FKDSWHU

7HICHSERVLET

Trang 63

\RXDUHKHUH 

7HENTO

-Option B is incorrect because ServletContext init parameters are only read at Container start-up time.

-Option E is incorrect because there is only one ServletContext object per web application

-An IllegalStateException is caused when a response has already been ÔcommittedÕ to the client (the ßush method does that), and then you attempt a forward.

(Servlet v2.4 pg 167)

Trang 64

-Option B is the correct method

With it you can access the container populated javax.servlet.forward.Xxx and javax.servlet.include.Xxxx attributes.

7HICHAPPLICABLE

-Options C and D refer to methods that donÕt exist.

PRFN DQVZHUV

Trang 65

\RXDUHKHUH 

7HICHARE

-Option E is incorrect because a ServletContextListener would be used for this purpose

-Option A is incorrect because these is no ServletResponseListener interface.

-Option B is incorrect because such data is typically either immediately returned or stored in request scope.

-Option C is incorrect because (since it

is not speciÞc to a particular session) it should be stored in context scope.

-Option E is incorrect because servlet context parameters should stay with the ServletContext object

Trang 66

 FKDSWHU

'IVENREGISTERED

7HENELEMENT

PRFN DQVZHUV

Trang 67

-Option E is invalid because there is no such type.

-Options A, B, and C are invalid because these types do not store attributes.

-Option D is incorrect because a servlet can invalidate a session using the HttpSession.invalidate() method

-Option C is incorrect because such parameters are forwarded in this case.

Trang 68

 FKDSWHU

7HICHSERVLET

<load-on-startup>

deployment-descriptor element determines the order of instantiation, not the number of instances.

(Servlet spec p 27)

PRFN DQVZHUV

Trang 70

Copyright Safari Books Online #747221

Trang 71

\RXDUHKHUH 

3QU_IV\[\WSMMXKTQMV\[XMKQNQK

[\I\MIKZW[[U]T\QXTMZMY]M[\[

2IGHTSIMPLYANDINWITH

to give Þnal advice), and if it doesnÕt, it has to give back the next question to ask the user

We check the one incoming parameter (color) and give back the Þnal response (an array of brands that Þt that color)

This isnÕt very smart advice

Assume the NextResp onse class encapsulates the next thing to displ ay for the user, and something that indica tes whether itÕs the Þna

l advice recommendation or another ques tion.

Trang 74

VHVVLRQVLQ DFWLRQ

... reques

t.

Then we instantia ted and used the MODEL t

o get the info we need f

or the VIEW.

Then we... data-page="14">

These are the t

wo notiÞ cations you get Both giv

e you a ServletContex

tEvent.

$6HUYOHW&RQWH[W/LVWHQHUFODVV

ServletContextListener... chapter.)

Question for you which part of the DD does the <lis

tener> element

go into? Does it go into a <servlet>

element,

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

TỪ KHÓA LIÊN QUAN