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,13 MB
Nội dung
FKDSWHU %XWKRZGR\RXORRSRYHUWKH ERG\",WORRNVOLNHGR6WDUW7DJLV FDOOHGWRRHDUO\DQGGR(QG7DJLVWRR ODWHDQG,GRQ·WKDYHDQ\ZD\WRNHHS UH LQYRNLQJWKHERG\HYDOXDWLRQ SDFNDJHDQGLPSRUWV SXEOLFFODVV&ODVVLF7HVWH[WHQGV7DJ6XSSRUW^ SXEOLFLQWGR6WDUW7DJWKURZV-VS([FHSWLRQ^ UHWXUQ (9$/B%2'<B,1&/8'( ` SXEOLFLQWGR(QG7DJWKURZV-VS([FHSWLRQ^ UHWXUQ(9$/B3$*( ` ` SDFNDJHDQGLPSRUWV SXEOLFFODVV6LPSOH7DJ7HVWH[WHQGV6LPSOH7DJ6XSSRUW^ SXEOLFYRLGGR7DJWKURZV-VS([FHSWLRQ,2([FHSWLRQ^ IRULQWL LL^ JHW-VS%RG\LQYRNHQXOO ` ` ` &ODVVLFWDJ 6LPSOHWDJ But where do you loop over the body, if the body is evaluated in between the methods instead of IN a method like doTag()? ItÕs easy to loop the body of a Simple tag; you just keep calling invoke() on the body, from within doTag(). LWHUDWLQJZLWK&ODVVLFWDJV" Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough 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. FXVWRPWDJGHYHORSPHQW \RXDUHKHUH 3IMPLETAGSARESIMPLEITSALLABOUTDO4AG"UTWITHCLASSICTAGS THERESADO3TART4AGANDADO%ND4AG!NDTHATBRINGSUPAN INTERESTINGPROBLEMWHENANDHOWISTHEBODYEVALUATED4HERES NODO"ODYMETHODBUTTHEREISADO!FTER"ODYMETHODTHATS CALLEDAFTERTHEBODYISEVALUATEDANDBEFORETHEDO%ND4AGRUNS +TI[[QK\IO[PI^MILQNNMZMV\TQNMKaKTM &RQWDLQHU :HE&RQWDLQHU &ODVVLFWDJFODVV 7DJ+DQGOHU2EMHFW These happen the Þ rst time the tag is invoked, but the Container may (depending on the circumstances) reuse the Classic tag object after this. This gives the handler a reference to a PageContext. If the tag is invoked with attributes, the JavaBean-style setter for each attribute is called (just as with SimpleTag handlers). A nested tag can communicate with the other tags in which its nested. doAfterBody() lets you do things AFTER the body runs, and unlike the other methods it can be invoked more than once. The body is evaluated between the doStartTag() and doEndTag() methods. &ODVVLF+DQGOHUFODVV /RDGFODVV ,QVWDQWLDWHFODVVQRDUJFRQVWUXFWRUUXQV &DOOWKHVHW3DJH&RQWH[W3DJH&RQWH[WPHWKRG ,IWKHWDJKDVDWWULEXWHVFDOODWWULEXWHVHWWHUV &DOOWKHGR6WDUW7DJPHWKRG &DOOWKHGR(QG7DJPHWKRG ,IWKHWDJLVQHVWHGLQYRNHGIURPZLWKLQDQRWKHU WDJFDOOWKHVHW3DUHQW7DJPHWKRG ,IWKHWDJLV127GHFODUHGWRKDYHDQHPSW\ ERG\$1'WKHWDJLV127LQYRNHGZLWKDQHPSW\ ERG\$1'WKHGR6WDUW7DJPHWKRGUHWXUQV (9$/B%2'<B,1&/8'(WKHERG\LVHYDOXDWHG ,IWKHERG\FRQWHQWZDVHYDOXDWHGFDOOWKH GR$IWHU%RG\PHWKRG doEndTag() is always called once, either after doStartTag() or after doAfterBody(). Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough 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 4HEDO3TART4AGANDDO%ND4AGMETHODSRETURNANINT4HATINTTELLSTHE #ONTAINERWHATTODONEXT7ITHDO3TART4AGTHEQUESTIONTHE#ONTAINER ASKSISh3HOULD)EVALUATETHEBODYvASSUMINGTHEREISONEANDASSUMING THE4,$DOESNTDECLARETHEBODYASEMPTY 7ITHDO%ND4AGTHE#ONTAINERASKSh3HOULD)KEEPEVALUATINGTHERESTOF THECALLINGPAGEv4HERETURNVALUESAREREPRESENTEDBYCONSTANTSDECLARED INTHE4AGAND)TERATION4AGINTERFACES <PM+TI[[QKTQNMKaKTMLMXMVL[WVZM\]ZV^IT]M[ GR6WDUW7DJ 6.,3B%2'< (9$/B%2'<B,1&/8'( GR(QG7DJ 6.,3B3$*( (9$/B3$*( 3RVVLEOHUHWXUQYDOXHVZKHQ \RXH[WHQG7DJ6XSSRUW GR$IWHU%RG\ 6.,3B%2'< (9$/B%2'<B$*$,1 GR$IWHU%RG\ GR6WDUW7DJ (YDOXDWH%2'< GR(QG7DJ (YDOXDWH3$*( UHWXUQ(9$/B%2'<B,1&/8'( UHWXUQ6.,3B%2'< UHWXUQ6.,3B%2'< UHWXUQ(9$/B%2'<B$*$,1 UHWXUQ(9$/B3$*( 'RQH UHWXUQ6.,3B3$*( This is the only return value constant declared in IterationTag (the others are all from Tag). :LWKGR6WDUW7DJWKHUHWXUQYDOXHVDUH 6.,3B%2'<DQG(9$/B%2'<B,1&/8'(%XWZLWK GR(QG7DJWKHYDOXHVDUH6.,3B3$*(DQG (9$/B3$*( ,IWKHQDPHVZHUHFRQVLVWHQWGR(QG7DJZRXOGUHWXUQ (9$/B3$*(B,1&/8'(DVRSSRVHGWR(9$/B3$*( WRPDWFKWKHZD\GR6WDUW7DJUHWXUQV(9$/B%2'<B ,1&/8'(%XWLW¶VQRW6RGRQ¶WEHIRROHGLI\RXVHHFRGH RQWKHH[DPZLWKFRUUHFWORRNLQJEXWZURQJUHWXUQYDOXHV 7KHFRQVWDQWVXVHGDVUHWXUQ YDOXHVIRUGR6WDUW7DJDQG GR(QG7DJUHWXUQYDOXHFRQVWDQWV DUHLQFRQVLVWHQWO\QDPHG Returning SKIP_PAGE from doEndTag() is exactly like throwing a SkipPageException from a Simple tag! If a page included the page that invoked the tag, the current (included) page stops processing, but the including page continues &ODVVLFWDJOLIHF\FOH Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough 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. FXVWRPWDJGHYHORSPHQW \RXDUHKHUH 7HENYOUWRITEATAGHANDLERTHATEXTENDS4AG3UPPORTYOUGETALLTHELIFECYCLEMETHODSFROMTHE 4AGINTERFACEPLUSTHEONEMETHODFROM)TERATION4AGDO!FTER"ODY7ITHOUTDO!FTER"ODY YOUCANTITERATEOVERTHEBODYBECAUSEDO3TART4AGISTOOEARLYANDDO%ND4AGISTOOLATE "UTWITHDO!FTER"ODYYOURRETURNVALUETELLSTHE#ONTAINERWHETHERITSHOULDREPEATTHEBODY AGAIN%6!,?"/$9?!'!).ORCALLTHEDO%ND4AGMETHOD3+)0?"/$9 1 \ M Z I \ Q W V < I O T M \ [ a W ]ZMXMI\\PMJWLa 7DJLQWHUIDFH LQWGR(QG7DJ 7DJJHW3DUHQW LQWGR6WDUW7DJ YRLGVHW3DJH&RQWH[W3DJH&RQWH[W YRLGVHW3DUHQW7DJ YRLGUHOHDVH LQWHUIDFH!! 7DJ ,WHUDWLRQ7DJLQWHUIDFH LQWGR$IWHU%RG\ LQWHUIDFH!! ,WHUDWLRQ7DJ YRLGGR$IWHU%RG\ LQWGR6WDUW7DJ LQWGR(QG7DJ YRLGVHW3DJH&RQWH[W3DJH&RQWH[W PRUHPHWKRGV 7DJ6XSSRUW 7DJ6XSSRUWFODVV 6KDUSHQ \RXU SHQFLO SXEOLFYRLGGR7DJWKURZV-VS([FHSWLRQ,2([FHSWLRQ^ 6WULQJ>@PRYLHV ^³6SLGHUPDQ´³6DYHG´³$PHOLH´` IRULQWL LPRYLHVOHQJWKL^ JHW-VS&RQWH[WVHW$WWULEXWH³PRYLH´PRYLHV>L@ JHW-VS%RG\LQYRNHQXOO ` ` 7U\WRPSOHPHQWWKHVDPHIXQFWLRQDOLW\RIWKLV 6LPSOH7DJGR7DJLQD&ODVVLFWDJKDQGOHU$VVXPH WKH7/'LVFRQILJXUHGWRDOORZERG\FRQWHQW // package and imports public class MyIteratorTag extends TagSupport { public int doStartTag() throws JspException { public int doAfterBody() throws JspException { public int doEndTag() throws JspException { } Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough 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 /RRNDWWKHOHJDOWDJKDQGOHUFRGHEHORZDQG ILJXUHRXWZKHWKHULWZRXOGJLYH\RXWKHUHVXOW VKRZQJLYHQWKH-63WDJLQYRFDWLRQ OLVWHGEHORZ7KLVLVDOVRWKHVDPH UHVXOWSURGXFHGE\WKH&ODVVLF7DJ KDQGOHUIURPWKHSUHYLRXVSDJH<HV ZH·UHDQVZHULQJWKH6KDUSHQ<RXU 3HQFLOZLWK\HWDQRWKHUH[HUFLVH %(WKH&RQWDLQHU SDFNDJHDQGLPSRUWV SXEOLFFODVV0\,WHUDWRU7DJH[WHQGV7DJ6XSSRUW^ 6WULQJ>@PRYLHV QHZ6WULQJ>@^³6SLGHUPDQ´³6DYHG´³$PHOLH´` LQWPRYLH&RXQWHU SXEOLFLQWGR6WDUW7DJWKURZV-VS([FHSWLRQ^ PRYLH&RXQWHU UHWXUQ(9$/B%2'<B,1&/8'( ` SXEOLFLQWGR$IWHU%RG\WKURZV-VS([FHSWLRQ^ LIPRYLH&RXQWHUPRYLHVOHQJWK^ SDJH&RQWH[WVHW$WWULEXWH³PRYLH´PRYLHV>PRYLH&RXQWHU@ PRYLH&RXQWHU UHWXUQ(9$/B%2'<B$*$,1 `HOVH^ UHWXUQ6.,3B%2'< ` ` SXEOLFLQWGR(QG7DJWKURZV-VS([FHSWLRQ^ UHWXUQ(9$/B3$*( ` ` 7KHWDJKDQGOHUFODVV :HWDNHWKHVWLQJRXWRI62$3 KWWSORFDOKRVWWHVWV3DJH$MVS &RQWDFWXVDWOLNHZHFDUH#ZLFNHGO\VPDUWFRP 6SLGHUPDQ 6DYHG $PHOLH 'HVLUHGUHVXOW-63WKDWLQYRNHVWKHWDJ #WDJOLESUH¿[ ´PLQH´XUL ´.DWK\&ODVVLF7DJV´! KWPO!ERG\! WDEOHERUGHU ´´! PLQHLWHUDWH0RYLHV! WU!WG!^PRYLH`WG!WU! PLQHLWHUDWH0RYLHV! WDEOH! ERG\!KWPO! &ODVVLFWDJ H[HUFLVH Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough 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. FXVWRPWDJGHYHORSPHQW \RXDUHKHUH )FYOUDONTOVERRIDETHE4AG3UPPORTLIFECYCLEMETHODSTHATRETURN ANINTEGERBEAWAREOFTHEDEFAULTVALUESTHE4AG3UPPORTMETHOD IMPLEMENTATIONSRETURN4HE4AG3UPPORTCLASSASSUMESTHATYOURTAG DOESNTHAVEABODYBYRETURNING3+)0?"/$9FROMDO3TART4AG ANDTHATIFYOU$/HAVEABODYTHATSEVALUATEDYOUWANTIT EVALUATEDONLYONCEBYRETURNING3+)0?"/$9FROMDO!FTER"ODY )TALSOASSUMESTHATYOUWANTTHERESTOFTHEPAGETOEVALUATEBY RETURNING%6!,0!'%FROMDO%NDTAG ,MNI]T\ZM\]ZV^IT]M[NZWU<IO;]XXWZ\ GR6WDUW7DJ 6.,3B%2'< (9$/B%2'<B,1&/8'( GR(QG7DJ 6.,3B3$*( (9$/B3$*( 'HIDXOWUHWXUQYDOXHVZKHQ\RXGRQ·WRYHUULGHWKH 7DJ6XSSRUWPHWKRGLPSOHPHQWDWLRQ GR$IWHU%RG\ 6.,3B%2'< (9$/B%2'<B$*$,1 7KH7DJ6XSSRUWFODVV DVVXPHV\RXUWDJGRHVQ·W KDYHDERG\RUWKDWLIWKH ERG\,6HYDOXDWHGWKDWWKH ERG\VKRXOGEHHYDOXDWHG RQO\21&( ,WDOVRDVVXPHVWKDW\RX DOZD\VZDQWWKHUHVWRIWKH SDJHWREHHYDOXDWHG <RXUHDOO\PXVWNQRZWKLVOLIHF\FOHIRUWKH H[DP'RQ¶WIRUJHWWKDWGR6WDUW7DJDQG GR(QG7DJDUHDOZD\VFDOOHGDQGWKH\¶UH FDOOHGRQO\RQFHUHJDUGOHVVRIDQ\WKLQJ HOVHWKDWKDSSHQV%XWGR$IWHU%RG\ FDQUXQIURPWRPDQ\WLPHVGHSHQGLQJ RQWKHUHWXUQYDOXHRIGR6WDUW7DJDQG SUHYLRXVGR$IWHU%RG\FDOOV GR6WDUW7DJDQG GR(QG7DJUXQ H[DFWO\RQFH 7KLQNDERXWLW7KHGHIDXOWUHWXUQYDOXHIURP GR6WDUW7DJLV6.,3B%2'<VRLI\RXZDQWWKHERG\ RI\RXUWDJHYDOXDWHGDQG\RXH[WHQG7DJ6XSSRUW \RX0867RYHUULGHGR6WDUW7DJLIIRUQRRWKHUUHDVRQ WKDQWRUHWXUQ(9$/B%2'<B,1&/8'( :LWKGR$IWHU%RG\LWVKRXOGEHREYLRXVWKDWLI\RX ZDQWWRLWHUDWHRYHUWKHERG\\RXKDYHWRRYHUULGHWKDW PHWKRGDVZHOOVLQFHLWVUHWXUQYDOXHLV6.,3B%2'< <RX0867RYHUULGHGR6WDUW7DJ LI\RXZDQWWKHWDJERG\WREH HYDOXDWHG Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough 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 %(WKH&RQWDLQHU$QVZHU SXEOLFFODVV0\,WHUDWRU7DJH[WHQGV7DJ6XSSRUW^ 6WULQJ>@PRYLHV QHZ6WULQJ>@^³6SLGHUPDQ´³6DYHG´³$PHOLH´` LQWPRYLH&RXQWHU SXEOLFLQWGR6WDUW7DJWKURZV-VS([FHSWLRQ^ PRYLH&RXQWHU SDJH&RQWH[WVHW$WWULEXWH³PRYLH´PRYLHV>PRYLH&RXQWHU@ PRYLH&RXQWHU UHWXUQ(9$/B%2'<B,1&/8'( ` SXEOLFLQWGR$IWHU%RG\WKURZV-VS([FHSWLRQ^ LIPRYLH&RXQWHUPRYLHVOHQJWK^ SDJH&RQWH[WVHW$WWULEXWH³PRYLH´PRYLHV>PRYLH&RXQWHU@ PRYLH&RXQWHU UHWXUQ(9$/B%2'<B$*$,1 `HOVH^ UHWXUQ6.,3B%2'< ` ` SXEOLFLQWGR(QG7DJWKURZV-VS([FHSWLRQ^ UHWXUQ(9$/B3$*( ` ` 7KHWDJKDQGOHUFODVV :HWDNHWKHVWLQJRXWRI62$3 KWWSORFDOKRVWWHVWV3DJH$MVS &RQWDFWXVDWOLNHZHFDUH#ZLFNHGO\VPDUWFRP 6SLGHUPDQ 6DYHG $PHOLH 'HVLUHGUHVXOW -63WKDWLQYRNHVWKHWDJ #WDJOLESUH¿[ ´PLQH´XUL ´.DWK\&ODVVLF7DJV´! KWPO!ERG\! WDEOHERUGHU ´´! PLQHLWHUDWH0RYLHV! WU!WG!^PRYLH`WG!WU! PLQHLWHUDWH0RYLHV! WDEOH! ERG\!KWPO! $FWXDOUHVXOWXQOHVV\RXDGGWKH WZROLQHVKLJKOLJKWHGEHORZ :HWDNHWKHVWLQJRXWRI62$3 KWWSORFDOKRVWWHVWV3DJH$MVS &RQWDFWXVDWOLNHZHFDUH#ZLFNHGO\VPDUWFRP 6SLGHUPDQ 6DYHG $PHOLH ThereÕs an empty cell at the top! You MUST add these two lines to produce the correct response. This doAfterBody() method was correct, but it runs only AFTER the body has already been processed once! Without the two extra lines in doStartTag(), the body is processed once without there being a movie attribute, so you get the empty cell. &ODVVLFWDJH[HUFLVHDQVZHUV Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough 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. FXVWRPWDJGHYHORSPHQW \RXDUHKHUH WKHUH DUH QR ' XPE 4XHVWLRQV 4 4HISSEEMSSTUPIDTHERES DUPLICATECODEINDO3TART4AGAND DO!FTER"ODY $ 9ESTHERESDUPLICATECODE )NTHISCASEIFYOUREIMPLEMENTING 4AG3UPPORTANDYOUWANTTOSET VALUESTHEBODYCANUSETHENYOU -534SETTHOSEATTRIBUTEVALUESIN DO3TART4AG9OUCANTWAITUNTIL DO!FTER"ODYBECAUSEBYTHETIME YOUGETTODO!FTER"ODYTHEBODY HASALREADYBEENPROCESSEDONCE 9ESITSKINDOFSTUPID7HICHISWHY 3IMPLE4AGISSOMUCHBETTER/F COURSEIFYOUWEREWRITINGTHECODE YOUDMAKEAPRIVATEMETHODINYOUR TAGHANDLERSAYSET-OVIEAND YOUDCALLTHATMETHODFROMBOTH DO3TART4AGANDDO!FTER"ODY"UT ITSSTILLANAWKWARDAPPROACH 4 7(9AREYOUSETTING THEINSTANCEVARIABLEVALUE FORMOVIE#OUNTER).3)$%THE DO3TART4AGMETHOD7HYCANTYOU JUSTINITIALIZEITWHENYOUDECLAREIT $ 9IKES5NLIKE3IMPLE4AG HANDLERSWHICHARENEVERREUSEDA #LASSICTAGHANDLERCANBEPOOLED ANDREUSEDBYTHE#ONTAINER4HAT MEANSYOUDBETTERRESETYOUR INSTANCEVARIABLEVALUESWITHEACH NEWTAGINVOCATIONWHICHMEANSIN DO3TART4AG/THERWISETHISCODE WORKSTHEFIRSTTIMEBUTTHENEXTTIME A*30INVOKESITTHEMOVIE#OUNTER VARIABLEWILLSTILLHAVEITSLASTVALUE INSTEADOF :DWFKRXW²WKLVLVFRPSOHWHO\GLIIHUHQW IURP6LPSOH7DJKDQGOHUVZKLFKDUH GH¿QLWHO\127UHXVHG7KDWPHDQV\RX KDYHWREHYHU\FDUHIXODERXWLQVWDQFH YDULDEOHV²\RXVKRXOGUHVHWWKHPLQ GR6WDUW7DJ 7KH7DJLQWHUIDFHGRHVKDYHDUHOHDVH PHWKRGEXWWKDW¶VFDOOHGRQO\ZKHQ WKHWDJKDQGOHULQVWDQFHLVDERXWWREH UHPRYHGE\WKH&RQWDLQHU6RGRQ¶W DVVXPHWKDWUHOHDVHLVDZD\WRUHVHW WKHWDJKDQGOHU¶VVWDWHLQEHWZHHQWDJ LQYRFDWLRQV 7KH&RQWDLQHUFDQ UHXVH&ODVVLFWDJ KDQGOHUV Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough 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 9OULLPROBABLYlNDTHATMOSTOFTHETIMETHELIFECYCLEMETHODSFROMTHE4AGAND )TERATION4AGINTERFACESASPROVIDEDBY4AG3UPPORTAREENOUGH"ETWEENTHETHREEKEY METHODSDO3TART4AGDO!FTER"ODYANDDO%ND4AGYOUCANDOJUSTABOUTANYTHING %XCEPTYOUDONTHAVEDIRECTACCESSTOTHECONTENTSOFTHEBODY)FYOUNEEDACCESSTOTHE ACTUALBODYCONTENTSSOTHATYOUCANSAYUSEITINANEXPRESSIONORPERHAPSlLTERORALTERIT INSOMEWAYTHENEXTEND"ODY4AG3UPPORTINSTEADOF4AG3UPPORTANDYOULLHAVEACCESSTO THE"ODY4AGINTERFACEMETHODS *]\_PI\QNaW],7VMMLIKKM[[\W\PMJWLaKWV\MV\[' LQWGR(QG7DJ 7DJJHW3DUHQW LQWGR6WDUW7DJ YRLGVHW3DJH&RQWH[W3DJH&RQWH[W YRLGVHW3DUHQW7DJ YRLGUHOHDVH LQWHUIDFH!! 7DJ ,WHUDWLRQ7DJLQWHUIDFH LQWGR$IWHU%RG\ LQWHUIDFH!! ,WHUDWLRQ7DJ %RG\7DJLQWHUIDFH YRLGGR,QLW%RG\ YRLGVHW%RG\&RQWHQW%RG\&RQWHQW LQWHUIDFH!! %RG\7DJ YRLGGR$IWHU%RG\ LQWGR6WDUW7DJ LQWGR(QG7DJ YRLGVHW3DJH&RQWH[W3DJH&RQWH[W PRUHPHWKRGV 7DJ6XSSRUW 7DJ6XSSRUWFODVV LQWGR6WDUW7DJ %RG\&RQWHQWJHW%RG\&RQWHQW YRLGGR,QLW%RG\ YRLGVHW%RG\&RQWHQW%RG\&RQWHQW PRUHPHWKRGV %RG\7DJ6XSSRUW %RG\7DJ6XSSRUWFODVV ([WHQGLQJ%RG\7DJ6XSSRUW JLYHV\RXWZRPRUHOLIHF\FOH PHWKRGVIURPWKH%RG\7DJ LQWHUIDFH³VHW%RG\&RQWHQW DQGGR,QLW%RG\<RXFDQ XVHWKHVHWRGRVRPHWKLQJ ZLWKWKHDFWXDO&217(176 RIWKHERG\RIWKHWDJXVHG WRLQYRNHWKHKDQGOHU %RG\7DJLQWHUIDFH Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough 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. FXVWRPWDJGHYHORSPHQW \RXDUHKHUH 7HENYOUIMPLEMENT"ODY4AGBYEXTENDING"ODY4AG3UPPORTYOUGET TWOMORELIFECYCLEMETHODSSET"ODY#ONTENTANDDO)NIT"ODY9OUALSO GETONENEWRETURNVALUEFORDO3TART4AG%6!,?"/$9?"5&&%2%$4HAT MEANSTHEREARENOWTHREEPOSSIBLERETURNVALUESFORDO3TART4AGINSTEAD OFTHETWOYOUGETWHENYOUEXTEND4AG3UPPORT ?Q\P*WLa<IOaW]OM\\_WVM_UM\PWL[ G R$IWHU%RG \ G R6WDUW7DJ (YDOXDWH%2'< GR(QG7DJ (YDOXDWH3$*( UHWXUQ(9$/B%2'<B,1&/8'( 2 UHWXUQ6.,3B%2'< XUQ6 UHWXUQ6.,3B%2'< , UHWXUQ(9$/B%2'<B$*$,1 9$ UHWXUQ(9$/B3$*( $ 'RQH UHWXUQ6.,3B3$*( UQ6 VHW%RG\&RQWHQW GR,QLW%RG\ UHWXUQ (9$/B%2'<B%8))(5(' /LIHF\FOHIRUDWDJWKDWLPSOHPHQWV%RG\7DJ GLUHFWO\RUE\H[WHQGLQJ%RG\7DJ6XSSRUW GR6WDUW7DJ 6.,3B%2'< (9$/B%2'<B,1&/8'( (9$/B%2'<B%8))(5(' $FKDQJHLQWKHGHIDXOWUHWXUQ YDOXHIRU%RG\7DJ6XSSRUW New return value, and itÕs the default for BodyTagSupport. (Instead of SKIP_BODY, the default for TagSupport.) Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough 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. [...]...Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/ 1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your... 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 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan... strictly prohibited Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/ 1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your... 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 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan... strictly prohibited Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/ 1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your... 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 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan... strictly prohibited Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/ 1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your... 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 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan... strictly prohibited Chapter 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher:Prepared for Augusto Jaramillo Forcada, Safari ID: augustojf.cv@gmail.com O'Reilly Print Publication Date: 8/ 1/2004 User number: 729515 Copyright 2007, Safari Books Online, LLC This PDF is exclusively for your... 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 10 When even JSTL is not enough Chapter 10 When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan . prohibited. FXVWRPWDJGHYHORSPHQW RXDUHKHUH )FYOUDONTOVERRIDETHE4AG3UPPORTLIFECYCLEMETHODSTHATRETURN ANINTEGERBEAWAREOFTHEDEFAULTVALUESTHE4AG3UPPORTMETHOD IMPLEMENTATIONSRETURN4HE4AG3UPPORTCLASSASSUMESTHATYOURTAG DOESNTHAVEABODYBYRETURNING3+)0?"/$9FROMDO3TART4AG AND THATIFYOU$/HAVEABODYTHATSEVALUATEDYOUWANTIT EVALUATEDONLYONCEBYRETURNING3+)0?"/$9FROMDO!FTER"ODY )TALSOASSUMESTHATYOUWANTTHERESTOFTHEPAGETOEVALUATEBY RETURNING%6!,0!'%FROMDO%NDTAG ,MNI]TZM]ZV^IT]M[NZWU<IO;]XXWZ GR6WDUW7DJ 6.,3B%2'< (9$/B%2'<B,1& /8& apos;( GR(QG7DJ 6.,3B3$*( (9$/B3$*( 'HIDXOWUHWXUQYDOXHVZKHQRXGRQ·WRYHUULGHWKH 7DJ6XSSRUWPHWKRGLPSOHPHQWDWLRQ GR$IWHU%RG 6.,3B%2'< (9$/B%2'<B$*$,1 7KH7DJ6XSSRUWFODVV DVVXPHVRXUWDJGRHVQ·W KDYHDERGRUWKDWLIWKH ERG,6HYDOXDWHGWKDWWKH ERGVKRXOGEHHYDOXDWHG RQO21&( ,WDOVRDVVXPHVWKDWRX DOZDVZDQWWKHUHVWRIWKH SDJHWREHHYDOXDWHG <RXUHDOOPXVWNQRZWKLVOLIHFFOHIRUWKH H[DP'RQ¶WIRUJHWWKDWGR6WDUW7DJDQG GR(QG7DJDUHDOZDVFDOOHGDQGWKH¶UH FDOOHGRQORQFHUHJDUGOHVVRIDQWKLQJ HOVHWKDWKDSSHQV%XWGR$IWHU%RG FDQUXQIURPWRPDQWLPHVGHSHQGLQJ RQWKHUHWXUQYDOXHRIGR6WDUW7DJDQG SUHYLRXVGR$IWHU%RGFDOOV GR6WDUW7DJDQG GR(QG7DJUXQ H[DFWORQFH 7KLQNDERXWLW7KHGHIDXOWUHWXUQYDOXHIURP GR6WDUW7DJLV6.,3B%2'<VRLIRXZDQWWKHERG RIRXUWDJHYDOXDWHGDQGRXH[WHQG7DJ6XSSRUW RX 086 7RYHUULGHGR6WDUW7DJLIIRUQRRWKHUUHDVRQ WKDQWRUHWXUQ(9$/B%2'<B,1& /8& apos;( :LWKGR$IWHU%RGLWVKRXOGEHREYLRXVWKDWLIRX ZDQWWRLWHUDWHRYHUWKHERGRXKDYHWRRYHUULGHWKDW PHWKRGDVZHOOVLQFHLWVUHWXUQYDOXHLV6.,3B%2'< <RX 086 7RYHUULGHGR6WDUW7DJ LIRXZDQWWKHWDJERGWREH HYDOXDWHG Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By. prohibited. FKDSWHU <RXGRQ¶WQHHGWR NQRZDOOWKHGHWDLOVRI XVLQJ%RG7DJ6XSSRUW )RUWKHH[DPDQGSUREDEOIRUWKHUHVW RIRXU-63GHYHORSPHQWOLIHRXQHHG WRNQRZWKHOLIHFFOHIRU%RG7DJ6XSSRUW DQGKRZLWGLIIHUVIURP7DJ6XSSRUW<RX QHHGWRNQRZIRUH[DPSOHWKDWLIRX GR127H[WHQG%RG7DJ6XSSRUWRU LPSOHPHQW%RG7DJWKHQRXPXVW127 UHWXUQ(9$/B%2'<B %8) )(5('IURP GR6WDUW7DJ$QGRXVKRXOGNQRZWKH WZRQHZPHWKRGVIURPWKH%RG7DJ LQWHUIDFHEXWWKDW¶VDERXWLW 4HE"ODY#ONTENTARGUMENTTOSET"ODY#ONTENTISACTUALLYA TYPEOFJAVAIO7RITER9ESITS/+TOlNDTHATDISTURBINGFROMAN //PERSPECTIVE"UTTHATMEANSYOUCANPROCESSTHEBODYBYSAY CHAININGITTOANOTHER)/STREAMORGETTINGTHERAWBYTES ?QP*WLa<IOaW]KIVJ]NNMZPMJWLa 4 7HATHAPPENSIF)RETURN %6!,?"/$9?"5&&%2%$EVENTHOUGHTHE INVOKINGTAGISEMPTY $ 4HESET"ODY#ONTENT AND DO)NIT"ODYMETHODWILLNOTBECALLED IFTHETAGINVOKINGTHEHANDLERISEMPTY !NDBYEMPTYWEMEANTHATTHETAGWAS INVOKEDUSINGANEMPTYTAGMYTAGOR WITHNOCONTENTBETWEENTHEOPENING AND CLOSINGTAGSMYTAGMYTAG 4HE#ONTAINERKNOWSTHERESNOBODYTHIS TIME AND ITJUSTSKIPSTOTHEDO%ND4AG METHODSOTHISISUSUALLYNOTAPROBLEM 5NLESSTHE4,$DECLARESTHETAGTOHAVEAN EMPTYBODY)FTHE4,$SAYS BODYCONTENTEMPTYBODYCONTENT YOUDONTHAVEACHOICE AND YOUMUST./4 RETURN%6!,?"/$9?"5&&%2%$OR %6!,?"/$9?).#,5$%FROMDO3TART4AG 4 7HATABOUTATTRIBUTESINA#LASSIC TAG!RETHEYHANDLEDTHESAMEWAYAS WITH3IMPLETAGS $ 9ESONTHESEQUENCEDIAGRAMFOR BOTH3IMPLETAGHANDLERS AND #LASSICTAG HANDLERSTHEREWASAPLACEWHEREBEAN STYLESETTERMETHODSARECALLEDFOREACH ATTRIBUTE4HISHAPPENSBEFOREA3IMPLE TAGSDO4AGORA#LASSICTAGSDO3TART4AG )NOTHERWORDSTAGATTRIBUTESWORKIN EXACTLYTHESAMEWAYFORBOTH#LASSIC AND 3IMPLETAGSINCLUDINGTHEWAYINWHICH THEYREDECLAREDINTHE4,$ 7KDWPLJKWEHREYLRXVEXWLWPHDQVRXKDYHWREHFDUHIXOWR NHHSRXUWDJKDQGOHUDQG7/'LQVQF6RLIRXGHFODUHD WDJLQWKH7/'WRKDYHERGFRQWHQW!HPSWERGFRQWHQW! WKHQWKHUHLVDEVROXWHO12SRLQWLQLPSOHPHQWLQJ%RG7DJRU H[WHQGLQJ%RG7DJ6XSSRUW7KDWDOVRPHDQVWKHUHLVQRSRLQW LQLPSOHPHQWLQJ,WHUDWLRQ7DJEXWRXJHWWKDWDXWRPDWLFDOOE H[WHQGLQJ7DJ6XSSRUW 7KHSRLQWLVRXQHHGWRUHWXUQ6.,3B%2'<IURPGR6WDUW7DJ LIRXU7/'GHFODUHVDQHPSWERGIRUWKHWDJHYHQ,)RX LPSOHPHQW,WHUDWLRQ7DJRU%RG7DJ ,IWKH7/'IRUDWDJGHFODUHVDQ HPSWERGGR6WDUW7DJ 086 7 UHWXUQ6.,3B%2'< %RG7DJDQG%RG7DJ6XSSRUW Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By Bert Bates,. &ODVVLFWDJOLIHFFOH Chapter 10. When even JSTL is not enough Chapter 10. When even JSTL is not enough Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 Publisher: O'Reilly Prepared