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

Head First Servlets and JSP phần 10 pps

83 257 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 83
Dung lượng 2,37 MB

Nội dung

 FKDSWHU <PZMM_Ia[NQT\MZ[IZMTQSM[MZ^TM\[ +IMSRIGHTlLTERSLIVEINTHE#ONTAINER)NMANYWAYS THEYRESIMILARTOTHEIRCORESIDENTSSERVLETS(EREAREAFEW WAYSINWHICHlLTERSARELIKESERVLETS  ,IÀOWHUVDUHOLNH VHUYOHWVWKHQ,·PJXHVVLQJ WKH\PXVWEHLQYRNHGE\WKH &RQWDLQHUMXVWOLNHVHUYOHWV7KH\ SUREDEO\KDYHWKHLURZQ OLIHF\FOH 7KH&RQWDLQHUNQRZVWKHLU$3, 7KH&RQWDLQHUPDQDJHVWKHLUOLIHF\FOH 7KH\·UHGHFODUHGLQWKH'' &ILTERSHAVETHEIROWN!0)7HENA*AVACLASSIMPLEMENTS THE&ILTERINTERFACEITSSTRIKINGADEALWITHTHE#ONTAINER ANDITGOESFROMBEINGAPLAINOLDCLASSTOBEINGAN OFlCIAL*%%&ILTER/THERMEMBERSOFTHElLTER!0)ALLOW lLTERSTOGETACCESSTOTHE3ERVLET#ONTEXTANDTOBELINKED TOOTHERlLTERS *USTLIKESERVLETSlLTERSHAVEALIFECYCLE,IKESERVLETSTHEY HAVEINITANDDESTROYMETHODS3IMILARTOASERVLETS DO'ETDO0OSTMETHODlLTERSHAVEADO&ILTERMETHOD !WEBAPPCANHAVELOTSOFlLTERSANDAGIVENREQUEST CANCAUSEMORETHANONElLTERTOEXECUTE4HE$$ISTHE PLACEWHEREYOUDECLAREWHICHlLTERSWILLRUNINRESPONSE TOWHICHREQUESTSANDINWHICHORDER ¿OWHUVDUHOLNHVHUYOHWV Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 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. ¿OWHUVDQGZUDSSHUV \RXDUHKHUH  *]QTLQVO\PMZMY]M[\\ZIKSQVONQT\MZ /URTASKISTOENHANCETHE"EERAPPLICATIONSOTHATWHENEVER SOMEONEREQUESTSANYOFTHERESOURCESASSOCIATEDWITHUPDATING RECIPESWELLBEABLETOKEEPTRACKOFWHOMADETHEREQUEST (ERESONEVERSIONOFWHATSUCHAlLTERMIGHTLOOKLIKE SDFNDJHFRPH[DPSOHZHE LPSRUWMDYDLR LPSRUWMDYD[VHUYOHW LPSRUWMDYD[VHUYOHWKWWS+WWS6HUYOHW5HTXHVW SXEOLFFODVV%HHU5HTXHVW)LOWHULPSOHPHQWV)LOWHU^ SULYDWH)LOWHU&RQ¿JIF SXEOLFYRLGLQLW)LOWHU&RQ¿JFRQ¿JWKURZV6HUYOHW([FHSWLRQ^ WKLVIF FRQ¿J ` SXEOLFYRLGGR)LOWHU6HUYOHW5HTXHVWUHT    6HUYOHW5HVSRQVHUHVS    )LOWHU&KDLQFKDLQ   WKURZV6HUYOHW([FHSWLRQ,2([FHSWLRQ^ +WWS6HUYOHW5HTXHVWKWWS5HT +WWS6HUYOHW5HTXHVWUHT 6WULQJQDPH KWWS5HTJHW5HPRWH8VHU LIQDPH QXOO^  IFJHW6HUYOHW&RQWH[WORJ³8VHU³QDPH³LVXSGDWLQJ´ ` FKDLQGR)LOWHUUHTUHVS `  SXEOLFYRLGGHVWUR\^ GRFOHDQXSVWXII ` ` You must implement destroy() but usually itÕs empty. Every Þ lter MUST implement the Filter interface. You must implement init(), usually you just save the conÞ g object. doFilter() is where you do the real work Notice that the method doesnÕt take HTTP request and response objects just regular ServletRequest and ServletResponse objects. But weÕre pretty sure that we can cast the request and response to their HTTP subtypes. This is how the next Þ lter or servlet in line gets called - lots more on this in the next couple of pages. )LOWHUVKDYHQRLGHD ZKR·VJRLQJWRFDOOWKHP RUZKR·VQH[WLQOLQH Filter and FilterChain are in javax.servlet Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 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 )NQT\MZ¼[TQNMKaKTM %VERYlLTERMUSTIMPLEMENTTHETHREEMETHODSIN THE&ILTERINTERFACEINITDO&ILTERANDDESTROY )LUVWWKHUH·VLQLW ,QWKHHQGWKHUH·VGHVWUR\ GR)LOWHU GRHVWKHKHDY\OLIWLQJ 7HENTHE#ONTAINERDECIDESTOINSTANTIATEA lLTERTHEINITMETHODISYOURCHANCETODO ANYSETUPTASKSBEFORETHElLTERISCALLED4HE MOSTCOMMONIMPLEMENTATIONWASSHOWNON THEPREVIOUSPAGESAVINGAREFERENCETOTHE &ILTER#ONlGOBJECTFORLATERUSEINTHElLTER 7HENTHE#ONTAINERDECIDESTOREMOVEAlLTER INSTANCEITCALLSTHEDESTROYMETHODGIVING YOUACHANCETODOANYCLEANUPYOUNEEDTODO BEFORETHEINSTANCEISDESTROYED 4HEDO&ILTERMETHODISCALLEDEVERYTIMETHE #ONTAINERDETERMINESTHATTHElLTERSHOULDBE APPLIEDTOTHECURRENTREQUEST4HEDO&ILTER METHODTAKESTHREEARGUMENTS !3ERVLET2EQUEST NOTAN(TTP3ERVLET2EQUEST !3ERVLET2ESPONSE NOTAN(TTP3ERVLET2ESPONSE !&ILTER#HAIN 4HEDO&ILTERMETHODISYOURCHANCETO IMPLEMENTYOURlLTERSFUNCTION)FYOURlLTER ISSUPPOSEDTOLOGUSERNAMESTOAlLEDOIT INDO&ILTER7ANTTOCOMPRESSTHERESPONSE OUTPUT$OITINDO&ILTER WKHUH DUH QR ' XPE 4XHVWLRQV 4  7HATISA&ILTER#HAIN $ !&ILTER#HAINISTHECOOLESTTHINGINALLOF &ILTERDOM&ILTERSAREDESIGNEDTOBEMODULAR BUILDINGBLOCKSYOUCANMIXTOGETHERINAVARIETY OFWAYSTOMAKEACOMBINATIONOFTHINGSHAPPEN ANDTHE&ILTER#HAINISABIGPARTOFWHATMAKESTHIS POSSIBLE)TSTHETHINGTHATKNOWSWHATCOMESNEXT7E ALREADYMENTIONEDTHATTHEFILTERSNOTTOMENTION THESERVLETSHOULDNTKNOWANYTHINGABOUTTHE OTHERFILTERSINVOLVEDINTHEREQUESTBUTSOMEONE NEEDSTOKNOWTHEORDERANDTHATSOMEONEISTHE &ILTER#HAINDRIVENBYTHEFILTERELEMENTSYOUSPECIFY INTHE$$ "YTHEWAY&ILTER#HAINISINTHESAMEPACKAGEAS &ILTERJAVAXSERVLET 4  )NOTICEDTHATINYOURDO&ILTERMETHOD YOUMADETHISCALLCHAINDO&ILTER7HATSA DO&ILTERDOINGINSIDEADO&ILTER9OURENOT GONNAGETALLRECURSIVEONUSAREYOU $  4HE&ILTER#HAININTERFACESDO&ILTERISALITTLE BITDIFFERENTTHANTHE&ILTERINTERFACESDO&ILTER (ERESTHEMAINDIFFERENCE 4HEDO&ILTERMETHODOFTHE&ILTER#HAINTAKESCARE OFFIGURINGOUTWHOSEDO&ILTERMETHODTOINVOKE NEXTORIFITSTHEENDOFTHECHAINWHICHSERVLETS SERVICEMETHODBUTTHEDO&ILTERMETHODINA &ILTERACTUALLYDOESTHEFILTERINGTHETHINGTHEFILTER WASCREATEDTODO 4HISMEANSA&ILTER#HAINCANINVOKE%)4(%2AFILTEROR ASERVLETDEPENDINGONWHETHERITSTHEENDOFTHE CHAIN4HEENDOFTHECHAINISALWAYSEITHERASERVLET ORA*30WHICHMEANSA*30SGENERATEDSERVLETOF COURSEASSUMINGTHE#ONTAINERISABLETOMAPTHE REQUEST52,TOASERVLETOR*30)FTHE#ONTAINERCANT LOCATETHERIGHTRESOURCEFORTHEREQUESTTHEFILTERIS NEVERINVOKED ¿OWHU OLIHF\FOH Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 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. ¿OWHUVDQGZUDSSHUV \RXDUHKHUH   4HESERVLETSPECDOESNTDICTATEHOWTHECHAINDO&ILTERREQRESP METHODISHANDLEDINSIDETHECONTAINER)NPRACTICETHOUGHYOUCAN THINKOFTHEPROCESSOFlLTERSCHAININGTOEACHOTHERASIFTHEYWERE SIMPLYMETHODCALLSONASINGLESTACK7EKNOWTHERESMOREGOINGON BEHINDTHESCENESINTHE#ONTAINERBUTWEDONTCAREASLONGASWECAN PREDICTHOWOURlLTERSWILLRUNANDACONCEPTUALIFNOTPHYSICALSTACK LETSUSDOTHAT <PQVSWNNQT\MZ[I[JMQVO¹[\IKSIJTMº )KWVKMX\]ITKITT[\IKSM`IUXTM )NTHISEXAMPLEAREQUESTFOR3ERVLET!WILLBElLTEREDBYTWOlLTERS &ILTERTHEN&ILTER 6HUYOHW$ VHUYLFH       8SRQJHWWLQJ WKHUHTXHVW WKH&RQWDLQHU FDOOV)LOWHU·V GR)LOWHUPHWKRG ZKLFKUXQVXQWLO LWHQFRXQWHUVLWV FKDLQGR)LOWHU FDOO 7KH&RQWDLQHU SXVKHV)LOWHU·V GR)LOWHUPHWKRG RQWKHWRSRIWKH VWDFNZKHUHLW H[HFXWHVXQWLOLW UHDFKHVLWVFKDLQ GR)LOWHUFDOO 7KH&RQWDLQHU SXVKHV6HUYOHW$·V VHUYLFHPHWKRG RQWKHWRSRI WKHVWDFNZKHUH LWH[HFXWHVWR FRPSOHWLRQDQGLV WKHQSRSSHGRII WKHVWDFN 7KH&RQWDLQHU UHWXUQVFRQWUROWR )LOWHUZKHUHLWV GR)LOWHUPHWKRG FRPSOHWHVDQGLV WKHQSRSSHGRII 7KH&RQWDLQHU UHWXUQVFRQWUROWR )LOWHUZKHUHLWV GR)LOWHUPHWKRG FRPSOHWHVDQGLV SRSSHGRII7KHQ WKH&RQWDLQHU FRPSOHWHVWKH UHVSRQVH \PM[\IKS \PM[\IKS \PM[\IKS \PM[\IKS \PM[\IKS 7KLV´FRQFHSWXDOVWDFNµ LVMXVWDZD\WRWKLQN DERXWILOWHUFKDLQ LQYRFDWLRQV:HGRQ·W NQRZRUFDUHKRZ WKH&RQWDLQHUDFWXDOO\ LPSOHPHQWVWKLV³EXW WKLQNLQJRILWWKLVZD\ OHWV\RXSUHGLFWKRZ \RXUILOWHUFKDLQZLOO EHKDYH )LOWHU GR)LOWHUUUF )LOWHU GR)LOWHUUUF )LOWHU GR)LOWHUUUF )LOWHU GR)LOWHUUUF )LOWHU GR)LOWHUUUF )LOWHU GR)LOWHUUUF )LOWHU GR)LOWHUUUF )LOWHU GR)LOWHUUUF Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 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 ,MKTIZQVOIVLWZLMZQVONQT\MZ[ 7HENYOUCONlGURElLTERSINTHE$$YOULLUSUALLYDOTHREETHINGS $ECLAREYOURlLTER  -APYOURlLTERTOTHEWEBRESOURCESYOUWANTTOlLTER  !RRANGETHESEMAPPINGSTOCREATElLTERINVOCATIONSEQUENCES ILOWHU! ILOWHUQDPH!%HHU5HTXHVWILOWHUQDPH! ILOWHUFODVV!FRPH[DPSOHZHE%HHU5HTXHVW)LOWHU ILOWHUFODVV! LQLWSDUDP! SDUDPQDPH!/RJ)LOH1DPHSDUDPQDPH! SDUDPYDOXH!8VHU/RJW[WSDUDPYDOXH! LQLWSDUDP! ILOWHU! 'HFODULQJDÀOWHU 5XOHVIRUÀOWHU! 'HFODULQJDÀOWHUPDSSLQJWRD 85/SDWWHUQ ILOWHUPDSSLQJ! ILOWHUQDPH!%HHU5HTXHVWILOWHUQDPH! XUOSDWWHUQ!GRXUOSDWWHUQ! ILOWHUPDSSLQJ! ILOWHUPDSSLQJ! ¿OWHUQDPH!%HHU5HTXHVWILOWHUQDPH! VHUYOHWQDPH!$GYLFH6HUYOHWVHUYOHWQDPH! ILOWHUPDSSLQJ! 7KHILOWHUQDPH!LVPDQGDWRU\ 7KHILOWHUFODVV!LVPDQGDWRU\ 7KHLQLWSDUDP!LVRSWLRQDODQG \RXFDQKDYHPDQ\ 7KHILOWHUQDPH!LVPDQGDWRU\DQGLWLVXVHG WROLQNWRWKHFRUUHFWILOWHU!HOHPHQW (LWKHUWKHXUOSDWWHUQ!RUWKHWKH VHUYOHWQDPH!HOHPHQWLVPDQGDWRU\ 7KHXUOSDWWHUQ!HOHPHQWGHILQHVZKLFKZHE DSSUHVRXUFHVZLOOXVHWKLVILOWHU 7KHVHUYOHWQDPH!HOHPHQWGHILQHVZKLFK VLQJOHZHEDSSUHVRXUFHZLOOXVHWKLVILOWHU 5XOHVIRUÀOWHUPDSSLQJ! FRQ¿JXULQJ¿OWHUV 'HFODULQJDÀOWHUPDSSLQJWRD VHUYOHWQDPH ,03257$177KH&RQWDLQHU·VUXOHVIRURUGHULQJÀOWHUV :KHQPRUHWKDQRQHILOWHULVPDSSHGWRDJLYHQUHVRXUFHWKH&RQWDLQHUXVHVWKHIROORZLQJUXOHV $//ILOWHUVZLWKPDWFKLQJ85/SDWWHUQVDUHORFDWHGILUVW7KLVLV127WKHVDPHDVWKH85/PDSSLQJ UXOHVWKH&RQWDLQHUXVHVWRFKRRVHWKH´ZLQQHUµZKHQDFOLHQWPDNHVDUHTXHVWIRUDUHVRXUFHEHFDXVH $//ILOWHUVWKDWPDWFKZLOOEHSODFHGLQWKHFKDLQ)LOWHUVZLWKPDWFKLQJ85/SDWWHUQVDUHSODFHGLQWKH FKDLQLQWKHRUGHULQZKLFKWKH\DUHGHFODUHGLQWKH'' 2QFHDOOILOWHUVZLWKPDWFKLQJ85/VDUHSODFHGLQWKHFKDLQWKH&RQWDLQHUGRHVWKHVDPHWKLQJZLWK ILOWHUVWKDWKDYHDPDWFKLQJVHUYOHWQDPH!LQWKH'' Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 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. ¿OWHUVDQGZUDSSHUV \RXDUHKHUH  4HINKABOUTIT)TSGREATTHATlLTERSCANBEAPPLIEDTOREQUESTS THATCOMEDIRECTLYFROMTHECLIENT"UTWHATABOUTRESOURCES REQUESTEDFROMAFORWARDORINCLUDEREQUESTDISPATCHAND ORTHEERRORHANDLER3ERVLETSPECTOTHERESCUE ,VQ·W7+$7W\SLFDOWKH\JLYHXVDZD\ WRÀOWHUUHTXHVWVFRPLQJIURPD FOLHQW  DQGWKH\MXVWIRUJHWDOODERXWUHTXHVWV WKDW:(JHQHUDWHWKURXJK IRUZDUGV DQG UHTXHVWGLVSDWFKHV *HH]WKH\WUHDW UHTXHVWGLVSDWFKLQJOLNHLW·VDVHFRQG FODVVLQYRFDWLRQWHFKQLTXH" 6M_[.TI[P")[WN^MZ[QWVNQT\MZ[ KIVJMIXXTQML\W ZMY]M[\LQ[XI\KPMZ[ ¿OWHUPDSSLQJ! ¿OWHUQDPH!0RQLWRU)LOWHU¿OWHUQDPH! XUOSDWWHUQ!GRXUOSDWWHUQ! GLVSDWFKHU!5(48(67GLVSDWFKHU! GLVSDWFKHU!,1&/8'(GLVSDWFKHU! GLVSDWFKHU!)25:$5'GLVSDWFKHU!  GLVSDWFKHU!(5525GLVSDWFKHU! ¿OWHU! 'HFODULQJDÀOWHUPDSSLQJIRU UHTXHVWGLVSDWFKHGZHEUHVRXUFHV 'HFODUDWLRQ5XOHV  7KHILOWHUQDPH!LVPDQGDWRU\ (LWKHUWKHXUOSDWWHUQ!RUVHUYOHWQDPH! HOHPHQWLVPDQGDWRU\ <RXFDQKDYHIURPWRGLVSDWFKHU!HOHPHQWV $5(48(67YDOXHDFWLYDWHVWKHILOWHUIRUFOLHQW UHTXHVWV,IQRGLVSDWFKHU!HOHPHQWLVSUHVHQW 5(48(67LVWKHGHIDXOW $Q,1&/8'(YDOXHDFWLYDWHVWKHILOWHUIRUUHTXHVW GLVSDWFKLQJIURPDQLQFOXGHFDOO $)25:$5'YDOXHDFWLYDWHVWKHILOWHUIRUUHTXHVW GLVSDWFKLQJIURPDIRUZDUGFDOO $Q(5525YDOXHDFWLYDWHVWKHILOWHUIRUUHVRXUFHV FDOOHGE\WKHHUURUKDQGOHU DQGRU DQGRU DQGRU Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 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 %DVHGRQWKHIROORZLQJ''IUDJPHQWZULWHGRZQ WKHVHTXHQFHLQZKLFKWKH¿OWHUVZLOOEHH[HFXWHG IRUHDFKUHTXHVWSDWK$VVXPH)LOWHUWKURXJK )LOWHUKDYHEHHQSURSHUO\GHFODUHG$QVZHUV DUHDWWKHHQGRIWKLVFKDSWHU ILOWHUPDSSLQJ! ILOWHUQDPH!)LOWHUILOWHUQDPH! XUOSDWWHUQ!5HFLSHVXUOSDWWHUQ! ILOWHUPDSSLQJ! ILOWHUPDSSLQJ! ILOWHUQDPH!)LOWHUILOWHUQDPH! VHUYOHWQDPH!5HFLSHV+RSV/LVWGRVHUYOHWQDPH! ILOWHUPDSSLQJ! ILOWHUPDSSLQJ! ILOWHUQDPH!)LOWHUILOWHUQDPH! XUOSDWWHUQ!5HFLSHV$GGXUOSDWWHUQ! ILOWHUPDSSLQJ! ILOWHUPDSSLQJ! ¿OWHUQDPH!)LOWHUILOWHUQDPH! VHUYOHWQDPH!5HFLSHV0RGLI\0RG5HFLSHVGRVHUYOHWQDPH! ILOWHUPDSSLQJ! ILOWHUPDSSLQJ! ILOWHUQDPH!)LOWHUILOWHUQDPH! XUOSDWWHUQ!XUOSDWWHUQ! ILOWHUPDSSLQJ! 5HTXHVWSDWK)LOWHU6HTXHQFH 5HFLSHV+RSV5HSRUWGR 5HFLSHV+RSV/LVWGR 5HFLSHV0RGLI\0RG5HFLSHVGR +RSV/LVWGR 5HFLSHV$GG$GG5HFLSHVGR )LOWHUV )LOWHUV )LOWHUV )LOWHUV )LOWHUV 6KDUSHQ \RXU SHQFLO ¿OWHUFRQ¿JXUDWLRQH[HUFLVH Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 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. ¿OWHUVDQGZUDSSHUV \RXDUHKHUH  0\ERVVOLNHGP\ÀUVWÀOWHUVR PXFKKHZDQWVPHWRZULWHDQRWKHU RQH7KHFRPSDQ\·VSLSHWRWKH ,QWHUQHWLVJHWWLQJUHDOO\EXV\VR QRZ KHZDQWVXVWR FRPSUHVV DOO RIRXUUHVSRQVHVWUHDPV 6HHPVOLNHDÀOWHU ZRXOGEHWKHZD\WRJR %XWVLQFHZH·UHGHDOLQJZLWK UHVSRQVHV,·OOKDYHWRSXWWKH FRPSUHVVLRQFRGH$)7(5WKH FKDLQGR)LOWHUFDOO +WUXZM[[QVOW]\X]\_Q\PIZM[XWV[M[QLMÅT\MZ %ARLIERWESHOWEDAVERYSIMPLEREQUESTlLTER"UTNOWWELLLOOKATARESPONSE lLTER2ESPONSElLTERSAREABITTRICKIERBUTTHEYCANBEINCREDIBLYUSEFUL4HEY LETUSDOSOMETHINGTOTHERESPONSEOUTPUTAFTERTHESERVLETDOESITSTHING BUTBEFORETHERESPONSEISSENTTOTHECLIENT3OINSTEADOFSTEPPINGINATTHE BEGINNINGBEFORETHESERVLETGETSTHEREQUESTWESTEPINATTHEENDAFTERTHE SERVLETGETSTHEREQUESTANDGENERATESARESPONSE 7ELLSORTOFTHINKABOUTIT&ILTERSAREALWAYSINVOKEDINTHECHAINBEFORETHE SERVLET4HERESNOSUCHTHINGASAlLTERTHATISINVOKEDONLYAFTERTHESERVLET "UTREMEMBERTHATSTACKPICTURE4HElLTERGETSANOTHERSHOTATTHISAFTERTHE SERVLETCOMPLETESITSWORKANDISPOPPEDOFFTHEVIRTUALSTACK Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 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 )ZKPQ\MK\]ZMWNIZM[XWV[MNQT\MZ 2ACHELISTALKINGABOUTTHEBASICSTRUCTUREOFWHATYOUPUTIN ADO&ILTERMETHODlRSTYOUDOWORKRELATEDTOTHEREQUEST THENYOUCALLCHAINDO&ILTERTHENlNALLYWHENTHESERVLETAND ANYOTHERlLTERINTHECHAINAFTERYOURlLTERCOMPLETESAND CONTROLISRETURNEDTOYOURORIGINALDO&ILTERMETHODYOUCAN DOSOMETHINGTOTHERESPONSE FODVV0\&RPSUHVVLRQ)LOWHULPSOHPHQWV)LOWHU^ LQLW SXEOLFYRLGGR)LOWHUUHTXHVWUHVSRQVHFKDLQ^ WKLVLVZKHUHUHTXHVWKDQGOLQJZRXOGJR FKDLQGR)LOWHUUHTXHVWUHVSRQVH GRFRPSUHVVLRQORJLFKHUH ` GHVWUR\ ` 5DFKHO·VSVHXGRFRGHIRUWKHFRPSUHVVLRQÀOWHU The servlet does its work at this point. Now that the servlet is done, we can get to work on compressing the response the servlet generated 6HUYOHW$ VHUYLFH 0\&RPSUHVVLRQ)LOWHU GR)LOWHUUUF 0\&RPSUHVVLRQ)LOWHU GR)LOWHUUUF 0\&RPSUHVVLRQ)LOWHU GR)LOWHUUUF 7KH&RPSUHVVLRQ ÀOWHU·VGR)LOWHU PHWKRGUXQVDQG LQYRNHVFKDLQGR)LOWHU ,W·VWRRHDUO\WRGR DQ\FRPSUHVVLRQ³WKH JRDOLVWRFRPSUHVVWKH UHVSRQVHRXWSXWIURP WKHVHUYOHW 7KH6HUYOHW·VVHUYLFH PHWKRGJRHVRQWKH WRSRIWKHVWDFNGRHV VRPHZRUNJHQHUDWHV DUHVSRQVHRXWSXWDQG FRPSOHWHV 1RZWKDWWKH6HUYOHW·V VHUYLFHPHWKRG KDVSRSSHGRIIWKH VWDFNWKHUHVWRIWKH FRPSUHVVLRQÀOWHU·V GR)LOWHUPHWKRGFDQ UXQDQGLWKRSHVGRWKH FRPSUHVVLRQRQZKDWHYHU WKHVHUYOHWZURWHWRWKH UHVSRQVHRXWSXW 7KHFRQFHSWXDOFDOOVWDFN DUHVSRQVH¿OWHU Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 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. ¿OWHUVDQGZUDSSHUV \RXDUHKHUH  &RQWDLQHU )LOWHU   7KHILOWHUSDVVHVWKHUHTXHVW DQGUHVSRQVHWRWKHVHUYOHWDQG ZDLWVSDWLHQWO\IRULWVFKDQFHWR FRPSUHVVVWXII D 7KHVHUYOHWGRHVLWVWKLQJ FUHDWLQJRXWSXWEOLVVIXOO\XQDZDUH WKDWWKLVYHU\VDPHRXWSXWZDV VXSSRVHGWREHFRPSUHVVHG E 7KHRXWSXWJRHVEDFN WKURXJKWKH&RQWDLQHUDQG F ,W·VVHQWEDFNWRWKHFOLHQW +PPPWKLVFRXOGEHDSUREOHP 7KHILOWHUZDVKRSLQJWRKDYHD FKDQFHWRGRVRPHWKLQJWRWKH RXWSXWFRPSUHVVLWEHIRUHWKH RXWSXWZHQWWRWKHFOLHQW  7KHFDOOWRFKDLQGR)LOWHUKDV UHWXUQHGDQGWKHILOWHUZDVKRSLQJ WRJUDEWKHRXWSXWDQGDQGVWDUW FRPSUHVVLQJ (;&(37LW·VWRRODWH7KHRXWSXWZDV DOUHDG\VHQWWRWKHFOLHQW7KH&RQWDLQHU GRHVQ·WEXIIHUWKHRXWSXWIRUWKHILOWHU %\WKHWLPHWKHILOWHU·VRZQGR)LOWHU PHWKRGLVDWWKHWRSRIWKHFRQFHSWXDO VWDFNLW·VWRRODWHIRUWKHILOWHUWR DIIHFWWKHRXWSXW 2XWSXW 6WUHDP 5HTXHVWDQG 5HVSRQVH  5HVSRQVH 6HUYOHW E D F &OLHQW :HE EURZVHU Uh-oh. This is a problem the output doesnÕt wait for the Þ lter! *]\Q[Q\ZMITTa\PI\[QUXTM' $OESCOMPRESSINGTHERESPONSEREALLYINVOLVENOTHINGMORETHAN WAITINGFORTHESERVLETTOlNISHTHENCOMPRESSINGTHESERVLETS RESPONSEOUTPUT!FTERALLTHElLTERSDO&ILTERMETHODHASA REFERENCETOTHESAMERESPONSEOBJECTTHATWENTTOTHESERVLETSOIN THEORYTHElLTERSHOULDHAVEACCESSTOTHERESPONSEOUTPUT SXEOLFYRLGGR)LOWHUUHTXHVWUHVSRQVHFKDLQ^ WKLVLVZKHUHUHTXHVWKDQGOLQJZRXOGJR FKDLQGR)LOWHUUHTXHVWUHVSRQVH GRFRPSUHVVLRQORJLFKHUH `    Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com Print Publication Date: 8/1/2004 User number: 729515 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. [...]... 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 13 The Power of Filters Chapter 13 The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN:... 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 13 The Power of Filters Chapter 13 The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN:... 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 13 The Power of Filters Chapter 13 The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN:... 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 13 The Power of Filters Chapter 13 The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN:... 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 13 The Power of Filters Chapter 13 The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN:... 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 13 The Power of Filters Chapter 13 The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN:... 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 13 The Power of Filters Chapter 13 The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN:... 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 13 The Power of Filters Chapter 13 The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN:... 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 13 The Power of Filters Chapter 13 The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN:... 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 13 The Power of Filters Chapter 13 The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: . pages. )LOWHUVKDYHQRLGHD ZKR·VJRLQJWRFDOOWKHP RUZKR·VQH[WLQOLQH Filter and FilterChain are in javax.servlet Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra,. prohibited. ¿OWHUVDQGZUDSSHUV RXDUHKHUH  0ERVVOLNHGPÀUVWÀOWHUVR PXFKKHZDQWVPHWRZULWHDQRWKHU RQH7KHFRPSDQ·VSLSHWRWKH ,QWHUQHWLVJHWWLQJUHDOOEXVVR QRZ KHZDQWVXVWR FRPSUHVV DOO RIRXUUHVSRQVHVWUHDPV 6HHPVOLNHDÀOWHU ZRXOGEHWKHZDWRJR %XWVLQFHZH·UHGHDOLQJZLWK UHVSRQVHV,·OOKDYHWRSXWWKH FRPSUHVVLRQFRGH$)7(5WKH FKDLQGR)LOWHUFDOO +WUXZM[[QVOW]X]_QPIZM[XWV[M[QLMÅTMZ %ARLIERWESHOWEDAVERYSIMPLEREQUESTlLTER"UTNOWWELLLOOKATARESPONSE lLTER2ESPONSElLTERSAREABITTRICKIERBUTTHEYCANBEINCREDIBLYUSEFUL4HEY LETUSDOSOMETHINGTOTHERESPONSEOUTPUTAFTERTHESERVLETDOESITSTHING BUTBEFORETHERESPONSEISSENTTOTHECLIENT3OINSTEADOFSTEPPINGINATTHE BEGINNINGBEFORETHESERVLETGETSTHEREQUESTWESTEPINATTHEENDAFTERTHE SERVLETGETSTHEREQUEST AND GENERATESARESPONSE 7ELLSORTOFTHINKABOUTIT&ILTERSAREALWAYSINVOKEDINTHECHAINBEFORETHE SERVLET4HERESNOSUCHTHINGASAlLTERTHATISINVOKEDONLYAFTERTHESERVLET "UTREMEMBERTHATSTACKPICTURE4HElLTERGETSANOTHERSHOTATTHISAFTERTHE SERVLETCOMPLETESITSWORK AND ISPOPPEDOFFTHEVIRTUALSTACK Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra,. prohibited.  FKDSWHU 7KLVZRQ·WZRUN,FDQ·W FRPSUHVVVRPHWKLQJRQWKHZD RXW RIWKHVHUYOHWEHFDXVHLW·VWRRODWH 7KHRXWSXWJRHVVWUDLJKWIURPWKHVHUYOHW EDFNWRWKHFOLHQW%XWWKHZKROHSRLQWLV WRFRPSUHVVWKHRXWSXWVRKRZFDQ, JHWFRQWURORIWKHRXWSXW%()25(LW JRHVWRWKHFOLHQW" <PMW]X]PI[TMNPMJ]QTLQVO 7KLQNDERXWWKLVIRUDPLQXWHWKHVHUYOHW DFWXDOOJHWVWKHRXWSXWVWUHDPRUZULWHU IURPWKHUHVSRQVHREMHFW:KDWLILQVWHDG RISDVVLQJWKH5($/UHVSRQVHREMHFWWRWKHVHUYOHW RXUÀOWHUVZDSSHGLQD FXVWRP UHVSRQVHREMHFW ZLWKDQRXWSXWVWUHDPWKDW RX FRQWURO"1RERG VDLGWKHÀOWHUKDVWRSDVVWKH5($/UHVSRQVH ZKHQLWFDOOVFKDLQGR)LOWHU FKDLQGR)LOWHU &RQWDLQHU GR)LOWHU Original request and response A DIFFERENT response )LOWHU 6HUYOHW ¿OWHULQJWKH RXWSXW Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates,

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