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,07 MB
Nội dung
DWWULEXWHVDQGOLVWHQHUV \RXDUHKHUH 6LQFHWKH&RQWDLQHU UHDGVWKHVHUYOHWLQLW SDUDPHWHUVRQO\ RQFH \RX VWLOO FDQ·WFKDQJH\RXUHPDLODGGUHVV GXULQJWKHOLIHRIWKHVHUYOHW 6RWKLVLVDGXPE VROXWLRQ ,W·VVWLOOZD\EHWWHUWKDQ SXWWLQJLWLQP\VHUYOHWVRXUFH FRGH$OO,KDYHWRGRLVFKDQJH WKH[PODQGKLWWKH´UHGHSOR\µ EXWWRQDQGWKHQHZDGGUHVVZLOO EHLQWKH6HUYOHW&RQÀJ 4 3OUMWHERESTHAT hREDEPLOYvBUTTONON4OMCAT $ 7ITH4OMCATTHEREISNTA ONEBUTTONREALLYSIMPLEADMINTOOL FORDEPLOYMENTANDREDEPLOYMENT ALTHOUGHTHEREISANADMINTOOLTHAT SHIPSWITH4OMCAT"UTTHINKABOUT ITWHATSTHEWORSTYOUHAVETODOTO CHANGETHESERVLETSINITPARAMETERS 9OUMAKEAQUICKCHANGETOTHEWEB XMLFILESHUTDOWN4OMCATBIN SHUTDOWNSHTHENRESTART4OMCAT BINSTARTUPSH/NRESTART4OMCAT LOOKSINITSWEBAPPSDIRECTORYAND DEPLOYSEVERYTHINGITFINDSTHERE 4 3UREITSEASYTOTELL4OMCAT TOSHUTDOWNANDSTARTUPBUT WHATABOUTTHEWEBAPPSTHATARE RUNNING4HEYALLHAVETOGODOWN $ 4ECHNICALLYYES4AKINGYOUR WEBAPPSDOWNSOTHATYOUCAN REDEPLOYONESERVLETISALITTLEHARSH ESPECIALLYIFYOUHAVEALOTOFTRAFFIC ONYOURWEBSITE"UTTHATSWHY MOSTOFTHEPRODUCTIONQUALITY7EB #ONTAINERSLETYOUDOAHOTREDEPLOY WHICHMEANSTHATYOUDONTHAVETO RESTARTYOURSERVERORTAKEANYOTHER WEBAPPSDOWN)NFACT4OMCATDOES INCLUDEAMANAGERTOOLTHATWILLLET YOUDEPLOYUNDEPLOYANDREDEPLOY ENTIREWEBAPPSWITHOUTRESTARTING 4OMCAT)NAPRODUCTIONENVIRONMENT THATSWHATYOUDUSE"UTFORTESTING ITSEASIERTOJUSTRESTART4OMCAT)NFO ONTHEMANAGEMENTTOOLISAT HTTPJAKARTAAPACHEORGTOMCAT TOMCATDOCMANAGERHOWTOHTML "UTINTHEREALWORLDEVENAHOT REDEPLOYISA"IG$EALANDTAKING EVENASINGLEAPPDOWNJUSTBECAUSE THEINITPARAMETERVALUECHANGED CANBEABADIDEA)FTHEVALUESOF YOURINITPARAMETERSAREGOINGTO CHANGEFREQUENTLYYOUREBETTEROFF HAVINGYOURSERVLETMETHODSGETTHE VALUESFROMAFILEORDATABASEBUT THISAPPROACHWILLMEANALOTMORE OVERHEADEACHTIMEYOURSERVLETCODE RUNSINSTEADOFONLYONCEDURING INITIALIZATION WKHUH DUH QR ' XPE 4XHVWLRQV Chapter 5. Being a Web App Chapter 5. Being a Web App 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 <M[\QVOaW]Z;MZ^TM\+WVNQO 3ERVLET#ONlGSMAINJOBISTOGIVEYOUINITPARAMETERS)TCAN ALSOGIVEYOUA3ERVLET#ONTEXTBUTWELLUSUALLYGETACONTEXTINA DIFFERENTWAYANDTHEGET3ERVLET.AMEMETHODISRARELYUSEFUL SXEOLFYRLGGR*HW+WWS6HUYOHW5HTXHVWUHTXHVW+WWS6HUYOHW5HVSRQVHUHVSRQVH WKURZV,2([FHSWLRQ6HUYOHW([FHSWLRQ^ UHVSRQVHVHW&RQWHQW7\SH³WH[WKWPO´ 3ULQW:ULWHURXW UHVSRQVHJHW:ULWHU RXWSULQWOQ³WHVWLQLWSDUDPHWHUVEU!´ (QXPHUDWLRQH JHW6HUYOHW&RQ¿JJHW,QLW3DUDPHWHU1DPHV ZKLOHHKDV0RUH(OHPHQWV^ RXWSULQWOQ³EU!SDUDPQDPH ³HQH[W(OHPHQW³EU!´ ` RXWSULQWOQ³PDLQHPDLOLV³JHW6HUYOHW&RQ¿JJHW,QLW3DUDPHWHU³PDLQ(PDLO´ RXWSULQWOQ³EU!´ RXWSULQWOQ³DGPLQHPDLOLV³JHW6HUYOHW&RQ¿JJHW,QLW3DUDPHWHU³DGPLQ(PDLO´ ` ` ,QWKH''ZHE[POÀOH ZHEDSS[POQV ´KWWSMDYDVXQFRP[POQVMHH´ [POQV[VL ´KWWSZZZZRUJ;0/6FKHPDLQVWDQFH´ [VLVFKHPD/RFDWLRQ ´KWWSMDYDVXQFRP[POQVMHHZHEDSSBB[VG´ YHUVLRQ ´´! VHUYOHW! VHUYOHWQDPH!%HHU3DUDP7HVWVVHUYOHWQDPH! VHUYOHWFODVV!FRPH[DPSOH7HVW,QLW3DUDPVVHUYOHWFODVV! LQLWSDUDP! SDUDPQDPH!DGPLQ(PDLOSDUDPQDPH! SDUDPYDOXH!OLNHZHFDUH#ZLFNHGO\VPDUWFRPSDUDPYDOXH! LQLWSDUDP! LQLWSDUDP! SDUDPQDPH!PDLQ(PDLOSDUDPQDPH! SDUDPYDOXH!EORRSHU#ZLFNHGO\VPDUWFRPSDUDPYDOXH! LQLWSDUDP! VHUYOHW! VHUYOHWPDSSLQJ! VHUYOHWQDPH!%HHU3DUDP7HVWVVHUYOHWQDPH! XUOSDWWHUQ!7HVWHUGRXUOSDWWHUQ! VHUYOHWPDSSLQJ! ZHEDSS! JHW,QLW3DUDPHWHU6WULQJ (QXPHUDWLRQJHW,QLW3DUDPHWHU1DPHV JHW6HUYOHW&RQWH[W JHW6HUYOHW1DPH LQWHUIDFH!! 6HUYOHW&RQILJ Most people never use this method. ,QDVHUYOHWFODVV MDYD[VHUYOHW6HUYOHW&RQILJ XVLQJ6HUYOHW&RQ¿J SDFNDJHFRPH[DPSOH LPSRUWMDYD[VHUYOHW LPSRUWMDYD[VHUYOHWKWWS LPSRUWMDYDLR SXEOLFFODVV7HVW,QLW3DUDPVH[WHQGV+WWS6HUYOHW^ Chapter 5. Being a Web App Chapter 5. Being a Web App 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. DWWULEXWHVDQGOLVWHQHUV \RXDUHKHUH 8KRK,MXVWUHDOL]HG WKDWLQP\UHDODSS,·PXVLQJ -63WRUHQGHUWKHSDJH6R FDQD-63´VHHµDVHUYOHW·V LQLWSDUDPHWHUV" 0W_KIVI2;8OM\[MZ^TM\ QVQ\XIZIUM\MZ[' !3ERVLET#ONlGISFORSERVLETCONlGURATIONITDOESNTSAY *30#ONlG3OIFYOUWANTOTHERPARTSOFYOURAPPLICATION TOUSETHESAMEINFOYOUPUTINTHESERVLETSINIT PARAMETERSINTHE$$YOUNEEDSOMETHINGMORE ?PI\IJW]\\PM_Ia_MLQLQ\_Q\P\PMJMMZ IXX'?MXI[[ML\PMUWLMTQVNW\W\PM2;8 ][QVOIZMY]M[\I\\ZQJ]\M 7ECOULDDOITTHISWAY4HEREQUESTOBJECTLETSYOUSET ATTRIBUTESTHINKOFTHEMASANAMEVALUEPAIRWHERETHE VALUECANBEANYOBJECTTHATANYOTHERSERVLETOR*30THAT GETSTHEREQUESTCANUSE4HATMEANSANYSERVLETOR*30TO WHICHTHEREQUESTISFORWARDEDUSINGA2EQUEST$ISPATCHER 7ELLLOOKAT2EQUEST$ISPATCHERINDETAILATTHEENDOF THISCHAPTERBUTFORNOWALLWECAREABOUTISGETTINGTHE DATAINTHISCASETHEEMAILADDRESSTOTHEPIECESOFTHE WEBAPPTHATNEEDITRATHERTHANJUSTONESERVLET LQVLGHWKHGR3RVWPHWKRG 6WULQJFRORU UHTXHVWJHW3DUDPHWHU³FRORU´ %HHU([SHUWEH QHZ%HHU([SHUW /LVWUHVXOW EHJHW%UDQGVFRORU UHTXHVWVHW$WWULEXWH³VW\OHV´UHVXOW Remember? We got the clientÕs color choice from the request. Then we instantiated and used the MODEL to get the info we need for the VIEW. Then we set an ÒattributeÓ in the request, and the JSP we forward the request to was able to get it. Chapter 5. Being a Web App Chapter 5. Being a Web App 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 ;M\\QVOIZMY]M[\ I\\ZQJ]\M _WZS[J]\WVTaNWZ\PM2;8\W _PQKPaW]NWZ_IZLML\PMZMY]M[\ 7ITHTHEBEERAPPITMADESENSETOSTORETHEMODELINFO FORTHECLIENTSREQUESTINTHEREQUESTOBJECTBECAUSETHENEXT STEPWASTOFORWARDTHEREQUESTTOTHE*30RESPONSIBLEFOR CREATINGTHEVIEW3INCETHAT*30NEEDEDTHEMODELDATA ANDTHEDATAWASRELEVANTTOONLYTHATPARTICULARREQUEST EVERYTHINGWASlNE "UTTHATDOESNTHELPUSWITHTHEEMAILADDRESSBECAUSEWE MIGHTNEEDTOUSEITFROMALLOVERTHEAPPLICATION4HERE ISAWAYTOHAVEASERVLETREADTHEINITPARAMETERSANDTHEN STORETHEMINAPLACEOTHERPARTSOFTHEAPPCOULDUSEBUT THENWEDHAVETOKNOWWHICHSERVLETWOULDALWAYSRUNlRST WHENTHEAPPISDEPLOYEDANDANYCHANGESTOTHEWEBAPP COULDBREAKTHEWHOLETHING.OTHATWONTDOEITHER %XW,UHDOO\ZDQW$//WKHSDUWV RIP\ZHEDSSWRKDYHDFFHVVWRWKH HPDLODGGUHVV:LWKLQLWSDUDPHWHUV ,KDYHWRFRQÀJXUHWKHPLQWKH''IRU HYHU\ VHUYOHWDQGWKHQKDYHDOOWKHVHUYOHWVPDNH WKHPDYDLODEOHIRUWKH-63V+RZERULQJ LVWKDW"1RWPDLQWDLQDEOHHLWKHU, QHHGVRPHWKLQJPRUH JOREDO ,ZRQGHULI WKHUH·VVRPHWKLQJOLNH LQLWSDUDPHWHUVIRUWKH DSSOLFDWLRQ " LQLWSDUDPHWHUOLPLWDWLRQV Chapter 5. Being a Web App Chapter 5. Being a Web App 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. DWWULEXWHVDQGOLVWHQHUV \RXDUHKHUH +WV\M`\QVQ\XIZIUM\MZ[\W\PMZM[K]M #ONTEXTINITPARAMETERSWORKJUSTLIKESERVLETINITPARAMETERSEXCEPT CONTEXTPARAMETERSAREAVAILABLETOTHEENTIREWEBAPPNOTJUST ASINGLESERVLET3OTHATMEANSANYSERVLETAND*30INTHEAPP AUTOMATICALLYHASACCESSTOTHECONTEXTINITPARAMETERSSOWEDONT HAVETOWORRYABOUTCONlGURINGTHE$$FOREVERYSERVLETAND WHENTHEVALUECHANGESYOUONLYHAVETOCHANGEITONEPLACE ,QWKH''ZHE[POÀOH VHUYOHW! VHUYOHWQDPH!%HHU3DUDP7HVWVVHUYOHWQDPH! VHUYOHWFODVV!7HVW,QLW3DUDPVVHUYOHWFODVV! VHUYOHW! FRQWH[WSDUDP! SDUDPQDPH!DGPLQ(PDLOSDUDPQDPH! SDUDPYDOXH!FOLHQWKHDGHUURU#ZLFNHGO\VPDUWFRPSDUDPYDOXH! FRQWH[WSDUDP! You give it a param-name and param-value just like with servlet init parameters, except this time itÕs in the <context-param> element instead of <init-param>. ,QWKHVHUYOHWFRGH RXWSULQWOQJHW6HUYOHW&RQWH[WJHW,QLW3DUDPHWHU³DGPLQ(PDLO´ Every servlet inherits a getServletContext() method (and JSPs have special access to a context as well). The getServletContext() method returns, surprisingly, a ServletContext object. And one of its methods is getInitParameter(). 6HUYOHW&RQWH[WFRQWH[W JHW6HUYOHW&RQWH[W RXWSULQWOQFRQWH[WJHW,QLW3DUDPHWHU³DGPLQ(PDLO´ 25 Here we broke out the code into TWO stepsÑ getting the ServletContext reference, and calling its getInitParameter() method. We took the <init-param> element out of the <servlet> element. IMPORTANT!! The <context-param> is for the WHOLE app, so its not nested inside an individual <servlet> element!! Put <context-param> inside the <web-app> but OUTSIDE any <servlet> declaration. Chapter 5. Being a Web App Chapter 5. Being a Web App 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 :MUMUJMZ\PMLQNNMZMVKMJM\_MMV[MZ^TM\QVQ\ XIZIUM\MZ[IVLKWV\M`\QVQ\XIZIUM\MZ[ (ERESAREVIEWOFTHEKEYDIFFERENCESBETWEENCONTEXTINITPARAMETERSANDSERVLET INITPARAMETERS0AYSPECIALATTENTIONTOTHEFACTTHATTHEYREBOTHREFERREDTOAS INITPARAMETERSEVENTHOUGHONLYSERVLETINITPARAMETERSHAVETHEWORDhINITvIN THE$$CONlGURATION &RQWH[WLQLWSDUDPHWHUV 6HUYOHWLQLWSDUDPHWHUV 'HSOR\PHQW 'HVFULSWRU ZHEDSS! FRQWH[WSDUDP! SDUDPQDPH!IRRSDUDPQDPH! SDUDPYDOXH!EDUSDUDPYDOXH! FRQWH[WSDUDP! RWKHUVWXIILQFOXGLQJ VHUYOHWGHFODUDWLRQV! ZHEDSS! :LWKLQWKHZHEDSS!HOHPHQWEXW127 ZLWKLQDVSHFLILFVHUYOHW!HOHPHQW VHUYOHW! VHUYOHWQDPH! %HHU3DUDP7HVWV VHUYOHWQDPH! VHUYOHWFODVV! 7HVW,QLW3DUDPV VHUYOHWFODVV! LQLWSDUDP! SDUDPQDPH!IRRSDUDPQDPH! SDUDPYDOXH!EDUSDUDPYDOXH! LQLWSDUDP! RWKHUVWXII! VHUYOHW! :LWKLQWKHVHUYOHW!HOHPHQWIRUHDFK VSHFLILFVHUYOHW JHW6HUYOHW&RQWH[WJHW,QLW3DUDPHWHU³IRR´ JHW6HUYOHW&RQ¿JJHW,QLW3DUDPHWHU³IRR´ 6HUYOHW&RGH $YDLODELOLW\ 7RDQ\VHUYOHWVDQG-63VWKDWDUHSDUW RIWKLVZHEDSS 7RRQO\WKHVHUYOHWIRUZKLFKWKHLQLWSDUDP!ZDV FRQILJXUHG $OWKRXJKWKHVHUYOHWFDQFKRRVHWRPDNHLWPRUH ZLGHO\DYDLODEOHE\VWRULQJLWLQDQDWWULEXWH FRQWH[WYVVHUYOHWLQLWSDUDPHWHUV Notice it doesnÕt say ÒinitÓ anywhere in the DD for context init parameters, the way it does for servlet init parameters. ItÕs the same method name! Chapter 5. Being a Web App Chapter 5. Being a Web App 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. DWWULEXWHVDQGOLVWHQHUV \RXDUHKHUH :DWFKLW <RXUHDOO\KDYHWRNHHSWKHVHVWUDLJKWRQ WKHH[DPDQGLW¶VWULFN\<RX0867NQRZ WKDWERWK6HUYOHW&RQ¿JDQG6HUYOHW&RQWH[W KDYHLQLWSDUDPHWHUVDQGERWKKDYHWKH VDPHJHWWHUPHWKRG²JHW,QLW3DUDPHWHU %87\RXDOVRKDYHWRNQRZWKDWFRQWH[W LQLWSDUDPHWHUVDUHVHWZLWKFRQWH[WSDUDP! QRWLQVLGHDVHUYOHW!HOHPHQWZKLOHVHUYOHW LQLWSDUDPHWHUVXVHLQLWSDUDP!LQVLGHWKH LQGLYLGXDOVHUYOHW!GHFODUDWLRQVLQWKH'' 'RQ·WFRQIXVH 6HUYOHW &RQÀJ SDUDPHWHUVZLWK 6HUYOHW &RQWH[W SDUDPHWHUV ; M Z ^ T M \ +WVNQO Q[WVMXMZ[MZ^TM\ ; M Z ^ T M \ +WV\M`\ Q[WVMXMZ_MJIXX 4HERESONLYONE3ERVLET#ONTEXTFORANENTIREWEBAPPANDALLTHE PARTSOFTHEWEBAPPSHAREIT"UTEACHSERVLETINTHEAPPHASITSOWN 3ERVLET#ONlG4HE#ONTAINERMAKESA3ERVLET#ONTEXTWHENAWEB APPISDEPLOYEDANDMAKESTHECONTEXTAVAILABLETOEACH3ERVLETAND *30WHICHBECOMESASERVLETINTHEWEBAPP &RQWDLQHUUHDGVWKH''DQG FUHDWHVDQDPHYDOXH6WULQJSDLU IRUHDFKFRQWH[WSDUDP! &RQWDLQHUFUHDWHVDQHZLQVWDQFH RI6HUYOHW&RQWH[W &RQWDLQHUJLYHVWKH 6HUYOHW&RQWH[WDUHIHUHQFHWR HDFKQDPHYDOXHSDLURIWKH FRQWH[WLQLWSDUDPHWHUV (YHU\VHUYOHWDQG-63GHSOR\HG DVSDUWRIDVLQJOHZHEDSS KDVDFFHVVWRWKDWVDPH 6HUYOHW&RQWH[W :HEDSSLQLWLDOL]DWLRQ app-wide context init params inside 6HUYOHW$ 6 H U Y O H W & R Q I L J 6HUYOHW% 6HUYOHW& 6 H U Y O H W & R Q I L J 6 H U Y O H W & R Q I L J 6 H U Y O H W & R Q W H [ W KWPO! ERG\! MVSVHW3URSHUW\ QDPH ´IRR´ SURSHUW\ ´EDU´! ERG\! KWPO! -63 init params for Servlet C inside init params for Servlet B inside init params for Servlet A inside :DWFKLW ,I\RXUDSSOLFDWLRQLVGLVWULEXWHGDFURVV PXOWLSOHVHUYHUVSUREDEO\LQDFOXVWHUHG HQYLURQPHQW\RXUZHEDSSUHDOO\&28/' KDYHPRUHWKDQRQH6HUYOHW&RQWH[W$ 6HUYOHW&RQWH[WLVRQHSHUDSSEXWRQO\LI WKHDSSLVLQDVLQJOH-90 ,QDGLVWULEXWHGHQYLURQPHQW\RX¶OOKDYHRQH 6HUYOHW&RQWH[WSHU-901RZFKDQFHVDUH WKLVZRQ¶WFUHDWHSUREOHPVEXWLI\RXKDYHD GLVWULEXWHGZHEDSS\RXEHWWHUFRQVLGHUWKH FRQVHTXHQFHVRIKDYLQJGLIIHUHQWFRQWH[WV IRUHDFK-90 ,IWKHDSSLV GLVWULEXWHGWKHUH·V RQH6HUYOHW&RQWH[W SHU-90 6 H U Y O H W & R Q I L J Yes, JSPs are turned into Þ rst-class servlets, so they also get their own ServletConÞ g. Chapter 5. Being a Web App Chapter 5. Being a Web App 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 7HATSWITHTHEINCONSISTENTNAMINGSCHEME (OWCOMETHE$$ELEMENTSARECONTEXTPARAMAND INITPARAMBUTINTHESERVLETCODE"/4(USETHE GET)NIT0ARAMETERMETHOD $ 4HEYDIDNTASKUSTOHELPTHEMCOMEUPWITHTHE NAMES)FTHEYHADOFCOURSEWEDHAVESAIDITSHOULDBE GET)NIT0ARAMETERANDGET#ONTEXT0ARAMETERTOMATCH THE8-,ELEMENTSINTHE$$/RTHEYCOULDHAVEUSED DIFFERENT8-,ELEMENTSPERHAPSSERVLETINITPARAM ANDCONTEXTINITPARAM"UTNOTHATWOULDHAVESUCKED ALLTHEFUNOUTOFTRYINGTOKEEPTHEMSTRAIGHT 4 7HYWOULD)EVERUSEINITPARAMANYWAY 7OULDNT)ALWAYSWANTTOUSECONTEXTPARAMSO THATOTHERPARTSOFMYAPPCOULDREUSETHEVALUESAND )WONTHAVETODUPLICATE8-,CODEFOREVERYSERVLET DECLARATION $ )TALLDEPENDSONWHICHPARTOFYOURAPPIS SUPPOSEDTOSEETHEVALUE9OURAPPLICATIONLOGICMIGHT REQUIREYOUTOUSEAVALUETHATYOUWANTTORESTRICTTO ONLYANINDIVIDUALSERVLET"UTTYPICALLYDEVELOPERSFIND APPWIDECONTEXTINITPARAMETERSALOTMOREHELPFULTHAN SERVLETSPECIFICSERVLETINITPARAMETERS0ERHAPSTHEMOST COMMONUSEOFACONTEXTPARAMETERISSTORINGDATABASE LOOKUPNAMES9OUDWANTALLPARTSOFYOURAPPTOHAVE ACCESSTOTHECORRECTNAMEANDWHENITCHANGESYOUWANT TOCHANGEITINONLYONEPLACE 4 7HATHAPPENSIF)GIVEACONTEXTINITPARAMETER THESAMENAMEASASERVLETINITPARAMETERINTHESAME WEBAPP $ 4HEMOLECULARSIZEDBLACKHOLEMIRACULOUSLY CREATEDINARESEARCHFACILITYIN.EW*ERSEYWILLSLIPFROMITS CONTAINMENTlELDPLUMMETTOTHEEARTHSCOREANDDESTROY THEPLANET /RMAYBENOTHINGBECAUSETHERESNONAMESPACECONmICT SINCEYOUGETTHEPARAMETERSTHROUGHTWODIFFERENTOBJECTS 3ERVLET#ONTEXTOR3ERVLET#ONlG 4 )FYOUMODIFYTHE8-,TOCHANGETHEVALUEOFAN INITPARAMETEREITHERSERVLETORCONTEXTWHENDOESTHE SERVLETORTHERESTOFTHEWEBAPPSEETHECHANGE $ /.,9WHENTHEWEBAPPISREDEPLOYED 2EMEMBERWETALKEDABOUTTHISBEFORETHESERVLETIS INITIALIZEDONLYONCEATTHEBEGINNINGOFITSLIFEANDTHATS WHENITSGIVENITS3ERVLET#ONFIGAND3ERVLET#ONTEXT4HE #ONTAINERREADSTHEVALUESFROMTHE$$WHENITCREATES THOSETWOOBJECTSANDSETSTHEVALUES 4 #ANT)GETAROUNDTHISBYSETTINGTHEVALUESAT RUNTIME3URELYTHERESAN!0)THATLLLETMECHANGE THOSEVALUESDYNAMICALLY $ .OTHERESNOT,OOKIN3ERVLET#ONTEXTOR 3ERVLET#ONFIGANDYOULLFINDAGETTERGET)NIT0ARAMETER BUTYOUWONTFINDASETTER4HERESNOSET)NIT0ARAMETER 4 4HATSLAME $ 4HESEAREINITPARAMETERS)NITFROMTHE,ATINWORD INITIALIZATION)FYOUTHINKOFTHEMPURELYASDEPLOYTIME CONSTANTSYOULLHAVETHERIGHTPERSPECTIVE)NFACTTHATS SOIMPORTANTWEREGOINGTOSAYITAGAININABOLDERWAY 7KLQNRILQLWSDUDPHWHUVDV GHSOR\WLPHFRQVWDQWV <RXFDQ JHW WKHPDWUXQWLPH EXW\RXFDQ·W VHW WKHP7KHUH·V QR VHW ,QLW3DUDPHWHU VHUYOHWDQGFRQWH[WLQLWSDUDPHWHUV WKHUH DUH QR ' XPE 4XHVWLRQV Chapter 5. Being a Web App Chapter 5. Being a Web App 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. DWWULEXWHVDQGOLVWHQHUV \RXDUHKHUH 6RPHSHRSOHXVHWKHSKUDVH³LQLWSDUDPHWHU´WRPHDQ³VHUYOHW LQLWSDUDPHWHU´DQGWKH\XVH³FRQWH[WSDUDPHWHU´RUHYHQ³DS SOLFDWLRQSDUDPHWHU´WRPHDQ³FRQWH[WLQLWSDUDPHWHU´6RHYHQ WKRXJK%27+DUHLQLWLDOL]DWLRQSDUDPHWHUVDQGERWKFRPHIURP WKHJHW,QLW3DUDPHWHUPHWKRGUHPHPEHUWKDWRQO\6(59/(7LQLW SDUDPHWHUVDUHOLVWHGLQWKH''DVLQLWSDUDPHWHUVVRWKHSKUDVH ³LQLWSDUDPHWHU´PHDQV³VHUYOHWLQLWSDUDPHWHU´E\GHIDXOW :HNQRZWKDWDVDGHYHORSHU\RX¶OOEHNLQGHUWRRWKHUVDQG DOZD\VVD\H[SOLFLWO\ZKHWKHUDQLQLWSDUDPHWHULVDVHUYOHWLQLW SDUDPHWHURUDFRQWH[WLQLWSDUDPHWHU ,I\RXVHH´LQLWSDUDPHWHUµZLWKRXW NQRZLQJLILWPHDQV VHUYOHW RU FRQWH[W LQLWSDUDPHWHUDVVXPH VHUYOHW FRQWH[WSDUDP! FRQWH[WSDUDP! VHUYOHW! VHUYOHW! ZHEDSS! ZHEDSS! SDUDPQDPH! VHUYOHWQDPH! SDUDPQDPH! VHUYOHWFODVV! VHUYOHWFODVV! SDUDPYDOXH! SDUDPYDOXH! IRR FRPZLFNHGO\VPDUW%HHU7HVWHU EDU %HHU7HVWVHUYOHWQDPH! 2EARRANGETHEMAGNETSTOFORMA$$THATDECLARESA PARAMETERTHATMATCHESTHESERVLETCODE 9OUWONTUSEALLOFTHEMAGNETS .OTEWHENYOUSEEWEBAPPREMEMBERTHAT THISISOURSHORTCUTTOSAVESPACEONTHEPAGE9OU CANTDEPLOYAWEBXMLFILEUNLESSTHEWEBAPP TAGHASALLTHEATTRIBUTESITNEEDS ([HUFLVH &RGH0DJQHWV JHW6HUYOHW&RQWH[WJHW,QLW3DUDPHWHU³IRR´ LQLWSDUDP! LQLWSDUDP! VHUYOHWSDUDP! VHUYOHWSDUDP! Chapter 5. Being a Web App Chapter 5. Being a Web App 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 ;W_PI\ MT[M KIVaW]LW_Q\P aW]Z;MZ^TM\+WV\M`\' !3ERVLET#ONTEXTISA*30ORSERVLETSCONNECTION TOBOTHTHE#ONTAINERANDTHEOTHERPARTSOFTHE WEBAPP(EREARESOMEOFTHE3ERVLET#ONTEXT METHODS7EPUTTHEONESYOUSHOULDKNOWFOR THEEXAMINBOLD Get init parameters and get/set attributes. Get info about the server/container. WeÕll talk about RequestDispatcher later in the chapter. JHW,QLW3DUDPHWHU6WULQJ JHW,QLW3DUDPHWHU1DPHV JHW$WWULEXWH6WULQJ JHW$WWULEXWH1DPHV VHW$WWULEXWH6WULQJ UHPRYH$WWULEXWH6WULQJ JHW0DMRU9HUVLRQ JHW6HUYHU,QIR JHW5HDO3DWK6WULQJ JHW5HVRXUFH$V6WUHDP6WULQJ JHW5HTXHVW'LVSDWFKHU6WULQJ ORJ6WULQJ PRUHPHWKRGV LQWHUIDFH!! 6HUYOHW&RQWH[W Write to the serverÕs log Þ le (vendor-speciÞ c) or System.out. WeÕll talk about parameters vs. attributes in a few pages. MDYD[VHUYOHW6HUYOHW&RQWH[W WKH6HUYOHW&RQWH[W 6 H U Y O H W & R Q W H [ W 6HUYOHW <RXFRPSOHWHPH %HIRUH,FDPHLQWR \RXUOLIH\RXZHUH MXVWDQRWKHUORVHUREMHFW LQVWHDGRIDUHDOVHUYOHW Chapter 5. Being a Web App Chapter 5. Being a Web App 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. [...]... 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 5 Being a Web App Chapter 5 Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407... 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 5 Being a Web App Chapter 5 Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407... 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 5 Being a Web App Chapter 5 Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407... 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 5 Being a Web App Chapter 5 Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407... 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 5 Being a Web App Chapter 5 Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407... 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 5 Being a Web App Chapter 5 Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407... 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 5 Being a Web App Chapter 5 Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407... 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 5 Being a Web App Chapter 5 Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407... 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 5 Being a Web App Chapter 5 Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407... 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 5 Being a Web App Chapter 5 Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham ISBN: 0596005407 . inside :DWFKLW ,IRXUDSSOLFDWLRQLVGLVWULEXWHGDFURVV PXOWLSOHVHUYHUVSUREDEOLQDFOXVWHUHG HQYLURQPHQWRXUZHEDSSUHDOO&28/' KDYHPRUHWKDQRQH6HUYOHW&RQWH[W$ 6HUYOHW&RQWH[WLVRQHSHUDSSEXWRQOLI WKHDSSLVLQDVLQJOH-90 ,QDGLVWULEXWHGHQYLURQPHQWRX¶OOKDYHRQH 6HUYOHW&RQWH[WSHU-901RZFKDQFHVDUH WKLVZRQ¶WFUHDWHSUREOHPVEXWLIRXKDYHD GLVWULEXWHGZHEDSSRXEHWWHUFRQVLGHUWKH FRQVHTXHQFHVRIKDYLQJGLIIHUHQWFRQWH[WV IRUHDFK-90 ,IWKHDSSLV GLVWULEXWHGWKHUH·V RQH6HUYOHW&RQWH[W SHU-90 6 H U Y O H W & R Q I L J Yes, JSPs are turned into Þ rst-class servlets, so they also get their own ServletConÞ g. Chapter 5. Being a Web App Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates,. method. ,QDVHUYOHWFODVV MDYD[VHUYOHW6HUYOHW&RQILJ XVLQJ6HUYOHW&RQ¿J SDFNDJHFRPH[DPSOH LPSRUWMDYD[VHUYOHW LPSRUWMDYD[VHUYOHWKWWS LPSRUWMDYDLR SXEOLFFODVV7HVW,QLW3DUDPVH[WHQGV+WWS6HUYOHW^ Chapter 5. Being a Web App Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates, Kathy Sierra, Bryan Basham. prohibited. FKDSWHU 4 7HATSWITHTHEINCONSISTENTNAMINGSCHEME (OWCOMETHE$$ELEMENTSARECONTEXTPARAM AND INITPARAMBUTINTHESERVLETCODE"/4(USETHE GET)NIT0ARAMETERMETHOD $ 4HEYDIDNTASKUSTOHELPTHEMCOMEUPWITHTHE NAMES)FTHEYHADOFCOURSEWEDHAVESAIDITSHOULDBE GET)NIT0ARAMETER AND GET#ONTEXT0ARAMETERTOMATCH THE8-,ELEMENTSINTHE$$/RTHEYCOULDHAVEUSED DIFFERENT8-,ELEMENTSPERHAPSSERVLETINITPARAM AND CONTEXTINITPARAM"UTNOTHATWOULDHAVESUCKED ALLTHEFUNOUTOFTRYINGTOKEEPTHEMSTRAIGHT 4 7HYWOULD)EVERUSEINITPARAMANYWAY 7OULDNT)ALWAYSWANTTOUSECONTEXTPARAMSO THATOTHERPARTSOFMYAPPCOULDREUSETHEVALUES AND )WONTHAVETODUPLICATE8-,CODEFOREVERYSERVLET DECLARATION $ )TALLDEPENDSONWHICHPARTOFYOURAPPIS SUPPOSEDTOSEETHEVALUE9OURAPPLICATIONLOGICMIGHT REQUIREYOUTOUSEAVALUETHATYOUWANTTORESTRICTTO ONLYANINDIVIDUALSERVLET"UTTYPICALLYDEVELOPERSFIND APPWIDECONTEXTINITPARAMETERSALOTMOREHELPFULTHAN SERVLETSPECIFICSERVLETINITPARAMETERS0ERHAPSTHEMOST COMMONUSEOFACONTEXTPARAMETERISSTORINGDATABASE LOOKUPNAMES9OUDWANTALLPARTSOFYOURAPPTOHAVE ACCESSTOTHECORRECTNAME AND WHENITCHANGESYOUWANT TOCHANGEITINONLYONEPLACE 4 7HATHAPPENSIF)GIVEACONTEXTINITPARAMETER THESAMENAMEASASERVLETINITPARAMETERINTHESAME WEBAPP $ 4HEMOLECULARSIZEDBLACKHOLEMIRACULOUSLY CREATEDINARESEARCHFACILITYIN.EW*ERSEYWILLSLIPFROMITS CONTAINMENTlELDPLUMMETTOTHEEARTHSCORE AND DESTROY THEPLANET /RMAYBENOTHINGBECAUSETHERESNONAMESPACECONmICT SINCEYOUGETTHEPARAMETERSTHROUGHTWODIFFERENTOBJECTS 3ERVLET#ONTEXTOR3ERVLET#ONlG 4 )FYOUMODIFYTHE8-,TOCHANGETHEVALUEOFAN INITPARAMETEREITHERSERVLETORCONTEXTWHENDOESTHE SERVLETORTHERESTOFTHEWEBAPPSEETHECHANGE $ /.,9WHENTHEWEBAPPISREDEPLOYED 2EMEMBERWETALKEDABOUTTHISBEFORETHESERVLETIS INITIALIZEDONLYONCEATTHEBEGINNINGOFITSLIFE AND THATS WHENITSGIVENITS3ERVLET#ONFIG AND 3ERVLET#ONTEXT4HE #ONTAINERREADSTHEVALUESFROMTHE$$WHENITCREATES THOSETWOOBJECTS AND SETSTHEVALUES 4 #ANT)GETAROUNDTHISBYSETTINGTHEVALUESAT RUNTIME3URELYTHERESAN!0)THATLLLETMECHANGE THOSEVALUESDYNAMICALLY $ .OTHERESNOT,OOKIN3ERVLET#ONTEXTOR 3ERVLET#ONFIG AND YOULLFINDAGETTERGET)NIT0ARAMETER BUTYOUWONTFINDASETTER4HERESNOSET)NIT0ARAMETER 4 4HATSLAME $ 4HESEAREINITPARAMETERS)NITFROMTHE,ATINWORD INITIALIZATION)FYOUTHINKOFTHEMPURELYASDEPLOYTIME CONSTANTSYOULLHAVETHERIGHTPERSPECTIVE)NFACTTHATS SOIMPORTANTWEREGOINGTOSAYITAGAININABOLDERWAY 7KLQNRILQLWSDUDPHWHUVDV GHSORWLPHFRQVWDQWV <RXFDQ JHW WKHPDWUXQWLPH EXWRXFDQ·W VHW WKHP7KHUH·V QR VHW ,QLW3DUDPHWHU VHUYOHWDQGFRQWH[WLQLWSDUDPHWHUV WKHUH DUH QR ' XPE 4XHVWLRQV Chapter 5. Being a Web App Chapter 5. Being a Web App Head First Servlets and JSP By Bert Bates,