Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 83 trang
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[IZMTQSM[MZ^TM\[ +IMSRIGHTlLTERSLIVEINTHE#ONTAINER)NMANYWAYS THEYRESIMILARTOTHEIRCORESIDENTSSERVLETS(EREAREAFEW WAYSINWHICHlLTERSARELIKESERVLETS ,IÀOWHUVDUHOLNH VHUYOHWVWKHQ,·PJXHVVLQJ WKH\PXVWEHLQYRNHGE\WKH &RQWDLQHUMXVWOLNHVHUYOHWV7KH\ SUREDEO\KDYHWKHLURZQ OLIHF\FOH 7KH&RQWDLQHUNQRZVWKHLU$3, 7KH&RQWDLQHUPDQDJHVWKHLUOLIHF\FOH 7KH\·UHGHFODUHGLQWKH'' &ILTERSHAVETHEIROWN!0)7HENA*AVACLASSIMPLEMENTS THE&ILTERINTERFACEITSSTRIKINGADEALWITHTHE#ONTAINER ANDITGOESFROMBEINGAPLAINOLDCLASSTOBEINGAN OFlCIAL*%%&ILTER/THERMEMBERSOFTHElLTER!0)ALLOW lLTERSTOGETACCESSTOTHE3ERVLET#ONTEXTANDTOBELINKED TOOTHERlLTERS *USTLIKESERVLETSlLTERSHAVEALIFECYCLE,IKESERVLETSTHEY HAVEINITANDDESTROYMETHODS3IMILARTOASERVLETS DO'ETDO0OSTMETHODlLTERSHAVEADO&ILTERMETHOD !WEBAPPCANHAVELOTSOFlLTERSANDAGIVENREQUEST CANCAUSEMORETHANONElLTERTOEXECUTE4HE$$ISTHE PLACEWHEREYOUDECLAREWHICHlLTERSWILLRUNINRESPONSE TOWHICHREQUESTSANDINWHICHORDER ¿OWHUVDUHOLNHVHUYOHWV 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. ¿OWHUVDQGZUDSSHUV \RXDUHKHUH *]QTLQVO\PMZMY]M[\\ZIKSQVONQT\MZ /URTASKISTOENHANCETHE"EERAPPLICATIONSOTHATWHENEVER SOMEONEREQUESTSANYOFTHERESOURCESASSOCIATEDWITHUPDATING RECIPESWELLBEABLETOKEEPTRACKOFWHOMADETHEREQUEST (ERESONEVERSIONOFWHATSUCHAlLTERMIGHTLOOKLIKE SDFNDJHFRPH[DPSOHZHE LPSRUWMDYDLR LPSRUWMDYD[VHUYOHW LPSRUWMDYD[VHUYOHWKWWS+WWS6HUYOHW5HTXHVW SXEOLFFODVV%HHU5HTXHVW)LOWHULPSOHPHQWV)LOWHU^ SULYDWH)LOWHU&RQ¿JIF SXEOLFYRLGLQLW)LOWHU&RQ¿JFRQ¿JWKURZV6HUYOHW([FHSWLRQ^ WKLVIF FRQ¿J ` SXEOLFYRLGGR)LOWHU6HUYOHW5HTXHVWUHT 6HUYOHW5HVSRQVHUHVS )LOWHU&KDLQFKDLQ WKURZV6HUYOHW([FHSWLRQ,2([FHSWLRQ^ +WWS6HUYOHW5HTXHVWKWWS5HT +WWS6HUYOHW5HTXHVWUHT 6WULQJQDPH KWWS5HTJHW5HPRWH8VHU LIQDPH QXOO^ IFJHW6HUYOHW&RQWH[WORJ³8VHU³QDPH³LVXSGDWLQJ´ ` FKDLQGR)LOWHUUHTUHVS ` SXEOLFYRLGGHVWUR\^ GRFOHDQXSVWXII ` ` 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. )LOWHUVKDYHQRLGHD ZKR·VJRLQJWRFDOOWKHP RUZKR·VQH[WLQOLQH 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¼[TQNMKaKTM %VERYlLTERMUSTIMPLEMENTTHETHREEMETHODSIN THE&ILTERINTERFACEINITDO&ILTERANDDESTROY )LUVWWKHUH·VLQLW ,QWKHHQGWKHUH·VGHVWUR\ GR)LOWHU GRHVWKHKHDY\OLIWLQJ 7HENTHE#ONTAINERDECIDESTOINSTANTIATEA lLTERTHEINITMETHODISYOURCHANCETODO ANYSETUPTASKSBEFORETHElLTERISCALLED4HE MOSTCOMMONIMPLEMENTATIONWASSHOWNON THEPREVIOUSPAGESAVINGAREFERENCETOTHE &ILTER#ONlGOBJECTFORLATERUSEINTHElLTER 7HENTHE#ONTAINERDECIDESTOREMOVEAlLTER INSTANCEITCALLSTHEDESTROYMETHODGIVING YOUACHANCETODOANYCLEANUPYOUNEEDTODO BEFORETHEINSTANCEISDESTROYED 4HEDO&ILTERMETHODISCALLEDEVERYTIMETHE #ONTAINERDETERMINESTHATTHElLTERSHOULDBE APPLIEDTOTHECURRENTREQUEST4HEDO&ILTER METHODTAKESTHREEARGUMENTS !3ERVLET2EQUEST NOTAN(TTP3ERVLET2EQUEST !3ERVLET2ESPONSE NOTAN(TTP3ERVLET2ESPONSE !&ILTER#HAIN 4HEDO&ILTERMETHODISYOURCHANCETO IMPLEMENTYOURlLTERSFUNCTION)FYOURlLTER ISSUPPOSEDTOLOGUSERNAMESTOAlLEDOIT INDO&ILTER7ANTTOCOMPRESSTHERESPONSE OUTPUT$OITINDO&ILTER WKHUH DUH QR ' XPE 4XHVWLRQV 4 7HATISA&ILTER#HAIN $ !&ILTER#HAINISTHECOOLESTTHINGINALLOF &ILTERDOM&ILTERSAREDESIGNEDTOBEMODULAR BUILDINGBLOCKSYOUCANMIXTOGETHERINAVARIETY OFWAYSTOMAKEACOMBINATIONOFTHINGSHAPPEN ANDTHE&ILTER#HAINISABIGPARTOFWHATMAKESTHIS POSSIBLE)TSTHETHINGTHATKNOWSWHATCOMESNEXT7E ALREADYMENTIONEDTHATTHEFILTERSNOTTOMENTION THESERVLETSHOULDNTKNOWANYTHINGABOUTTHE OTHERFILTERSINVOLVEDINTHEREQUESTBUTSOMEONE NEEDSTOKNOWTHEORDERANDTHATSOMEONEISTHE &ILTER#HAINDRIVENBYTHEFILTERELEMENTSYOUSPECIFY INTHE$$ "YTHEWAY&ILTER#HAINISINTHESAMEPACKAGEAS &ILTERJAVAXSERVLET 4 )NOTICEDTHATINYOURDO&ILTERMETHOD YOUMADETHISCALLCHAINDO&ILTER7HATSA DO&ILTERDOINGINSIDEADO&ILTER9OURENOT GONNAGETALLRECURSIVEONUSAREYOU $ 4HE&ILTER#HAININTERFACESDO&ILTERISALITTLE BITDIFFERENTTHANTHE&ILTERINTERFACESDO&ILTER (ERESTHEMAINDIFFERENCE 4HEDO&ILTERMETHODOFTHE&ILTER#HAINTAKESCARE OFFIGURINGOUTWHOSEDO&ILTERMETHODTOINVOKE NEXTORIFITSTHEENDOFTHECHAINWHICHSERVLETS SERVICEMETHODBUTTHEDO&ILTERMETHODINA &ILTERACTUALLYDOESTHEFILTERINGTHETHINGTHEFILTER WASCREATEDTODO 4HISMEANSA&ILTER#HAINCANINVOKE%)4(%2AFILTEROR ASERVLETDEPENDINGONWHETHERITSTHEENDOFTHE CHAIN4HEENDOFTHECHAINISALWAYSEITHERASERVLET ORA*30WHICHMEANSA*30SGENERATEDSERVLETOF COURSEASSUMINGTHE#ONTAINERISABLETOMAPTHE REQUEST52,TOASERVLETOR*30)FTHE#ONTAINERCANT LOCATETHERIGHTRESOURCEFORTHEREQUESTTHEFILTERIS NEVERINVOKED ¿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. ¿OWHUVDQGZUDSSHUV \RXDUHKHUH 4HESERVLETSPECDOESNTDICTATEHOWTHECHAINDO&ILTERREQRESP METHODISHANDLEDINSIDETHECONTAINER)NPRACTICETHOUGHYOUCAN THINKOFTHEPROCESSOFlLTERSCHAININGTOEACHOTHERASIFTHEYWERE SIMPLYMETHODCALLSONASINGLESTACK7EKNOWTHERESMOREGOINGON BEHINDTHESCENESINTHE#ONTAINERBUTWEDONTCAREASLONGASWECAN PREDICTHOWOURlLTERSWILLRUNANDACONCEPTUALIFNOTPHYSICALSTACK LETSUSDOTHAT <PQVSWNNQT\MZ[I[JMQVO¹[\IKSIJTMº )KWVKMX\]ITKITT[\IKSM`IUXTM )NTHISEXAMPLEAREQUESTFOR3ERVLET!WILLBElLTEREDBYTWOlLTERS &ILTERTHEN&ILTER 6HUYOHW$ VHUYLFH 8SRQJHWWLQJ WKHUHTXHVW WKH&RQWDLQHU FDOOV)LOWHU·V GR)LOWHUPHWKRG ZKLFKUXQVXQWLO LWHQFRXQWHUVLWV FKDLQGR)LOWHU FDOO 7KH&RQWDLQHU SXVKHV)LOWHU·V GR)LOWHUPHWKRG RQWKHWRSRIWKH VWDFNZKHUHLW H[HFXWHVXQWLOLW UHDFKHVLWVFKDLQ GR)LOWHUFDOO 7KH&RQWDLQHU SXVKHV6HUYOHW$·V VHUYLFHPHWKRG RQWKHWRSRI WKHVWDFNZKHUH LWH[HFXWHVWR FRPSOHWLRQDQGLV WKHQSRSSHGRII WKHVWDFN 7KH&RQWDLQHU UHWXUQVFRQWUROWR )LOWHUZKHUHLWV GR)LOWHUPHWKRG FRPSOHWHVDQGLV WKHQSRSSHGRII 7KH&RQWDLQHU UHWXUQVFRQWUROWR )LOWHUZKHUHLWV GR)LOWHUPHWKRG FRPSOHWHVDQGLV SRSSHGRII7KHQ WKH&RQWDLQHU FRPSOHWHVWKH UHVSRQVH \PM[\IKS \PM[\IKS \PM[\IKS \PM[\IKS \PM[\IKS 7KLV´FRQFHSWXDOVWDFNµ LVMXVWDZD\WRWKLQN DERXWILOWHUFKDLQ LQYRFDWLRQV:HGRQ·W NQRZRUFDUHKRZ WKH&RQWDLQHUDFWXDOO\ LPSOHPHQWVWKLV³EXW WKLQNLQJRILWWKLVZD\ OHWV\RXSUHGLFWKRZ \RXUILOWHUFKDLQZLOO EHKDYH )LOWHU GR)LOWHUUUF )LOWHU GR)LOWHUUUF )LOWHU GR)LOWHUUUF )LOWHU GR)LOWHUUUF )LOWHU GR)LOWHUUUF )LOWHU GR)LOWHUUUF )LOWHU GR)LOWHUUUF )LOWHU GR)LOWHUUUF 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 ,MKTIZQVOIVLWZLMZQVONQT\MZ[ 7HENYOUCONlGURElLTERSINTHE$$YOULLUSUALLYDOTHREETHINGS $ECLAREYOURlLTER -APYOURlLTERTOTHEWEBRESOURCESYOUWANTTOlLTER !RRANGETHESEMAPPINGSTOCREATElLTERINVOCATIONSEQUENCES ILOWHU! ILOWHUQDPH!%HHU5HTXHVWILOWHUQDPH! ILOWHUFODVV!FRPH[DPSOHZHE%HHU5HTXHVW)LOWHU ILOWHUFODVV! LQLWSDUDP! SDUDPQDPH!/RJ)LOH1DPHSDUDPQDPH! SDUDPYDOXH!8VHU/RJW[WSDUDPYDOXH! LQLWSDUDP! ILOWHU! 'HFODULQJDÀOWHU 5XOHVIRUÀOWHU! 'HFODULQJDÀOWHUPDSSLQJWRD 85/SDWWHUQ ILOWHUPDSSLQJ! ILOWHUQDPH!%HHU5HTXHVWILOWHUQDPH! XUOSDWWHUQ!GRXUOSDWWHUQ! ILOWHUPDSSLQJ! ILOWHUPDSSLQJ! ¿OWHUQDPH!%HHU5HTXHVWILOWHUQDPH! VHUYOHWQDPH!$GYLFH6HUYOHWVHUYOHWQDPH! ILOWHUPDSSLQJ! 7KHILOWHUQDPH!LVPDQGDWRU\ 7KHILOWHUFODVV!LVPDQGDWRU\ 7KHLQLWSDUDP!LVRSWLRQDODQG \RXFDQKDYHPDQ\ 7KHILOWHUQDPH!LVPDQGDWRU\DQGLWLVXVHG WROLQNWRWKHFRUUHFWILOWHU!HOHPHQW (LWKHUWKHXUOSDWWHUQ!RUWKHWKH VHUYOHWQDPH!HOHPHQWLVPDQGDWRU\ 7KHXUOSDWWHUQ!HOHPHQWGHILQHVZKLFKZHE DSSUHVRXUFHVZLOOXVHWKLVILOWHU 7KHVHUYOHWQDPH!HOHPHQWGHILQHVZKLFK VLQJOHZHEDSSUHVRXUFHZLOOXVHWKLVILOWHU 5XOHVIRUÀOWHUPDSSLQJ! FRQ¿JXULQJ¿OWHUV 'HFODULQJDÀOWHUPDSSLQJWRD VHUYOHWQDPH ,03257$177KH&RQWDLQHU·VUXOHVIRURUGHULQJÀOWHUV :KHQPRUHWKDQRQHILOWHULVPDSSHGWRDJLYHQUHVRXUFHWKH&RQWDLQHUXVHVWKHIROORZLQJUXOHV $//ILOWHUVZLWKPDWFKLQJ85/SDWWHUQVDUHORFDWHGILUVW7KLVLV127WKHVDPHDVWKH85/PDSSLQJ UXOHVWKH&RQWDLQHUXVHVWRFKRRVHWKH´ZLQQHUµZKHQDFOLHQWPDNHVDUHTXHVWIRUDUHVRXUFHEHFDXVH $//ILOWHUVWKDWPDWFKZLOOEHSODFHGLQWKHFKDLQ)LOWHUVZLWKPDWFKLQJ85/SDWWHUQVDUHSODFHGLQWKH FKDLQLQWKHRUGHULQZKLFKWKH\DUHGHFODUHGLQWKH'' 2QFHDOOILOWHUVZLWKPDWFKLQJ85/VDUHSODFHGLQWKHFKDLQWKH&RQWDLQHUGRHVWKHVDPHWKLQJZLWK ILOWHUVWKDWKDYHDPDWFKLQJVHUYOHWQDPH!LQWKH'' 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. ¿OWHUVDQGZUDSSHUV \RXDUHKHUH 4HINKABOUTIT)TSGREATTHATlLTERSCANBEAPPLIEDTOREQUESTS THATCOMEDIRECTLYFROMTHECLIENT"UTWHATABOUTRESOURCES REQUESTEDFROMAFORWARDORINCLUDEREQUESTDISPATCHAND ORTHEERRORHANDLER3ERVLETSPECTOTHERESCUE ,VQ·W7+$7W\SLFDOWKH\JLYHXVDZD\ WRÀOWHUUHTXHVWVFRPLQJIURPD FOLHQW DQGWKH\MXVWIRUJHWDOODERXWUHTXHVWV WKDW:(JHQHUDWHWKURXJK IRUZDUGV DQG UHTXHVWGLVSDWFKHV *HH]WKH\WUHDW UHTXHVWGLVSDWFKLQJOLNHLW·VDVHFRQG FODVVLQYRFDWLRQWHFKQLTXH" 6M_[.TI[P")[WN^MZ[QWVNQT\MZ[ KIVJMIXXTQML\W ZMY]M[\LQ[XI\KPMZ[ ¿OWHUPDSSLQJ! ¿OWHUQDPH!0RQLWRU)LOWHU¿OWHUQDPH! XUOSDWWHUQ!GRXUOSDWWHUQ! GLVSDWFKHU!5(48(67GLVSDWFKHU! GLVSDWFKHU!,1&/8'(GLVSDWFKHU! GLVSDWFKHU!)25:$5'GLVSDWFKHU! GLVSDWFKHU!(5525GLVSDWFKHU! ¿OWHU! 'HFODULQJDÀOWHUPDSSLQJIRU UHTXHVWGLVSDWFKHGZHEUHVRXUFHV 'HFODUDWLRQ5XOHV 7KHILOWHUQDPH!LVPDQGDWRU\ (LWKHUWKHXUOSDWWHUQ!RUVHUYOHWQDPH! HOHPHQWLVPDQGDWRU\ <RXFDQKDYHIURPWRGLVSDWFKHU!HOHPHQWV $5(48(67YDOXHDFWLYDWHVWKHILOWHUIRUFOLHQW UHTXHVWV,IQRGLVSDWFKHU!HOHPHQWLVSUHVHQW 5(48(67LVWKHGHIDXOW $Q,1&/8'(YDOXHDFWLYDWHVWKHILOWHUIRUUHTXHVW GLVSDWFKLQJIURPDQLQFOXGHFDOO $)25:$5'YDOXHDFWLYDWHVWKHILOWHUIRUUHTXHVW GLVSDWFKLQJIURPDIRUZDUGFDOO $Q(5525YDOXHDFWLYDWHVWKHILOWHUIRUUHVRXUFHV FDOOHGE\WKHHUURUKDQGOHU DQGRU DQGRU DQGRU 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 %DVHGRQWKHIROORZLQJ''IUDJPHQWZULWHGRZQ WKHVHTXHQFHLQZKLFKWKH¿OWHUVZLOOEHH[HFXWHG IRUHDFKUHTXHVWSDWK$VVXPH)LOWHUWKURXJK )LOWHUKDYHEHHQSURSHUO\GHFODUHG$QVZHUV DUHDWWKHHQGRIWKLVFKDSWHU ILOWHUPDSSLQJ! ILOWHUQDPH!)LOWHUILOWHUQDPH! XUOSDWWHUQ!5HFLSHVXUOSDWWHUQ! ILOWHUPDSSLQJ! ILOWHUPDSSLQJ! ILOWHUQDPH!)LOWHUILOWHUQDPH! VHUYOHWQDPH!5HFLSHV+RSV/LVWGRVHUYOHWQDPH! ILOWHUPDSSLQJ! ILOWHUPDSSLQJ! ILOWHUQDPH!)LOWHUILOWHUQDPH! XUOSDWWHUQ!5HFLSHV$GGXUOSDWWHUQ! ILOWHUPDSSLQJ! ILOWHUPDSSLQJ! ¿OWHUQDPH!)LOWHUILOWHUQDPH! VHUYOHWQDPH!5HFLSHV0RGLI\0RG5HFLSHVGRVHUYOHWQDPH! ILOWHUPDSSLQJ! ILOWHUPDSSLQJ! ILOWHUQDPH!)LOWHUILOWHUQDPH! XUOSDWWHUQ!XUOSDWWHUQ! ILOWHUPDSSLQJ! 5HTXHVWSDWK)LOWHU6HTXHQFH 5HFLSHV+RSV5HSRUWGR 5HFLSHV+RSV/LVWGR 5HFLSHV0RGLI\0RG5HFLSHVGR +RSV/LVWGR 5HFLSHV$GG$GG5HFLSHVGR )LOWHUV )LOWHUV )LOWHUV )LOWHUV )LOWHUV 6KDUSHQ \RXU SHQFLO ¿OWHUFRQ¿JXUDWLRQH[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. ¿OWHUVDQGZUDSSHUV \RXDUHKHUH 0\ERVVOLNHGP\ÀUVWÀOWHUVR PXFKKHZDQWVPHWRZULWHDQRWKHU RQH7KHFRPSDQ\·VSLSHWRWKH ,QWHUQHWLVJHWWLQJUHDOO\EXV\VR QRZ KHZDQWVXVWR FRPSUHVV DOO RIRXUUHVSRQVHVWUHDPV 6HHPVOLNHDÀOWHU ZRXOGEHWKHZD\WRJR %XWVLQFHZH·UHGHDOLQJZLWK UHVSRQVHV,·OOKDYHWRSXWWKH FRPSUHVVLRQFRGH$)7(5WKH FKDLQGR)LOWHUFDOO +WUXZM[[QVOW]\X]\_Q\PIZM[XWV[M[QLMÅT\MZ %ARLIERWESHOWEDAVERYSIMPLEREQUESTlLTER"UTNOWWELLLOOKATARESPONSE lLTER2ESPONSElLTERSAREABITTRICKIERBUTTHEYCANBEINCREDIBLYUSEFUL4HEY LETUSDOSOMETHINGTOTHERESPONSEOUTPUTAFTERTHESERVLETDOESITSTHING BUTBEFORETHERESPONSEISSENTTOTHECLIENT3OINSTEADOFSTEPPINGINATTHE BEGINNINGBEFORETHESERVLETGETSTHEREQUESTWESTEPINATTHEENDAFTERTHE SERVLETGETSTHEREQUESTANDGENERATESARESPONSE 7ELLSORTOFTHINKABOUTIT&ILTERSAREALWAYSINVOKEDINTHECHAINBEFORETHE SERVLET4HERESNOSUCHTHINGASAlLTERTHATISINVOKEDONLYAFTERTHESERVLET "UTREMEMBERTHATSTACKPICTURE4HElLTERGETSANOTHERSHOTATTHISAFTERTHE SERVLETCOMPLETESITSWORKANDISPOPPEDOFFTHEVIRTUALSTACK 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\]ZMWNIZM[XWV[MNQT\MZ 2ACHELISTALKINGABOUTTHEBASICSTRUCTUREOFWHATYOUPUTIN ADO&ILTERMETHODlRSTYOUDOWORKRELATEDTOTHEREQUEST THENYOUCALLCHAINDO&ILTERTHENlNALLYWHENTHESERVLETAND ANYOTHERlLTERINTHECHAINAFTERYOURlLTERCOMPLETESAND CONTROLISRETURNEDTOYOURORIGINALDO&ILTERMETHODYOUCAN DOSOMETHINGTOTHERESPONSE FODVV0\&RPSUHVVLRQ)LOWHULPSOHPHQWV)LOWHU^ LQLW SXEOLFYRLGGR)LOWHUUHTXHVWUHVSRQVHFKDLQ^ WKLVLVZKHUHUHTXHVWKDQGOLQJZRXOGJR FKDLQGR)LOWHUUHTXHVWUHVSRQVH GRFRPSUHVVLRQORJLFKHUH ` GHVWUR\ ` 5DFKHO·VSVHXGRFRGHIRUWKHFRPSUHVVLRQÀ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)LOWHUUUF 0\&RPSUHVVLRQ)LOWHU GR)LOWHUUUF 0\&RPSUHVVLRQ)LOWHU GR)LOWHUUUF 7KH&RPSUHVVLRQ ÀOWHU·VGR)LOWHU PHWKRGUXQVDQG LQYRNHVFKDLQGR)LOWHU ,W·VWRRHDUO\WRGR DQ\FRPSUHVVLRQ³WKH JRDOLVWRFRPSUHVVWKH UHVSRQVHRXWSXWIURP WKHVHUYOHW 7KH6HUYOHW·VVHUYLFH PHWKRGJRHVRQWKH WRSRIWKHVWDFNGRHV VRPHZRUNJHQHUDWHV DUHVSRQVHRXWSXWDQG FRPSOHWHV 1RZWKDWWKH6HUYOHW·V VHUYLFHPHWKRG KDVSRSSHGRIIWKH VWDFNWKHUHVWRIWKH FRPSUHVVLRQÀOWHU·V GR)LOWHUPHWKRGFDQ UXQDQGLWKRSHVGRWKH FRPSUHVVLRQRQZKDWHYHU WKHVHUYOHWZURWHWRWKH UHVSRQVHRXWSXW 7KHFRQFHSWXDOFDOOVWDFN DUHVSRQVH¿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. ¿OWHUVDQGZUDSSHUV \RXDUHKHUH &RQWDLQHU )LOWHU 7KHILOWHUSDVVHVWKHUHTXHVW DQGUHVSRQVHWRWKHVHUYOHWDQG ZDLWVSDWLHQWO\IRULWVFKDQFHWR FRPSUHVVVWXII D 7KHVHUYOHWGRHVLWVWKLQJ FUHDWLQJRXWSXWEOLVVIXOO\XQDZDUH WKDWWKLVYHU\VDPHRXWSXWZDV VXSSRVHGWREHFRPSUHVVHG E 7KHRXWSXWJRHVEDFN WKURXJKWKH&RQWDLQHUDQG F ,W·VVHQWEDFNWRWKHFOLHQW +PPPWKLVFRXOGEHDSUREOHP 7KHILOWHUZDVKRSLQJWRKDYHD FKDQFHWRGRVRPHWKLQJWRWKH RXWSXWFRPSUHVVLWEHIRUHWKH RXWSXWZHQWWRWKHFOLHQW 7KHFDOOWRFKDLQGR)LOWHUKDV UHWXUQHGDQGWKHILOWHUZDVKRSLQJ WRJUDEWKHRXWSXWDQGDQGVWDUW FRPSUHVVLQJ (;&(37LW·VWRRODWH7KHRXWSXWZDV DOUHDG\VHQWWRWKHFOLHQW7KH&RQWDLQHU GRHVQ·WEXIIHUWKHRXWSXWIRUWKHILOWHU %\WKHWLPHWKHILOWHU·VRZQGR)LOWHU PHWKRGLVDWWKHWRSRIWKHFRQFHSWXDO VWDFNLW·VWRRODWHIRUWKHILOWHUWR DIIHFWWKHRXWSXW 2XWSXW 6WUHDP 5HTXHVWDQG 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' $OESCOMPRESSINGTHERESPONSEREALLYINVOLVENOTHINGMORETHAN WAITINGFORTHESERVLETTOlNISHTHENCOMPRESSINGTHESERVLETS RESPONSEOUTPUT!FTERALLTHElLTERSDO&ILTERMETHODHASA REFERENCETOTHESAMERESPONSEOBJECTTHATWENTTOTHESERVLETSOIN THEORYTHElLTERSHOULDHAVEACCESSTOTHERESPONSEOUTPUT SXEOLFYRLGGR)LOWHUUHTXHVWUHVSRQVHFKDLQ^ WKLVLVZKHUHUHTXHVWKDQGOLQJZRXOGJR FKDLQGR)LOWHUUHTXHVWUHVSRQVH GRFRPSUHVVLRQORJLFKHUH ` 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. )LOWHUVKDYHQRLGHD ZKR·VJRLQJWRFDOOWKHP RUZKR·VQH[WLQOLQH 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. ¿OWHUVDQGZUDSSHUV RXDUHKHUH 0ERVVOLNHGPÀUVWÀOWHUVR PXFKKHZDQWVPHWRZULWHDQRWKHU RQH7KHFRPSDQ·VSLSHWRWKH ,QWHUQHWLVJHWWLQJUHDOOEXVVR QRZ KHZDQWVXVWR FRPSUHVV DOO RIRXUUHVSRQVHVWUHDPV 6HHPVOLNHDÀOWHU ZRXOGEHWKHZDWRJR %XWVLQFHZH·UHGHDOLQJZLWK UHVSRQVHV,·OOKDYHWRSXWWKH FRPSUHVVLRQFRGH$)7(5WKH FKDLQGR)LOWHUFDOO +WUXZM[[QVOW]X]_QPIZM[XWV[M[QLMÅTMZ %ARLIERWESHOWEDAVERYSIMPLEREQUESTlLTER"UTNOWWELLLOOKATARESPONSE lLTER2ESPONSElLTERSAREABITTRICKIERBUTTHEYCANBEINCREDIBLYUSEFUL4HEY LETUSDOSOMETHINGTOTHERESPONSEOUTPUTAFTERTHESERVLETDOESITSTHING BUTBEFORETHERESPONSEISSENTTOTHECLIENT3OINSTEADOFSTEPPINGINATTHE BEGINNINGBEFORETHESERVLETGETSTHEREQUESTWESTEPINATTHEENDAFTERTHE SERVLETGETSTHEREQUEST AND GENERATESARESPONSE 7ELLSORTOFTHINKABOUTIT&ILTERSAREALWAYSINVOKEDINTHECHAINBEFORETHE SERVLET4HERESNOSUCHTHINGASAlLTERTHATISINVOKEDONLYAFTERTHESERVLET "UTREMEMBERTHATSTACKPICTURE4HElLTERGETSANOTHERSHOTATTHISAFTERTHE SERVLETCOMPLETESITSWORK AND ISPOPPEDOFFTHEVIRTUALSTACK Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates, Kathy Sierra,. prohibited. FKDSWHU 7KLVZRQ·WZRUN,FDQ·W FRPSUHVVVRPHWKLQJRQWKHZD RXW RIWKHVHUYOHWEHFDXVHLW·VWRRODWH 7KHRXWSXWJRHVVWUDLJKWIURPWKHVHUYOHW EDFNWRWKHFOLHQW%XWWKHZKROHSRLQWLV WRFRPSUHVVWKHRXWSXWVRKRZFDQ, JHWFRQWURORIWKHRXWSXW%()25(LW JRHVWRWKHFOLHQW" <PMW]X]PI[TMNPMJ]QTLQVO 7KLQNDERXWWKLVIRUDPLQXWHWKHVHUYOHW DFWXDOOJHWVWKHRXWSXWVWUHDPRUZULWHU IURPWKHUHVSRQVHREMHFW:KDWLILQVWHDG RISDVVLQJWKH5($/UHVSRQVHREMHFWWRWKHVHUYOHW RXUÀOWHUVZDSSHGLQD FXVWRP UHVSRQVHREMHFW ZLWKDQRXWSXWVWUHDPWKDW RX FRQWURO"1RERG VDLGWKHÀOWHUKDVWRSDVVWKH5($/UHVSRQVH ZKHQLWFDOOVFKDLQGR)LOWHU FKDLQGR)LOWHU &RQWDLQHU GR)LOWHU Original request and response A DIFFERENT response )LOWHU 6HUYOHW ¿OWHULQJWKH RXWSXW Chapter 13. The Power of Filters Chapter 13. The Power of Filters Head First Servlets and JSP By Bert Bates,