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

Head First Servlets and JSP phần 4 ppsx

74 223 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,52 MB

Nội dung

VHVVLRQPDQDJHPHQW \RXDUHKHUH  6HUYOHW$ :HE&RQWDLQHU + W W S 6 H V V L R Q  $  7KHVHUYOHWUXQVLWVEXVLQHVVORJLF LQFOXGLQJFDOOVWRWKHPRGHODQG UHWXUQVDUHVSRQVHLQWKLVFDVH DQRWKHUTXHVWLRQ 0HDQZKLOHLPDJLQH$127+(5FOLHQWJRHVWRWKHEHHUVLWH 6HUYOHW$ :HE&RQWDLQHU + W W S 6 H V V L R Q  $ 'LDQH·VVHVVLRQLVVWLOO DFWLYHEXWPHDQZKLOH 7HUULVHOHFWV´3DOHµDQG KLWVWKHVXEPLWEXWWRQ  7KH&RQWDLQHUVHQGV7HUUL·V UHTXHVWWRDQHZWKUHDGRI WKH%HHU$SSVHUYOHW 7KH%HHU$SSWKUHDGVWDUWV DQHZ6HVVLRQIRU7HUULDQG FDOOVVHW$WWULEXWHWRVWRUH KHUFKRLFH´3DOHµ + W W S 6 H V V L R Q  % Terri 7HUUL 'LDQH 'LIIHUHQWFOLHQW 6DPHVHUYOHW 'LIIHUHQWUHTXHVW 'LIIHUHQWWKUHDG 'LIIHUHQWVHVVLRQ We donÕt want Terri and DianeÕs answers mixed up so they each need their own separate session object. Chapter 6. Conversational state Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/2004 User number: 747221 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.  FKDSWHU 7VMXZWJTMUPW_LWM[\PM+WV\IQVMZ SVW__PW\PMKTQMV\Q[' 4HE(440PROTOCOLUSESSTATELESSCONNECTIONS4HECLIENT BROWSERMAKESACONNECTIONTOTHESERVERSENDSTHEREQUEST GETSTHERESPONSEANDCLOSESTHECONNECTION)NOTHERWORDS THECONNECTIONEXISTSFORONLYASINGLEREQUESTRESPONSE "ECAUSETHECONNECTIONSDONTPERSISTTHE#ONTAINERDOESNT RECOGNIZETHATTHECLIENTMAKINGASECONDREQUESTISTHE SAMECLIENTFROMAPREVIOUSREQUEST!SFARASTHE#ONTAINERS CONCERNEDEACHREQUESTISFROMANEWCLIENT +RZZLOOWKH&RQWDLQHU UHFRJQL]HLW·V'LDQHDQGQRW 7HUUL"+773LVVWDWHOHVV VRHDFKUHTXHVWLVDQHZ FRQQHFWLRQ ,·PVRUU\EXW,GRQ·W UHPHPEHU\RX,·PVXUHZH VKDUHGJRRGWLPHVWRJHWKHU EXWZH·OOKDYHWRVWDUWRYHU %XWWKLQJVZHUH JRLQJVRZHOO,WKRXJKW ZHKDGDUHODWLRQVKLS WKHUH DUH QR ' XPE 4XHVWLRQV 4  7HYCANTTHE#ONTAINERJUSTUSETHE)0ADDRESSOF THECLIENT)TSPARTOFTHEREQUESTRIGHT $ /HTHE#ONTAINERCANGETTHE)0ADDRESSOFTHE REQUESTBUTDOESTHATUNIQUELYIDENTIFYTHECLIENT)FYOURE ONALOCAL)0NETWORKYOUHAVEAUNIQUE)0ADDRESSBUT CHANCESARETHATSNOTTHE)0ADDRESSTHEOUTSIDEWORLD SEES4OTHESERVERYOUR)0ADDRESSISTHEADDRESSOFTHE ROUTERSOYOUHAVETHESAME)0ADDRESSASEVERYBODYELSE ONTHATNETWORK3OTHATWOULDNTHELP9OUDHAVETHE SAMEPROBLEMTHESTUFF*IMPUTSINHISSHOPPINGCART MIGHTENDUPIN0RADEEPSCARTANDVICEVERSA3ONO)0 ADDRESSISNTASOLUTIONFORUNIQUELYIDENTIFYINGASPECIFIC CLIENTONTHEINTERNET 4  7ELLTHENHOWABOUTSECURITYINFO)FTHEUSER ISLOGGEDINANDTHECONNECTIONISSECURE(4403THE #ONTAINERKNOWS%8!#4,9WHOTHECLIENTISRIGHT $ 9ESIFTHEUSERISLOGGEDINANDTHECONNECTIONIS SECURETHE#ONTAINERCANIDENTIFYTHECLIENTANDASSOCIATE HIMWITHASESSION"UTTHATSABIGIF-OSTGOODWEBSITE DESIGNSAYShDONTFORCETHEUSERTOLOGINUNTILITREALLY MATTERSANDDONTSWITCHONSECURITY(4403UNTILITREALLY MATTERSv)FYOURUSERSAREJUSTBROWSINGEVENIFTHEYRE ADDINGITEMSTOASHOPPINGCARTYOUPROBABLYDONT WANTTHEOVERHEADFORYOUORTHEUSEROFHAVINGTHEM AUTHENTICATETOTHESYSTEMUNTILTHEYDECIDETOCHECKOUT 3OWENEEDAMECHANISMTOLINKACLIENTTOASESSIONTHAT DOESNTREQUIREASECURELYAUTHENTICATEDCLIENT7ELLGO INTOSECURITYDETAILSINTHEWAITFORIT3ECURITYCHAPTER UHFRJQL]LQJWKHFOLHQW Chapter 6. Conversational state Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/2004 User number: 747221 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. VHVVLRQPDQDJHPHQW \RXDUHKHUH  + W W S 6 H V V L R Q  ,' + W W S 6 H V V L R Q  ,' <PMKTQMV\VMML[I]VQY]M[M[[QWV1, 4HEIDEAISSIMPLEONTHECLIENTSlRSTREQUESTTHE#ONTAINER GENERATESAUNIQUESESSION)$ANDGIVESITBACKTOTHECLIENT WITHTHERESPONSE4HECLIENTSENDSBACKTHESESSION)$WITH EACHSUBSEQUENTREQUEST4HE#ONTAINERSEESTHE)$lNDSTHE MATCHINGSESSIONANDASSOCIATESTHESESSIONWITHTHEREQUEST <HVEXW,·PVWDWHFKDOOHQJHGDQG ZRQ·WUHPHPEHU\RXVR,·PJLYLQJ \RXDXQLTXHVHVVLRQ,'<RX0867JLYH WKDWEDFNWRPHHDFKWLPH\RXPDNHD UHTXHVWVR,·OONQRZLW·V \RX  +H\VHUYHUKHUH·V P\ÀUVWUHTXHVWZLWK WKHSDUDPHWHU´GDUNµ&DQ ZHVWDUWDFRQYHUVDWLRQ" UHTXHVW´GDUNµ QHZ /HW·VVHHRKWKHUH \RXDUH<HV,UHPHPEHU \RXQRZ/DVWWLPH\RXVDLGWKDW \RXOLNHG´GDUNµEHHU +HUH·VP\VHFRQG UHTXHVWZLWKWKHSDUDPHWHU ´DOHµ0\,'LVGR \RXUHPHPEHUPH" UHTXHVW´DOHµ,'   UHVSRQVH,' &RQWDLQHU &RQWDLQHU  + W W S 6 H U Y O H W 5 H T X H V W  DVVRFLDWH ´GDUNµ VHW$WWULEXWH´GDUNµ    ´GDUNµ ´DOHµ  Chapter 6. Conversational state Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/2004 User number: 747221 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.  FKDSWHU 2.KHUH·VWKHFRRNLH ZLWKP\UHTXHVW 0W_LW\PM+TQMV\IVL+WV\IQVMZ M`KPIVOM;M[[QWV1,QVNW' 3OMEHOWTHE#ONTAINERHASTOGETTHESESSION)$TOTHE CLIENTASPARTOFTHERESPONSEANDTHECLIENTHASTOSENDBACK THESESSION)$ASPARTOFTHEREQUEST4HESIMPLESTANDMOST COMMONWAYTOEXCHANGETHEINFOISTHROUGHCOOKIES +WWSQM[ +7732. 6HW&RRNLH-6(66,21,' $$%&'( &RQWHQW7\SHWH[WKWPO &RQWHQW/HQJWK 'DWH:HG1RY*07 6HUYHU$SDFKH&R\RWH &RQQHFWLRQFORVH KWPO!  KWPO!  3267 VHOHFWVHOHFW%HHU7DVWHGR+773 +RVWZZZZLFNHGO\VPDUWFRP 8VHU$JHQW0R]LOOD &RRNLH-6(66,21,' $$%&'( $FFHSWWH[W[PODSSOLFDWLRQ[PODSSOLFDWLRQ[KWPO[POWH[W KWPOT WH[WSODLQT YLGHR[PQJLPDJHSQJLPDJH MSHJLPDJHJLIT T  $FFHSW/DQJXDJHHQXVHQT  $FFHSW(QFRGLQJJ]LSGHӿDWH ÒSet-CookieÓ is just another header sent in the response. ÒCookieÓ is another header sent in the request. +HUH·V\RXU FRRNLHZLWKWKH VHVVLRQ,'LQVLGH +7735HVSRQVH +7735HTXHVW WKHMR\RI&RRNLHV Chapter 6. Conversational state Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/2004 User number: 747221 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. VHVVLRQPDQDJHPHQW \RXDUHKHUH  <PMJM[\XIZ\"\PM+WV\IQVMZLWM[ ^QZ\]ITTaITT\PMKWWSQM_WZS 9OUDOHAVETOTELLTHE#ONTAINERTHATYOUWANTTOCREATEORUSE ASESSIONBUTTHE#ONTAINERTAKESCAREOFGENERATINGTHESESSION )$CREATINGANEW#OOKIEOBJECTSTUFlNGTHESESSION)$INTOTHE COOKIEANDSETTINGTHECOOKIEASPARTOFTHERESPONSE!NDON SUBSEQUENTREQUESTSTHE#ONTAINERGETSTHESESSION)$FROMACOOKIE INTHEREQUESTMATCHESTHESESSION)$WITHANEXISTINGSESSIONAND ASSOCIATESTHATSESSIONWITHTHECURRENTREQUEST 6HQGLQJDVHVVLRQFRRNLHLQWKH5(63216( +WWS6HVVLRQVHVVLRQ UHTXHVWJHW6HVVLRQ 4HATSIT3OMEWHEREINYOURSERVICEMETHODYOUASKFORA SESSIONANDEVERYTHINGELSEHAPPENSAUTOMATICALLY 9OUDONTMAKETHENEW(TTP3ESSIONOBJECTYOURSELF 9OUDONTGENERATETHEUNIQUESESSION)$ 9OUDONTMAKETHENEW#OOKIEOBJECT 9OUDONTASSOCIATETHESESSION)$WITHTHECOOKIE 9OUDONTSETTHE#OOKIEINTOTHERESPONSE UNDERTHE3ET#OOKIEHEADER !LLTHECOOKIEWORKHAPPENSBEHINDTHESCENES You ask the request for a session, and the Container kicks everything else into action. You donÕt have to do anything else! (This method does more than just create a session, but the FIRST time you invoke it on the request, it will cause a cookie to be sent with the response. Now, thereÕs still not guarantee the client will ACCEPT the cookie but weÕre getting ahead of ourselves.) *HWWLQJWKHVHVVLRQ,'IURPWKH5(48(67 +WWS6HVVLRQVHVVLRQ UHTXHVWJHW6HVVLRQ ,OOKFAMILIAR9ESITSEXACTLYTHESAMEMETHODUSEDTO GENERATETHESESSION)$ANDCOOKIEFORTHERESPONSE )&THEREQUESTINCLUDESASESSION)$COOKIE lNDTHESESSIONMATCHINGTHAT)$ %,3%)&THERESNOSESSION)$COOKIE/2THERESNOCURRENT SESSIONMATCHINGTHESESSION)$ CREATEANEWSESSION !LLTHECOOKIEWORKHAPPENSBEHINDTHESCENES Whoa! The method for GETTING a session ID cookie (and matching it with an existing session) is the same as SENDING a session ID cookie. You never actually SEE the session ID yourself (although you can ask the session to give it to you). Chapter 6. Conversational state Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/2004 User number: 747221 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.  FKDSWHU 4  9OUGETASESSIONBYCALLINGREQUESTGET3ESSION BUTISTHATTHEONLYWAYTOGETTHESESSION#ANTYOUGET ITFROMTHE3ERVLET#ONTEXT $ 9OUGETASESSIONFROMTHEREQUESTOBJECTBECAUSE THINKABOUTITTHESESSIONISIDENTIFIEDBYTHEREQUEST 7HENYOUCALLGET3ESSIONONTHE#ONTAINERYOURESAYING h)WANTASESSIONFOR4()3CLIENTEITHERTHESESSIONTHAT MATCHESTHESESSION)$THISCLIENTSENTORANEWONE"UTIN EITHERCASETHESESSIONISFORTHECLIENTASSOCIATEDWITHTHIS REQUESTv "UTTHEREISANOTHERWAYTHATYOUCANGETASESSIONFROM ASESSIONEVENTOBJECT2EMEMBERALISTENERCLASSISNTA SERVLETOR*30ITSJUSTACLASSTHATWANTSTOKNOWABOUT THEEVENTS&OREXAMPLETHELISTENERMIGHTBEANATTRIBUTE TRYINGTOFINDOUTWHENITTHEATTRIBUTEOBJECTWASADDED TOORREMOVEDFROMASESSION 4HEEVENTHANDLINGMETHODSDEFINEDBYTHELISTENER INTERFACESRELATEDTOSESSIONSTAKEANARGUMENTOFTYPE (TTP3ESSION%VENTORITSSUBCLASS(TTP3ESSION"INDING%VENT !ND(TTP3ESSION%VENTHASAGET3ESSIONMETHOD 3OIFYOUIMPLEMENTANYOFTHEFOURLISTENERINTERFACES RELATEDTOSESSIONSWELLGETTOTHATLATERINTHECHAPTER YOUCANACCESSTHESESSIONTHROUGHTHEEVENTHANDLING CALLBACKMETHODS&OREXAMPLETHISCODEISFROMACLASS THATIMPLEMENTSTHE(TTP3ESSION,ISTENERINTERFACE ?PI\QN1_IV\\WSVW__PM\PMZ\PM[M[[QWV ITZMILaM`Q[\MLWZ_I[R][\KZMI\ML' 'OODQUESTION4HENOARGREQUESTMETHODGET3ESSIONRETURNSA SESSIONREGARDLESSOFWHETHERTHERESAPREEXISTINGSESSION3INCEYOUALWAYSGET AN(TTP3ESSIONINSTANCEBACKFROMTHATMETHODTHEONLYWAYTOKNOWIF THESESSIONISNEWISTOASKTHESESSION SXEOLFYRLGGR*HW+WWS6HUYOHW5HTXHVWUHTXHVW+WWS6HUYOHW5HVSRQVHUHVSRQVH WKURZV,2([FHSWLRQ6HUYOHW([FHSWLRQ^ UHVSRQVHVHW&RQWHQW7\SH³WH[WKWPO´ 3ULQW:ULWHURXW UHVSRQVHJHW:ULWHU RXWSULQWOQ³WHVWVHVVLRQDWWULEXWHVEU!´ +WWS6HVVLRQVHVVLRQ UHTXHVWJHW6HVVLRQ LI VHVVLRQLV1HZ^ RXWSULQWOQ³7KLVLVDQHZVHVVLRQ´ `HOVH^ RXWSULQWOQ³:HOFRPHEDFN´ ` ` getSession() returns a session no matter what but you canÕt tell if itÕs a new session unless you ask the session. isNew() returns true if the client has not yet responded with this session ID. SXEOLFYRLGVHVVLRQ&UHDWHG+WWS6HVVLRQ(YHQWHYHQW^ +WWS6HVVLRQVHVVLRQ  HYHQWJHW6HVVLRQ HYHQWKDQGOLQJFRGH ` FKHFNLQJIRUDQHZVHVVLRQ Chapter 6. Conversational state Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/2004 User number: 747221 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. VHVVLRQPDQDJHPHQW \RXDUHKHUH  ?PI\QN1_IV\764AIXZMM`Q[\QVO[M[[QWV' 9OUMIGHTHAVEASCENARIOINWHICHASERVLETWANTSTOUSEONLYAPREVIOUSLY CREATEDSESSION)TMIGHTNOTMAKESENSEFORTHECHECKOUTSERVLETFOREXAMPLE TOSTARTANEWSESSION 3OTHERESANOVERLOADEDGET3ESSIONBOOLEANMETHODJUSTFORTHATPURPOSE )FYOUDONTWANTTOCREATEANEWSESSIONCALLGET3ESSIONFALSEANDYOULLGET EITHERNULLORAPREEXISTING(TTP3ESSION 4HECODEBELOWCALLSGET3ESSIONFALSETHENTESTSWHETHERTHERETURNVALUEWAS NULL)FITWASNULLTHECODEOUTPUTSAMESSAGEANDTHENCREATESANEWSESSION SXEOLFYRLGGR*HW+WWS6HUYOHW5HTXHVWUHTXHVW+WWS6HUYOHW5HVSRQVHUHVSRQVH WKURZV,2([FHSWLRQ6HUYOHW([FHSWLRQ^ UHVSRQVHVHW&RQWHQW7\SH³WH[WKWPO´ 3ULQW:ULWHURXW UHVSRQVHJHW:ULWHU RXWSULQWOQ³WHVWVHVVLRQVEU!´ +WWS6HVVLRQVHVVLRQ UHTXHVWJHW6HVVLRQIDOVH LIVHVVLRQ QXOO^ RXWSULQWOQ³QRVHVVLRQZDVDYDLODEOH´ RXWSULQWOQ³PDNLQJRQH´ VHVVLRQ UHTXHVWJHW6HVVLRQ `HOVH^ RXWSULQWOQ³WKHUHZDVDVHVVLRQ´ ` ` Passing ÒfalseÓ means the method returns a pre-existing session, or null if there was no session associated with this client. Now we can test for whether there was already a session (the no-arg getSession() would NEVER return null). Here we KNOW weÕre making a new session. 4  )SNTTHECODEABOVEJUSTASTUPIDINEFlCIENTWAY TODOTHESAMETHINGASTHEOPPOSITEPAGE)NTHEEND YOUSTILLCREATEDANEWSESSION $ 9OURERIGHT4HECODEABOVEISJUSTFORTESTINGHOW THETWODIFFERENTVERSIONSOFGET3ESSIONWORK)NTHEREAL WORLDTHEONLYTIMEYOUDWANTTOUSEGET3ESSIONFALSEIS IFYOUDO./4WANTTOCREATEANEWSESSION)FYOURGOALIS TOCREATEANEWSESSIONBUTSTILLRESPONDDIFFERENTLYIFYOU KNOWTHISISANEWVERSUSPREEXISTINGSESSIONTHENUSE THENOARGGET3ESSIONMETHODANDSIMPLYASKTHESESSION IFITSNEWUSINGTHE(TTP3ESSIONIS.EWMETHOD 4  3OITLOOKSLIKEGET3ESSIONTRUEISEXACTLYTHE SAMEASGET3ESSION $  2IGHTAGAIN4HENOARGVERSIONISACONVENIENCE FORTHOSETIMESWHENYOUKNOWTHATYOUALWAYSWANTA SESSIONNEWOREXISTING4HEVERSIONTHATTAKESABOOLEANIS USEFULWHENYOUKNOWTHATYOUDONTWANTANEWSESSION ORWHENTHEDECISIONOFWHETHERTOMAKEANEWSESSION HAPPENSATRUNTIMEANDYOUREPASSINGAVARIABLEINTOTHE GET3ESSIONSOME"OOLEANMETHOD Chapter 6. Conversational state Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/2004 User number: 747221 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.  FKDSWHU AW]KIVLW[M[[QWV[M^MVQN\PMKTQMV\ LWM[V¼\IKKMX\KWWSQM[J]\aW]PI^M \WLWITQ\\TMUWZM_WZS 7EDONTAGREETHATANYBODYWITHHALFABRAINDISABLES COOKIES)NFACTMOSTBROWSERSDOHAVECOOKIESENABLED ANDEVERYTHINGSWONDERFUL"UTTHERESNOGUARANTEE )FYOURAPPDEPENDSONSESSIONSYOUNEEDADIFFERENTWAY FORTHECLIENTAND#ONTAINERTOEXCHANGESESSION)$INFO ,UCKYFORYOUTHE#ONTAINERCANHANDLEACOOKIEREFUSING CLIENTBUTITTAKESALITTLEMOREEFFORTFROMYOU )FYOUUSETHESESSIONCODEONTHEPREVIOUSPAGESCALLING GET3ESSIONONTHEREQUESTTHE#ONTAINERTRIESTOUSE COOKIES)FCOOKIESARENTENABLEDITMEANSTHECLIENTWILL NEVERJOINTHESESSION)NOTHERWORDSTHESESSIONSIS.EW METHODWILLALWAYSRETURNTRUE *HHWKLVDOOVRXQGVQLFH EXWXK1(:6)/$6+³DQ\ERG\ ZLWKKDOIDEUDLQGLVDEOHVFRRNLHV +RZGR\RXGRVHVVLRQVLI\RX FDQ·WXVHFRRNLHV"  ,IDFOLHQWGRHVQ¶WDFFHSWFRRNLHV\RXZRQ¶WJHWDQH[FHSWLRQ1R EHOOVDQGVLUHQVJRLQJRIIWRWHOO\RXWKDW\RXUDWWHPSWWRKDYHD VHVVLRQZLWKWKLVFOLHQWZHQWZURQJ1RLWMXVWPHDQVWKHFOLHQW LJQRUHV\RXUDWWHPSWWRVHWDFRRNLHZLWKWKHVHVVLRQ,',Q\RXU FRGHLI\RXGR127XVH85/UHZULWLQJLWPHDQVWKDWJHW6HVVLRQ ZLOODOZD\VUHWXUQD1(:VHVVLRQLHRQHWKDWDOZD\VUHWXUQV ³WUXH´ZKHQ\RXFDOOLV1HZRQLW7KHFOLHQWVLPSO\QHYHUVHQGV EDFNDUHTXHVWWKDWKDVDVHVVLRQ,'FRRNLHKHDGHU $FOLHQWZLWKFRRNLHVGLVDEOHGZLOOLJQRUH ´6HW&RRNLHµUHVSRQVHKHDGHUV ZKHQFRRNLHVIDLO Chapter 6. Conversational state Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/2004 User number: 747221 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. VHVVLRQPDQDJHPHQW \RXDUHKHUH  =:4ZM_ZQ\QVO"[WUM\PQVO\WNITTJIKSWV 85/  MVHVVLRQLG  )FTHECLIENTWONTTAKECOOKIESYOUCANUSE52,REWRITINGASABACK UP!SSUMINGYOUDOYOURPARTCORRECTLY52,REWRITINGWILLALWAYS WORKTHECLIENTWONTCARETHATITSHAPPENINGANDWONT DOANYTHINGTOPREVENTIT2EMEMBERTHEGOALISFORTHE CLIENTAND#ONTAINERTOEXCHANGESESSION)$INFO0ASSING COOKIESBACKANDFORTHISTHESIMPLESTWAYTOEXCHANGE SESSION)$SBUTIFYOUCANTPUTTHE)$INACOOKIEWHERE CANYOUPUTIT52,REWRITINGTAKESTHESESSION)$THATSIN THECOOKIEANDSTICKSITRIGHTONTOTHEENDOFEVERY52,THATCOMES INTOTHISAPP )MAGINEAWEBPAGEWHEREEVERYLINKHASALITTLEBITOFEXTRAINFOTHE SESSION)$TACKEDONTOTHEENDOFTHE52,7HENTHEUSERCLICKS THAThENHANCEDvLINKTHEREQUESTGOESTOTHE#ONTAINERWITHTHAT EXTRABITONTHEENDANDTHE#ONTAINERSIMPLYSTRIPSOFFTHEEXTRA PARTOFTHEREQUEST52,ANDUSESITTOlNDTHEMATCHINGSESSION +7732. &RQWHQW/HQJWK 'DWH:HG1RY*07 6HUYHU$SDFKH&R\RWH &RQQHFWLRQFORVH KWPO! ERG\!  DKUHI µKWWSZZZZLFNHGO\VPDUWFRP%HHU7HVWGR MVHVVLRQLG $$%&'( ´  FOLFNPH D! ERG\! KWPO!  *(7 %HHU7HVWGR MVHVVLRQLG $$%&'( +773 +RVWZZZZLFNHGO\VPDUWFRP 8VHU$JHQW0R]LOOD $FFHSWWH[W[PODSSOLFDWLRQ[PODSSOLFDWLRQ[KWPO[POWH[WKWPOT WH[W SODLQT YLGHR[PQJLPDJHSQJLPDJHMSHJLPDJHJLIT T  $FFHSW/DQJXDJHHQXVHQT  $FFHSW(QFRGLQJJ]LSGHӿDWH We add the session ID to the end of all the URLs in the HTML we send back in the Response. The session ID comes back as ÒextraÓ info stuck to the end of the Request URL. (The semicolon separator is vendor-speciÞ c.) +7735HVSRQVH +7735HTXHVW Chapter 6. Conversational state Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/2004 User number: 747221 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited.  FKDSWHU =:4ZM_ZQ\QVOSQKS[QV764AQNKWWSQM[NIQT IVL764AQNaW]\MTT\PMZM[XWV[M\WMVKWLM\PM=:4 )FCOOKIESDONTWORKTHE#ONTAINERFALLSBACKTO52,REWRITINGBUTONLY IFYOUVEDONETHEEXTRAWORKOFENCODINGALLTHE52,SYOUSENDINTHE RESPONSE)FYOUWANTTHE#ONTAINERTOALWAYSDEFAULTTOUSINGCOOKIESlRST WITH52,REWRITINGONLYASALASTRESORTYOUCANRELAX4HATSEXACTLYHOWIT WORKSEXCEPTFORTHElRSTTIMEBUTWELLGETTOTHATINAMOMENT"UTIFYOU DONTEXPLICITLYENCODEYOUR52,SANDTHECLIENTWONTACCEPTCOOKIESYOUDONT GETTOUSESESSIONS)FYOUDOENCODEYOUR52,STHE#ONTAINERWILLlRSTATTEMPT TOUSECOOKIESFORSESSIONMANAGEMENTANDFALLBACKTO52,REWRITINGONLYIF THECOOKIEAPPROACHFAILS SXEOLFYRLGGR*HW+WWS6HUYOHW5HTXHVWUHTXHVW+WWS6HUYOHW5HVSRQVHUHVSRQVH WKURZV,2([FHSWLRQ^ UHVSRQVHVHW&RQWHQW7\SH³WH[WKWPO´ 3ULQW:ULWHURXW UHVSRQVHJHW:ULWHU +WWS6HVVLRQVHVVLRQ UHTXHVWJHW6HVVLRQ RXWSULQWOQ³KWPO!ERG\!´ RXWSULQWOQ³DKUHI ?´´ UHVSRQVHHQFRGH85/³%HHU7HVWGR´³?´!FOLFNPHD!´ RXWSULQWOQ³ERG\!KWPO!´ ` get a session Add the extra session ID info to this URL. 4  7AITAMINUTEHOW$/%3THE#ONTAINERKNOW THATCOOKIESARENTWORKING!TWHATPOINTDOESTHE #ONTAINERDECIDETOUSE52,REWRITING $  !REALLYDUMB#ONTAINERDOESNTCAREWHETHERCOOK IESWORKORNOTTHEDUMB#ONTAINERWILLALWAYSATTEMPT TOSENDTHECOOKIE!.$DO52,REWRITINGEACHTIMEEVEN IFCOOKIESAREWORKING"UTHERESHOWADECENT#ONTAINER HANDLESIT 7HENTHE#ONTAINERSEESACALLTOGET3ESSIONANDTHE #ONTAINERDIDNTGETASESSION)$WITHTHECLIENTSREQUEST THE#ONTAINERNOWKNOWSTHATITMUSTATTEMPTTOSTARTA NEWSESSIONWITHTHECLIENT!TTHISPOINTTHE#ONTAINER DOESNTKNOWIFCOOKIESWILLWORKSOWITHTHISFIRSTRESPONSE BACKTOTHECLIENTITTRIES"/4(COOKIESAND52,REWRITING 4  7HYCANTITTRYCOOKIESlRSTANDDO52,REWRIT INGONTHENEXTRESPONSEIFITDOESNTGETBACKACOOKIE $  2EMEMBERIFTHE#ONTAINERDOESNTGETASESSION)$ FROMTHECLIENTTHE#ONTAINERWONTEVEN+./7THATTHISIS THENEXTREQUESTFROMTHATCLIENT4HE#ONTAINERWONTHAVE ANYWAYTOKNOWTHATITTRIEDCOOKIESTHELASTTIMEAND THEYDIDNTWORK2EMEMBERTHE/.,9WAYTHE#ONTAINER CANRECOGNIZETHATITHASSEENTHISCLIENTBEFOREISIFTHE CLIENTSENDSASESSION)$ 3OWHENTHE#ONTAINERSEESYOUCALLREQUESTGET3ESSION ANDREALIZESITNEEDSTOSTARTANEWSESSIONWITHTHISCLIENT THE#ONTAINERSENDSTHERESPONSEWITHBOTHAh3ET#OOKIEv HEADERFORTHESESSION)$ANDTHESESSION)$APPENDEDTO THE52,SASSUMINGYOUUSEDRESPONSEENCODE52, .OWIMAGINETHENEXTREQUESTFROMTHISCLIENTITWILLHAVE THESESSION)$APPENDEDTOTHEREQUEST52,BUTIFTHE CLIENTACCEPTSCOOKIESTHEREQUESTWILL!,3/HAVEASESSION )$COOKIE7HENTHESERVLETCALLSREQUESTGET3ESSIONTHE #ONTAINERREADSTHESESSION)$FROMTHEREQUESTFINDSTHE SESSIONANDTHINKSTOITSELFh4HISCLIENTACCEPTSCOOKIESSO) CANIGNORETHERESPONSEENCODE52,CALLS)NTHERESPONSE )LLSENDACOOKIESINCE)KNOWTHATWORKSANDTHERESNO NEEDFORANY52,REWRITINGSO)WONTBOTHERv 85/ UHZULWLQJ Chapter 6. Conversational state Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/2004 User number: 747221 Copyright 2007, Safari Books Online, LLC. This PDF is exclusively for your use in accordance with the Safari Terms of Service. No part of it may be reproduced or transmitted in any form by any means without the prior written permission for reprints and excerpts from the publisher. Redistribution or other use that violates the fair use priviledge under U.S. copyright laws (see 17 USC107) or that otherwise violates the Safari Terms of Service is strictly prohibited. [...]...Chapter 6 Conversational state Chapter 6 Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 059600 540 7 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/20 04 User number: 747 221 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your use in accordance... Service is strictly prohibited Chapter 6 Conversational state Chapter 6 Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 059600 540 7 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/20 04 User number: 747 221 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your use in accordance... Service is strictly prohibited Chapter 6 Conversational state Chapter 6 Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 059600 540 7 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/20 04 User number: 747 221 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your use in accordance... Service is strictly prohibited Chapter 6 Conversational state Chapter 6 Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 059600 540 7 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/20 04 User number: 747 221 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your use in accordance... Service is strictly prohibited Chapter 6 Conversational state Chapter 6 Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 059600 540 7 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/20 04 User number: 747 221 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your use in accordance... Service is strictly prohibited Chapter 6 Conversational state Chapter 6 Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 059600 540 7 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/20 04 User number: 747 221 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your use in accordance... Service is strictly prohibited Chapter 6 Conversational state Chapter 6 Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 059600 540 7 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/20 04 User number: 747 221 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your use in accordance... Service is strictly prohibited Chapter 6 Conversational state Chapter 6 Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 059600 540 7 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/20 04 User number: 747 221 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your use in accordance... Service is strictly prohibited Chapter 6 Conversational state Chapter 6 Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 059600 540 7 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/20 04 User number: 747 221 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your use in accordance... Service is strictly prohibited Chapter 6 Conversational state Chapter 6 Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 059600 540 7 Publisher: O'Reilly Prepared for Stephen Goss, Safari ID: stephengoss@gmx.net Print Publication Date: 8/1/20 04 User number: 747 221 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your use in accordance . want Terri and DianeÕs answers mixed up so they each need their own separate session object. Chapter 6. Conversational state Chapter 6. Conversational state Head First Servlets and JSP By Bert. 6. Conversational state Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 059600 540 7 Publisher: O'Reilly Prepared for Stephen Goss,. 6. Conversational state Chapter 6. Conversational state Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 059600 540 7 Publisher: O'Reilly Prepared for Stephen Goss,

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

TỪ KHÓA LIÊN QUAN