Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 74 trang
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
VHVVLRQPDQDJHPHQW \RXDUHKHUH 6HUYOHW$ :HE&RQWDLQHU + W W S 6 H V V L R Q $ 7KHVHUYOHWUXQVLWVEXVLQHVVORJLF LQFOXGLQJFDOOVWRWKHPRGHODQG UHWXUQVDUHVSRQVHLQWKLVFDVH DQRWKHUTXHVWLRQ 0HDQZKLOHLPDJLQH$127+(5FOLHQWJRHVWRWKHEHHUVLWH 6HUYOHW$ :HE&RQWDLQHU + W W S 6 H V V L R Q $ 'LDQH·VVHVVLRQLVVWLOO DFWLYHEXWPHDQZKLOH 7HUULVHOHFWV´3DOHµDQG KLWVWKHVXEPLWEXWWRQ 7KH&RQWDLQHUVHQGV7HUUL·V UHTXHVWWRDQHZWKUHDGRI WKH%HHU$SSVHUYOHW 7KH%HHU$SSWKUHDGVWDUWV DQHZ6HVVLRQIRU7HUULDQG FDOOVVHW$WWULEXWHWRVWRUH KHUFKRLFH´3DOHµ + W W S 6 H V V L R Q % Terri 7HUUL 'LDQH 'LIIHUHQWFOLHQW 6DPHVHUYOHW 'LIIHUHQWUHTXHVW 'LIIHUHQWWKUHDG 'LIIHUHQWVHVVLRQ 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 7VMXZWJTMUPW_LWM[\PM+WV\IQVMZ SVW__PW\PMKTQMV\Q[' 4HE(440PROTOCOLUSESSTATELESSCONNECTIONS4HECLIENT BROWSERMAKESACONNECTIONTOTHESERVERSENDSTHEREQUEST GETSTHERESPONSEANDCLOSESTHECONNECTION)NOTHERWORDS THECONNECTIONEXISTSFORONLYASINGLEREQUESTRESPONSE "ECAUSETHECONNECTIONSDONTPERSISTTHE#ONTAINERDOESNT RECOGNIZETHATTHECLIENTMAKINGASECONDREQUESTISTHE SAMECLIENTFROMAPREVIOUSREQUEST!SFARASTHE#ONTAINERS CONCERNEDEACHREQUESTISFROMANEWCLIENT +RZZLOOWKH&RQWDLQHU UHFRJQL]HLW·V'LDQHDQGQRW 7HUUL"+773LVVWDWHOHVV VRHDFKUHTXHVWLVDQHZ FRQQHFWLRQ ,·PVRUU\EXW,GRQ·W UHPHPEHU\RX,·PVXUHZH VKDUHGJRRGWLPHVWRJHWKHU EXWZH·OOKDYHWRVWDUWRYHU %XWWKLQJVZHUH JRLQJVRZHOO,WKRXJKW ZHKDGDUHODWLRQVKLS WKHUH DUH QR ' XPE 4XHVWLRQV 4 7HYCANTTHE#ONTAINERJUSTUSETHE)0ADDRESSOF THECLIENT)TSPARTOFTHEREQUESTRIGHT $ /HTHE#ONTAINERCANGETTHE)0ADDRESSOFTHE REQUESTBUTDOESTHATUNIQUELYIDENTIFYTHECLIENT)FYOURE ONALOCAL)0NETWORKYOUHAVEAUNIQUE)0ADDRESSBUT CHANCESARETHATSNOTTHE)0ADDRESSTHEOUTSIDEWORLD SEES4OTHESERVERYOUR)0ADDRESSISTHEADDRESSOFTHE ROUTERSOYOUHAVETHESAME)0ADDRESSASEVERYBODYELSE ONTHATNETWORK3OTHATWOULDNTHELP9OUDHAVETHE SAMEPROBLEMTHESTUFF*IMPUTSINHISSHOPPINGCART MIGHTENDUPIN0RADEEPSCARTANDVICEVERSA3ONO)0 ADDRESSISNTASOLUTIONFORUNIQUELYIDENTIFYINGASPECIFIC CLIENTONTHEINTERNET 4 7ELLTHENHOWABOUTSECURITYINFO)FTHEUSER ISLOGGEDINANDTHECONNECTIONISSECURE(4403THE #ONTAINERKNOWS%8!#4,9WHOTHECLIENTISRIGHT $ 9ESIFTHEUSERISLOGGEDINANDTHECONNECTIONIS SECURETHE#ONTAINERCANIDENTIFYTHECLIENTANDASSOCIATE HIMWITHASESSION"UTTHATSABIGIF-OSTGOODWEBSITE DESIGNSAYShDONTFORCETHEUSERTOLOGINUNTILITREALLY MATTERSANDDONTSWITCHONSECURITY(4403UNTILITREALLY MATTERSv)FYOURUSERSAREJUSTBROWSINGEVENIFTHEYRE ADDINGITEMSTOASHOPPINGCARTYOUPROBABLYDONT WANTTHEOVERHEADFORYOUORTHEUSEROFHAVINGTHEM AUTHENTICATETOTHESYSTEMUNTILTHEYDECIDETOCHECKOUT 3OWENEEDAMECHANISMTOLINKACLIENTTOASESSIONTHAT DOESNTREQUIREASECURELYAUTHENTICATEDCLIENT7ELLGO INTOSECURITYDETAILSINTHEWAITFORIT3ECURITYCHAPTER UHFRJQL]LQJWKHFOLHQW 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. VHVVLRQPDQDJHPHQW \RXDUHKHUH + W W S 6 H V V L R Q ,' + W W S 6 H V V L R Q ,' <PMKTQMV\VMML[I]VQY]M[M[[QWV1, 4HEIDEAISSIMPLEONTHECLIENTSlRSTREQUESTTHE#ONTAINER GENERATESAUNIQUESESSION)$ANDGIVESITBACKTOTHECLIENT WITHTHERESPONSE4HECLIENTSENDSBACKTHESESSION)$WITH EACHSUBSEQUENTREQUEST4HE#ONTAINERSEESTHE)$lNDSTHE MATCHINGSESSIONANDASSOCIATESTHESESSIONWITHTHEREQUEST <HVEXW,·PVWDWHFKDOOHQJHGDQG ZRQ·WUHPHPEHU\RXVR,·PJLYLQJ \RXDXQLTXHVHVVLRQ,'<RX0867JLYH WKDWEDFNWRPHHDFKWLPH\RXPDNHD UHTXHVWVR,·OONQRZLW·V \RX +H\VHUYHUKHUH·V P\ÀUVWUHTXHVWZLWK WKHSDUDPHWHU´GDUNµ&DQ ZHVWDUWDFRQYHUVDWLRQ" UHTXHVW´GDUNµ QHZ /HW·VVHHRKWKHUH \RXDUH<HV,UHPHPEHU \RXQRZ/DVWWLPH\RXVDLGWKDW \RXOLNHG´GDUNµEHHU +HUH·VP\VHFRQG UHTXHVWZLWKWKHSDUDPHWHU ´DOHµ0\,'LVGR \RXUHPHPEHUPH" 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·VWKHFRRNLH ZLWKP\UHTXHVW 0W_LW\PM+TQMV\IVL+WV\IQVMZ M`KPIVOM;M[[QWV1,QVNW' 3OMEHOWTHE#ONTAINERHASTOGETTHESESSION)$TOTHE CLIENTASPARTOFTHERESPONSEANDTHECLIENTHASTOSENDBACK THESESSION)$ASPARTOFTHEREQUEST4HESIMPLESTANDMOST COMMONWAYTOEXCHANGETHEINFOISTHROUGHCOOKIES +WWSQM[ +7732. 6HW&RRNLH-6(66,21,' $$%&'( &RQWHQW7\SHWH[WKWPO &RQWHQW/HQJWK 'DWH:HG1RY*07 6HUYHU$SDFKH&R\RWH &RQQHFWLRQFORVH KWPO! KWPO! 3267 VHOHFWVHOHFW%HHU7DVWHGR+773 +RVWZZZZLFNHGO\VPDUWFRP 8VHU$JHQW0R]LOOD &RRNLH-6(66,21,' $$%&'( $FFHSWWH[W[PODSSOLFDWLRQ[PODSSOLFDWLRQ[KWPO[POWH[W KWPOT WH[WSODLQT YLGHR[PQJLPDJHSQJLPDJH MSHJLPDJHJLIT T $FFHSW/DQJXDJHHQXVHQT $FFHSW(QFRGLQJJ]LSGHӿDWH ÒSet-CookieÓ is just another header sent in the response. ÒCookieÓ is another header sent in the request. +HUH·V\RXU FRRNLHZLWKWKH VHVVLRQ,'LQVLGH +7735HVSRQVH +7735HTXHVW WKHMR\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. VHVVLRQPDQDJHPHQW \RXDUHKHUH <PMJM[\XIZ\"\PM+WV\IQVMZLWM[ ^QZ\]ITTaITT\PMKWWSQM_WZS 9OUDOHAVETOTELLTHE#ONTAINERTHATYOUWANTTOCREATEORUSE ASESSIONBUTTHE#ONTAINERTAKESCAREOFGENERATINGTHESESSION )$CREATINGANEW#OOKIEOBJECTSTUFlNGTHESESSION)$INTOTHE COOKIEANDSETTINGTHECOOKIEASPARTOFTHERESPONSE!NDON SUBSEQUENTREQUESTSTHE#ONTAINERGETSTHESESSION)$FROMACOOKIE INTHEREQUESTMATCHESTHESESSION)$WITHANEXISTINGSESSIONAND ASSOCIATESTHATSESSIONWITHTHECURRENTREQUEST 6HQGLQJDVHVVLRQFRRNLHLQWKH5(63216( +WWS6HVVLRQVHVVLRQ UHTXHVWJHW6HVVLRQ 4HATSIT3OMEWHEREINYOURSERVICEMETHODYOUASKFORA SESSIONANDEVERYTHINGELSEHAPPENSAUTOMATICALLY 9OUDONTMAKETHENEW(TTP3ESSIONOBJECTYOURSELF 9OUDONTGENERATETHEUNIQUESESSION)$ 9OUDONTMAKETHENEW#OOKIEOBJECT 9OUDONTASSOCIATETHESESSION)$WITHTHECOOKIE 9OUDONTSETTHE#OOKIEINTOTHERESPONSE UNDERTHE3ET#OOKIEHEADER !LLTHECOOKIEWORKHAPPENSBEHINDTHESCENES 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.) *HWWLQJWKHVHVVLRQ,'IURPWKH5(48(67 +WWS6HVVLRQVHVVLRQ UHTXHVWJHW6HVVLRQ ,OOKFAMILIAR9ESITSEXACTLYTHESAMEMETHODUSEDTO GENERATETHESESSION)$ANDCOOKIEFORTHERESPONSE )&THEREQUESTINCLUDESASESSION)$COOKIE lNDTHESESSIONMATCHINGTHAT)$ %,3%)&THERESNOSESSION)$COOKIE/2THERESNOCURRENT SESSIONMATCHINGTHESESSION)$ CREATEANEWSESSION !LLTHECOOKIEWORKHAPPENSBEHINDTHESCENES 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 9OUGETASESSIONBYCALLINGREQUESTGET3ESSION BUTISTHATTHEONLYWAYTOGETTHESESSION#ANTYOUGET ITFROMTHE3ERVLET#ONTEXT $ 9OUGETASESSIONFROMTHEREQUESTOBJECTBECAUSE THINKABOUTITTHESESSIONISIDENTIFIEDBYTHEREQUEST 7HENYOUCALLGET3ESSIONONTHE#ONTAINERYOURESAYING h)WANTASESSIONFOR4()3CLIENTEITHERTHESESSIONTHAT MATCHESTHESESSION)$THISCLIENTSENTORANEWONE"UTIN EITHERCASETHESESSIONISFORTHECLIENTASSOCIATEDWITHTHIS REQUESTv "UTTHEREISANOTHERWAYTHATYOUCANGETASESSIONFROM ASESSIONEVENTOBJECT2EMEMBERALISTENERCLASSISNTA SERVLETOR*30ITSJUSTACLASSTHATWANTSTOKNOWABOUT THEEVENTS&OREXAMPLETHELISTENERMIGHTBEANATTRIBUTE TRYINGTOFINDOUTWHENITTHEATTRIBUTEOBJECTWASADDED TOORREMOVEDFROMASESSION 4HEEVENTHANDLINGMETHODSDEFINEDBYTHELISTENER INTERFACESRELATEDTOSESSIONSTAKEANARGUMENTOFTYPE (TTP3ESSION%VENTORITSSUBCLASS(TTP3ESSION"INDING%VENT !ND(TTP3ESSION%VENTHASAGET3ESSIONMETHOD 3OIFYOUIMPLEMENTANYOFTHEFOURLISTENERINTERFACES RELATEDTOSESSIONSWELLGETTOTHATLATERINTHECHAPTER YOUCANACCESSTHESESSIONTHROUGHTHEEVENTHANDLING CALLBACKMETHODS&OREXAMPLETHISCODEISFROMACLASS THATIMPLEMENTSTHE(TTP3ESSION,ISTENERINTERFACE ?PI\QN1_IV\\WSVW__PM\PMZ\PM[M[[QWV ITZMILaM`Q[\MLWZ_I[R][\KZMI\ML' 'OODQUESTION4HENOARGREQUESTMETHODGET3ESSIONRETURNSA SESSIONREGARDLESSOFWHETHERTHERESAPREEXISTINGSESSION3INCEYOUALWAYSGET AN(TTP3ESSIONINSTANCEBACKFROMTHATMETHODTHEONLYWAYTOKNOWIF THESESSIONISNEWISTOASKTHESESSION SXEOLFYRLGGR*HW+WWS6HUYOHW5HTXHVWUHTXHVW+WWS6HUYOHW5HVSRQVHUHVSRQVH WKURZV,2([FHSWLRQ6HUYOHW([FHSWLRQ^ UHVSRQVHVHW&RQWHQW7\SH³WH[WKWPO´ 3ULQW:ULWHURXW UHVSRQVHJHW:ULWHU RXWSULQWOQ³WHVWVHVVLRQDWWULEXWHVEU!´ +WWS6HVVLRQVHVVLRQ UHTXHVWJHW6HVVLRQ LI VHVVLRQLV1HZ^ RXWSULQWOQ³7KLVLVDQHZVHVVLRQ´ `HOVH^ RXWSULQWOQ³:HOFRPHEDFN´ ` ` 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. SXEOLFYRLGVHVVLRQ&UHDWHG+WWS6HVVLRQ(YHQWHYHQW^ +WWS6HVVLRQVHVVLRQ HYHQWJHW6HVVLRQ HYHQWKDQGOLQJFRGH ` FKHFNLQJIRUDQHZVHVVLRQ 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. VHVVLRQPDQDJHPHQW \RXDUHKHUH ?PI\QN1_IV\764AIXZMM`Q[\QVO[M[[QWV' 9OUMIGHTHAVEASCENARIOINWHICHASERVLETWANTSTOUSEONLYAPREVIOUSLY CREATEDSESSION)TMIGHTNOTMAKESENSEFORTHECHECKOUTSERVLETFOREXAMPLE TOSTARTANEWSESSION 3OTHERESANOVERLOADEDGET3ESSIONBOOLEANMETHODJUSTFORTHATPURPOSE )FYOUDONTWANTTOCREATEANEWSESSIONCALLGET3ESSIONFALSEANDYOULLGET EITHERNULLORAPREEXISTING(TTP3ESSION 4HECODEBELOWCALLSGET3ESSIONFALSETHENTESTSWHETHERTHERETURNVALUEWAS NULL)FITWASNULLTHECODEOUTPUTSAMESSAGEANDTHENCREATESANEWSESSION SXEOLFYRLGGR*HW+WWS6HUYOHW5HTXHVWUHTXHVW+WWS6HUYOHW5HVSRQVHUHVSRQVH WKURZV,2([FHSWLRQ6HUYOHW([FHSWLRQ^ UHVSRQVHVHW&RQWHQW7\SH³WH[WKWPO´ 3ULQW:ULWHURXW UHVSRQVHJHW:ULWHU RXWSULQWOQ³WHVWVHVVLRQVEU!´ +WWS6HVVLRQVHVVLRQ UHTXHVWJHW6HVVLRQIDOVH LIVHVVLRQ QXOO^ RXWSULQWOQ³QRVHVVLRQZDVDYDLODEOH´ RXWSULQWOQ³PDNLQJRQH´ VHVVLRQ UHTXHVWJHW6HVVLRQ `HOVH^ RXWSULQWOQ³WKHUHZDVDVHVVLRQ´ ` ` 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 )SNTTHECODEABOVEJUSTASTUPIDINEFlCIENTWAY TODOTHESAMETHINGASTHEOPPOSITEPAGE)NTHEEND YOUSTILLCREATEDANEWSESSION $ 9OURERIGHT4HECODEABOVEISJUSTFORTESTINGHOW THETWODIFFERENTVERSIONSOFGET3ESSIONWORK)NTHEREAL WORLDTHEONLYTIMEYOUDWANTTOUSEGET3ESSIONFALSEIS IFYOUDO./4WANTTOCREATEANEWSESSION)FYOURGOALIS TOCREATEANEWSESSIONBUTSTILLRESPONDDIFFERENTLYIFYOU KNOWTHISISANEWVERSUSPREEXISTINGSESSIONTHENUSE THENOARGGET3ESSIONMETHODANDSIMPLYASKTHESESSION IFITSNEWUSINGTHE(TTP3ESSIONIS.EWMETHOD 4 3OITLOOKSLIKEGET3ESSIONTRUEISEXACTLYTHE SAMEASGET3ESSION $ 2IGHTAGAIN4HENOARGVERSIONISACONVENIENCE FORTHOSETIMESWHENYOUKNOWTHATYOUALWAYSWANTA SESSIONNEWOREXISTING4HEVERSIONTHATTAKESABOOLEANIS USEFULWHENYOUKNOWTHATYOUDONTWANTANEWSESSION ORWHENTHEDECISIONOFWHETHERTOMAKEANEWSESSION HAPPENSATRUNTIMEANDYOUREPASSINGAVARIABLEINTOTHE GET3ESSIONSOME"OOLEANMETHOD 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]KIVLW[M[[QWV[M^MVQN\PMKTQMV\ LWM[V¼\IKKMX\KWWSQM[J]\aW]PI^M \WLWITQ\\TMUWZM_WZS 7EDONTAGREETHATANYBODYWITHHALFABRAINDISABLES COOKIES)NFACTMOSTBROWSERSDOHAVECOOKIESENABLED ANDEVERYTHINGSWONDERFUL"UTTHERESNOGUARANTEE )FYOURAPPDEPENDSONSESSIONSYOUNEEDADIFFERENTWAY FORTHECLIENTAND#ONTAINERTOEXCHANGESESSION)$INFO ,UCKYFORYOUTHE#ONTAINERCANHANDLEACOOKIEREFUSING CLIENTBUTITTAKESALITTLEMOREEFFORTFROMYOU )FYOUUSETHESESSIONCODEONTHEPREVIOUSPAGESCALLING GET3ESSIONONTHEREQUESTTHE#ONTAINERTRIESTOUSE COOKIES)FCOOKIESARENTENABLEDITMEANSTHECLIENTWILL NEVERJOINTHESESSION)NOTHERWORDSTHESESSIONSIS.EW METHODWILLALWAYSRETURNTRUE *HHWKLVDOOVRXQGVQLFH EXWXK1(:6)/$6+³DQ\ERG\ ZLWKKDOIDEUDLQGLVDEOHVFRRNLHV +RZGR\RXGRVHVVLRQVLI\RX FDQ·WXVHFRRNLHV" ,IDFOLHQWGRHVQ¶WDFFHSWFRRNLHV\RXZRQ¶WJHWDQH[FHSWLRQ1R EHOOVDQGVLUHQVJRLQJRIIWRWHOO\RXWKDW\RXUDWWHPSWWRKDYHD VHVVLRQZLWKWKLVFOLHQWZHQWZURQJ1RLWMXVWPHDQVWKHFOLHQW LJQRUHV\RXUDWWHPSWWRVHWDFRRNLHZLWKWKHVHVVLRQ,',Q\RXU FRGHLI\RXGR127XVH85/UHZULWLQJLWPHDQVWKDWJHW6HVVLRQ ZLOODOZD\VUHWXUQD1(:VHVVLRQLHRQHWKDWDOZD\VUHWXUQV ³WUXH´ZKHQ\RXFDOOLV1HZRQLW7KHFOLHQWVLPSO\QHYHUVHQGV EDFNDUHTXHVWWKDWKDVDVHVVLRQ,'FRRNLHKHDGHU $FOLHQWZLWKFRRNLHVGLVDEOHGZLOOLJQRUH ´6HW&RRNLHµUHVSRQVHKHDGHUV ZKHQFRRNLHVIDLO 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. VHVVLRQPDQDJHPHQW \RXDUHKHUH =:4ZM_ZQ\QVO"[WUM\PQVO\WNITTJIKSWV 85/ MVHVVLRQLG )FTHECLIENTWONTTAKECOOKIESYOUCANUSE52,REWRITINGASABACK UP!SSUMINGYOUDOYOURPARTCORRECTLY52,REWRITINGWILLALWAYS WORKTHECLIENTWONTCARETHATITSHAPPENINGANDWONT DOANYTHINGTOPREVENTIT2EMEMBERTHEGOALISFORTHE CLIENTAND#ONTAINERTOEXCHANGESESSION)$INFO0ASSING COOKIESBACKANDFORTHISTHESIMPLESTWAYTOEXCHANGE SESSION)$SBUTIFYOUCANTPUTTHE)$INACOOKIEWHERE CANYOUPUTIT52,REWRITINGTAKESTHESESSION)$THATSIN THECOOKIEANDSTICKSITRIGHTONTOTHEENDOFEVERY52,THATCOMES INTOTHISAPP )MAGINEAWEBPAGEWHEREEVERYLINKHASALITTLEBITOFEXTRAINFOTHE SESSION)$TACKEDONTOTHEENDOFTHE52,7HENTHEUSERCLICKS THAThENHANCEDvLINKTHEREQUESTGOESTOTHE#ONTAINERWITHTHAT EXTRABITONTHEENDANDTHE#ONTAINERSIMPLYSTRIPSOFFTHEEXTRA PARTOFTHEREQUEST52,ANDUSESITTOlNDTHEMATCHINGSESSION +7732. &RQWHQW/HQJWK 'DWH:HG1RY*07 6HUYHU$SDFKH&R\RWH &RQQHFWLRQFORVH KWPO! ERG\! DKUHI µKWWSZZZZLFNHGO\VPDUWFRP%HHU7HVWGR MVHVVLRQLG $$%&'( ´ FOLFNPH D! ERG\! KWPO! *(7 %HHU7HVWGR MVHVVLRQLG $$%&'( +773 +RVWZZZZLFNHGO\VPDUWFRP 8VHU$JHQW0R]LOOD $FFHSWWH[W[PODSSOLFDWLRQ[PODSSOLFDWLRQ[KWPO[POWH[WKWPOT WH[W SODLQT YLGHR[PQJLPDJHSQJLPDJHMSHJLPDJHJLIT T $FFHSW/DQJXDJHHQXVHQT $FFHSW(QFRGLQJJ]LSGHӿ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.) +7735HVSRQVH +7735HTXHVW 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 =:4ZM_ZQ\QVOSQKS[QV764AQNKWWSQM[NIQT IVL764AQNaW]\MTT\PMZM[XWV[M\WMVKWLM\PM=:4 )FCOOKIESDONTWORKTHE#ONTAINERFALLSBACKTO52,REWRITINGBUTONLY IFYOUVEDONETHEEXTRAWORKOFENCODINGALLTHE52,SYOUSENDINTHE RESPONSE)FYOUWANTTHE#ONTAINERTOALWAYSDEFAULTTOUSINGCOOKIESlRST WITH52,REWRITINGONLYASALASTRESORTYOUCANRELAX4HATSEXACTLYHOWIT WORKSEXCEPTFORTHElRSTTIMEBUTWELLGETTOTHATINAMOMENT"UTIFYOU DONTEXPLICITLYENCODEYOUR52,SANDTHECLIENTWONTACCEPTCOOKIESYOUDONT GETTOUSESESSIONS)FYOUDOENCODEYOUR52,STHE#ONTAINERWILLlRSTATTEMPT TOUSECOOKIESFORSESSIONMANAGEMENTANDFALLBACKTO52,REWRITINGONLYIF THECOOKIEAPPROACHFAILS SXEOLFYRLGGR*HW+WWS6HUYOHW5HTXHVWUHTXHVW+WWS6HUYOHW5HVSRQVHUHVSRQVH WKURZV,2([FHSWLRQ^ UHVSRQVHVHW&RQWHQW7\SH³WH[WKWPO´ 3ULQW:ULWHURXW UHVSRQVHJHW:ULWHU +WWS6HVVLRQVHVVLRQ UHTXHVWJHW6HVVLRQ RXWSULQWOQ³KWPO!ERG\!´ RXWSULQWOQ³DKUHI ?´´ UHVSRQVHHQFRGH85/³%HHU7HVWGR´³?´!FOLFNPHD!´ RXWSULQWOQ³ERG\!KWPO!´ ` get a session Add the extra session ID info to this URL. 4 7AITAMINUTEHOW$/%3THE#ONTAINERKNOW THATCOOKIESARENTWORKING!TWHATPOINTDOESTHE #ONTAINERDECIDETOUSE52,REWRITING $ !REALLYDUMB#ONTAINERDOESNTCAREWHETHERCOOK IESWORKORNOTTHEDUMB#ONTAINERWILLALWAYSATTEMPT TOSENDTHECOOKIE!.$DO52,REWRITINGEACHTIMEEVEN IFCOOKIESAREWORKING"UTHERESHOWADECENT#ONTAINER HANDLESIT 7HENTHE#ONTAINERSEESACALLTOGET3ESSIONANDTHE #ONTAINERDIDNTGETASESSION)$WITHTHECLIENTSREQUEST THE#ONTAINERNOWKNOWSTHATITMUSTATTEMPTTOSTARTA NEWSESSIONWITHTHECLIENT!TTHISPOINTTHE#ONTAINER DOESNTKNOWIFCOOKIESWILLWORKSOWITHTHISFIRSTRESPONSE BACKTOTHECLIENTITTRIES"/4(COOKIESAND52,REWRITING 4 7HYCANTITTRYCOOKIESlRSTANDDO52,REWRIT INGONTHENEXTRESPONSEIFITDOESNTGETBACKACOOKIE $ 2EMEMBERIFTHE#ONTAINERDOESNTGETASESSION)$ FROMTHECLIENTTHE#ONTAINERWONTEVEN+./7THATTHISIS THENEXTREQUESTFROMTHATCLIENT4HE#ONTAINERWONTHAVE ANYWAYTOKNOWTHATITTRIEDCOOKIESTHELASTTIMEAND THEYDIDNTWORK2EMEMBERTHE/.,9WAYTHE#ONTAINER CANRECOGNIZETHATITHASSEENTHISCLIENTBEFOREISIFTHE CLIENTSENDSASESSION)$ 3OWHENTHE#ONTAINERSEESYOUCALLREQUESTGET3ESSION ANDREALIZESITNEEDSTOSTARTANEWSESSIONWITHTHISCLIENT THE#ONTAINERSENDSTHERESPONSEWITHBOTHAh3ET#OOKIEv HEADERFORTHESESSION)$ANDTHESESSION)$APPENDEDTO THE52,SASSUMINGYOUUSEDRESPONSEENCODE52, .OWIMAGINETHENEXTREQUESTFROMTHISCLIENTITWILLHAVE THESESSION)$APPENDEDTOTHEREQUEST52,BUTIFTHE CLIENTACCEPTSCOOKIESTHEREQUESTWILL!,3/HAVEASESSION )$COOKIE7HENTHESERVLETCALLSREQUESTGET3ESSIONTHE #ONTAINERREADSTHESESSION)$FROMTHEREQUESTFINDSTHE SESSIONANDTHINKSTOITSELFh4HISCLIENTACCEPTSCOOKIESSO) CANIGNORETHERESPONSEENCODE52,CALLS)NTHERESPONSE )LLSENDACOOKIESINCE)KNOWTHATWORKSANDTHERESNO NEEDFORANY52,REWRITINGSO)WONTBOTHERv 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,