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

Head First Servlets and JSP phần 3 docx

74 296 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 74
Dung lượng 2,07 MB

Nội dung

DWWULEXWHVDQGOLVWHQHUV \RXDUHKHUH  6LQFHWKH&RQWDLQHU UHDGVWKHVHUYOHWLQLW SDUDPHWHUVRQO\ RQFH \RX VWLOO  FDQ·WFKDQJH\RXUHPDLODGGUHVV GXULQJWKHOLIHRIWKHVHUYOHW 6RWKLVLVDGXPE VROXWLRQ ,W·VVWLOOZD\EHWWHUWKDQ SXWWLQJLWLQP\VHUYOHWVRXUFH FRGH$OO,KDYHWRGRLVFKDQJH WKH[PODQGKLWWKH´UHGHSOR\µ EXWWRQDQGWKHQHZDGGUHVVZLOO EHLQWKH6HUYOHW&RQÀJ 4  3OUMWHERESTHAT hREDEPLOYvBUTTONON4OMCAT $  7ITH4OMCATTHEREISNTA ONEBUTTONREALLYSIMPLEADMINTOOL FORDEPLOYMENTANDREDEPLOYMENT ALTHOUGHTHEREISANADMINTOOLTHAT SHIPSWITH4OMCAT"UTTHINKABOUT ITWHATSTHEWORSTYOUHAVETODOTO CHANGETHESERVLETSINITPARAMETERS 9OUMAKEAQUICKCHANGETOTHEWEB XMLFILESHUTDOWN4OMCATBIN SHUTDOWNSHTHENRESTART4OMCAT BINSTARTUPSH/NRESTART4OMCAT LOOKSINITSWEBAPPSDIRECTORYAND DEPLOYSEVERYTHINGITFINDSTHERE 4  3UREITSEASYTOTELL4OMCAT TOSHUTDOWNANDSTARTUPBUT WHATABOUTTHEWEBAPPSTHATARE RUNNING4HEYALLHAVETOGODOWN $  4ECHNICALLYYES4AKINGYOUR WEBAPPSDOWNSOTHATYOUCAN REDEPLOYONESERVLETISALITTLEHARSH ESPECIALLYIFYOUHAVEALOTOFTRAFFIC ONYOURWEBSITE"UTTHATSWHY MOSTOFTHEPRODUCTIONQUALITY7EB #ONTAINERSLETYOUDOAHOTREDEPLOY WHICHMEANSTHATYOUDONTHAVETO RESTARTYOURSERVERORTAKEANYOTHER WEBAPPSDOWN)NFACT4OMCATDOES INCLUDEAMANAGERTOOLTHATWILLLET YOUDEPLOYUNDEPLOYANDREDEPLOY ENTIREWEBAPPSWITHOUTRESTARTING 4OMCAT)NAPRODUCTIONENVIRONMENT THATSWHATYOUDUSE"UTFORTESTING ITSEASIERTOJUSTRESTART4OMCAT)NFO ONTHEMANAGEMENTTOOLISAT HTTPJAKARTAAPACHEORGTOMCAT TOMCATDOCMANAGERHOWTOHTML "UTINTHEREALWORLDEVENAHOT REDEPLOYISA"IG$EALANDTAKING EVENASINGLEAPPDOWNJUSTBECAUSE THEINITPARAMETERVALUECHANGED CANBEABADIDEA)FTHEVALUESOF YOURINITPARAMETERSAREGOINGTO CHANGEFREQUENTLYYOUREBETTEROFF HAVINGYOURSERVLETMETHODSGETTHE VALUESFROMAFILEORDATABASEBUT THISAPPROACHWILLMEANALOTMORE OVERHEADEACHTIMEYOURSERVLETCODE RUNSINSTEADOFONLYONCEDURING 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[\QVOaW]Z;MZ^TM\+WVNQO 3ERVLET#ONlGSMAINJOBISTOGIVEYOUINITPARAMETERS)TCAN ALSOGIVEYOUA3ERVLET#ONTEXTBUTWELLUSUALLYGETACONTEXTINA DIFFERENTWAYANDTHEGET3ERVLET.AMEMETHODISRARELYUSEFUL SXEOLFYRLGGR*HW+WWS6HUYOHW5HTXHVWUHTXHVW+WWS6HUYOHW5HVSRQVHUHVSRQVH WKURZV,2([FHSWLRQ6HUYOHW([FHSWLRQ^ UHVSRQVHVHW&RQWHQW7\SH³WH[WKWPO´ 3ULQW:ULWHURXW UHVSRQVHJHW:ULWHU RXWSULQWOQ³WHVWLQLWSDUDPHWHUVEU!´  (QXPHUDWLRQH JHW6HUYOHW&RQ¿JJHW,QLW3DUDPHWHU1DPHV ZKLOHHKDV0RUH(OHPHQWV^  RXWSULQWOQ³EU!SDUDPQDPH ³HQH[W(OHPHQW³EU!´ ` RXWSULQWOQ³PDLQHPDLOLV³JHW6HUYOHW&RQ¿JJHW,QLW3DUDPHWHU³PDLQ(PDLO´ RXWSULQWOQ³EU!´ RXWSULQWOQ³DGPLQHPDLOLV³JHW6HUYOHW&RQ¿JJHW,QLW3DUDPHWHU³DGPLQ(PDLO´ ` ` ,QWKH''ZHE[POÀOH ZHEDSS[POQV ´KWWSMDYDVXQFRP[POQVMHH´ [POQV[VL ´KWWSZZZZRUJ;0/6FKHPDLQVWDQFH´ [VLVFKHPD/RFDWLRQ ´KWWSMDYDVXQFRP[POQVMHHZHEDSSBB[VG´ YHUVLRQ ´´! VHUYOHW! VHUYOHWQDPH!%HHU3DUDP7HVWVVHUYOHWQDPH! VHUYOHWFODVV!FRPH[DPSOH7HVW,QLW3DUDPVVHUYOHWFODVV! LQLWSDUDP! SDUDPQDPH!DGPLQ(PDLOSDUDPQDPH! SDUDPYDOXH!OLNHZHFDUH#ZLFNHGO\VPDUWFRPSDUDPYDOXH! LQLWSDUDP! LQLWSDUDP! SDUDPQDPH!PDLQ(PDLOSDUDPQDPH! SDUDPYDOXH!EORRSHU#ZLFNHGO\VPDUWFRPSDUDPYDOXH! LQLWSDUDP! VHUYOHW! VHUYOHWPDSSLQJ! VHUYOHWQDPH!%HHU3DUDP7HVWVVHUYOHWQDPH! XUOSDWWHUQ!7HVWHUGRXUOSDWWHUQ! VHUYOHWPDSSLQJ! ZHEDSS! JHW,QLW3DUDPHWHU6WULQJ (QXPHUDWLRQJHW,QLW3DUDPHWHU1DPHV JHW6HUYOHW&RQWH[W JHW6HUYOHW1DPH LQWHUIDFH!! 6HUYOHW&RQILJ Most people never use this method. ,QDVHUYOHWFODVV MDYD[VHUYOHW6HUYOHW&RQILJ XVLQJ6HUYOHW&RQ¿J SDFNDJHFRPH[DPSOH LPSRUWMDYD[VHUYOHW LPSRUWMDYD[VHUYOHWKWWS LPSRUWMDYDLR SXEOLFFODVV7HVW,QLW3DUDPVH[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. DWWULEXWHVDQGOLVWHQHUV \RXDUHKHUH  8KRK,MXVWUHDOL]HG WKDWLQP\UHDODSS,·PXVLQJ -63WRUHQGHUWKHSDJH6R FDQD-63´VHHµDVHUYOHW·V LQLWSDUDPHWHUV" 0W_KIVI2;8OM\[MZ^TM\ QVQ\XIZIUM\MZ[' !3ERVLET#ONlGISFORSERVLETCONlGURATIONITDOESNTSAY *30#ONlG3OIFYOUWANTOTHERPARTSOFYOURAPPLICATION TOUSETHESAMEINFOYOUPUTINTHESERVLETSINIT PARAMETERSINTHE$$YOUNEEDSOMETHINGMORE ?PI\IJW]\\PM_Ia_MLQLQ\_Q\P\PMJMMZ IXX'?MXI[[ML\PMUWLMTQVNW\W\PM2;8 ][QVOIZMY]M[\I\\ZQJ]\M 7ECOULDDOITTHISWAY4HEREQUESTOBJECTLETSYOUSET ATTRIBUTESTHINKOFTHEMASANAMEVALUEPAIRWHERETHE VALUECANBEANYOBJECTTHATANYOTHERSERVLETOR*30THAT GETSTHEREQUESTCANUSE4HATMEANSANYSERVLETOR*30TO WHICHTHEREQUESTISFORWARDEDUSINGA2EQUEST$ISPATCHER 7ELLLOOKAT2EQUEST$ISPATCHERINDETAILATTHEENDOF THISCHAPTERBUTFORNOWALLWECAREABOUTISGETTINGTHE DATAINTHISCASETHEEMAILADDRESSTOTHEPIECESOFTHE WEBAPPTHATNEEDITRATHERTHANJUSTONESERVLET LQVLGHWKHGR3RVWPHWKRG 6WULQJFRORU UHTXHVWJHW3DUDPHWHU³FRORU´ %HHU([SHUWEH QHZ%HHU([SHUW /LVWUHVXOW EHJHW%UDQGVFRORU UHTXHVWVHW$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\\QVOIZMY]M[\ I\\ZQJ]\M _WZS[J]\WVTaNWZ\PM2;8\W _PQKPaW]NWZ_IZLML\PMZMY]M[\ 7ITHTHEBEERAPPITMADESENSETOSTORETHEMODELINFO FORTHECLIENTSREQUESTINTHEREQUESTOBJECTBECAUSETHENEXT STEPWASTOFORWARDTHEREQUESTTOTHE*30RESPONSIBLEFOR CREATINGTHEVIEW3INCETHAT*30NEEDEDTHEMODELDATA ANDTHEDATAWASRELEVANTTOONLYTHATPARTICULARREQUEST EVERYTHINGWASlNE "UTTHATDOESNTHELPUSWITHTHEEMAILADDRESSBECAUSEWE MIGHTNEEDTOUSEITFROMALLOVERTHEAPPLICATION4HERE ISAWAYTOHAVEASERVLETREADTHEINITPARAMETERSANDTHEN STORETHEMINAPLACEOTHERPARTSOFTHEAPPCOULDUSEBUT THENWEDHAVETOKNOWWHICHSERVLETWOULDALWAYSRUNlRST WHENTHEAPPISDEPLOYEDANDANYCHANGESTOTHEWEBAPP COULDBREAKTHEWHOLETHING.OTHATWONTDOEITHER %XW,UHDOO\ZDQW$//WKHSDUWV RIP\ZHEDSSWRKDYHDFFHVVWRWKH HPDLODGGUHVV:LWKLQLWSDUDPHWHUV ,KDYHWRFRQÀJXUHWKHPLQWKH''IRU HYHU\  VHUYOHWDQGWKHQKDYHDOOWKHVHUYOHWVPDNH WKHPDYDLODEOHIRUWKH-63V+RZERULQJ LVWKDW"1RWPDLQWDLQDEOHHLWKHU, QHHGVRPHWKLQJPRUH JOREDO  ,ZRQGHULI WKHUH·VVRPHWKLQJOLNH LQLWSDUDPHWHUVIRUWKH DSSOLFDWLRQ " LQLWSDUDPHWHUOLPLWDWLRQV 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. DWWULEXWHVDQGOLVWHQHUV \RXDUHKHUH  +WV\M`\QVQ\XIZIUM\MZ[\W\PMZM[K]M #ONTEXTINITPARAMETERSWORKJUSTLIKESERVLETINITPARAMETERSEXCEPT CONTEXTPARAMETERSAREAVAILABLETOTHEENTIREWEBAPPNOTJUST ASINGLESERVLET3OTHATMEANSANYSERVLETAND*30INTHEAPP AUTOMATICALLYHASACCESSTOTHECONTEXTINITPARAMETERSSOWEDONT HAVETOWORRYABOUTCONlGURINGTHE$$FOREVERYSERVLETAND WHENTHEVALUECHANGESYOUONLYHAVETOCHANGEITONEPLACE ,QWKH''ZHE[POÀOH VHUYOHW! VHUYOHWQDPH!%HHU3DUDP7HVWVVHUYOHWQDPH! VHUYOHWFODVV!7HVW,QLW3DUDPVVHUYOHWFODVV! VHUYOHW! FRQWH[WSDUDP! SDUDPQDPH!DGPLQ(PDLOSDUDPQDPH! SDUDPYDOXH!FOLHQWKHDGHUURU#ZLFNHGO\VPDUWFRPSDUDPYDOXH! FRQWH[WSDUDP! 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>. ,QWKHVHUYOHWFRGH RXWSULQWOQJHW6HUYOHW&RQWH[WJHW,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[WFRQWH[W JHW6HUYOHW&RQWH[W RXWSULQWOQFRQWH[WJHW,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\PMLQNNMZMVKMJM\_MMV[MZ^TM\QVQ\ XIZIUM\MZ[IVLKWV\M`\QVQ\XIZIUM\MZ[ (ERESAREVIEWOFTHEKEYDIFFERENCESBETWEENCONTEXTINITPARAMETERSANDSERVLET INITPARAMETERS0AYSPECIALATTENTIONTOTHEFACTTHATTHEYREBOTHREFERREDTOAS INITPARAMETERSEVENTHOUGHONLYSERVLETINITPARAMETERSHAVETHEWORDhINITvIN THE$$CONlGURATION &RQWH[WLQLWSDUDPHWHUV 6HUYOHWLQLWSDUDPHWHUV 'HSOR\PHQW 'HVFULSWRU ZHEDSS!  FRQWH[WSDUDP! SDUDPQDPH!IRRSDUDPQDPH! SDUDPYDOXH!EDUSDUDPYDOXH!  FRQWH[WSDUDP! RWKHUVWXIILQFOXGLQJ VHUYOHWGHFODUDWLRQV! ZHEDSS! :LWKLQWKHZHEDSS!HOHPHQWEXW127 ZLWKLQDVSHFLILFVHUYOHW!HOHPHQW VHUYOHW! VHUYOHWQDPH! %HHU3DUDP7HVWV VHUYOHWQDPH! VHUYOHWFODVV! 7HVW,QLW3DUDPV VHUYOHWFODVV! LQLWSDUDP! SDUDPQDPH!IRRSDUDPQDPH! SDUDPYDOXH!EDUSDUDPYDOXH!  LQLWSDUDP!  RWKHUVWXII! VHUYOHW! :LWKLQWKHVHUYOHW!HOHPHQWIRUHDFK VSHFLILFVHUYOHW JHW6HUYOHW&RQWH[WJHW,QLW3DUDPHWHU³IRR´ JHW6HUYOHW&RQ¿JJHW,QLW3DUDPHWHU³IRR´ 6HUYOHW&RGH $YDLODELOLW\ 7RDQ\VHUYOHWVDQG-63VWKDWDUHSDUW RIWKLVZHEDSS 7RRQO\WKHVHUYOHWIRUZKLFKWKHLQLWSDUDP!ZDV FRQILJXUHG $OWKRXJKWKHVHUYOHWFDQFKRRVHWRPDNHLWPRUH ZLGHO\DYDLODEOHE\VWRULQJLWLQDQDWWULEXWH FRQWH[WYVVHUYOHWLQLWSDUDPHWHUV 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. DWWULEXWHVDQGOLVWHQHUV \RXDUHKHUH   :DWFKLW <RXUHDOO\KDYHWRNHHSWKHVHVWUDLJKWRQ WKHH[DPDQGLW¶VWULFN\<RX0867NQRZ WKDWERWK6HUYOHW&RQ¿JDQG6HUYOHW&RQWH[W KDYHLQLWSDUDPHWHUVDQGERWKKDYHWKH VDPHJHWWHUPHWKRG²JHW,QLW3DUDPHWHU %87\RXDOVRKDYHWRNQRZWKDWFRQWH[W LQLWSDUDPHWHUVDUHVHWZLWKFRQWH[WSDUDP! QRWLQVLGHDVHUYOHW!HOHPHQWZKLOHVHUYOHW LQLWSDUDPHWHUVXVHLQLWSDUDP!LQVLGHWKH LQGLYLGXDOVHUYOHW!GHFODUDWLRQVLQWKH'' 'RQ·WFRQIXVH 6HUYOHW &RQÀJ  SDUDPHWHUVZLWK 6HUYOHW &RQWH[W  SDUDPHWHUV ; M Z ^ T M \ +WVNQO Q[WVMXMZ[MZ^TM\ ; M Z ^ T M \ +WV\M`\ Q[WVMXMZ_MJIXX 4HERESONLYONE3ERVLET#ONTEXTFORANENTIREWEBAPPANDALLTHE PARTSOFTHEWEBAPPSHAREIT"UTEACHSERVLETINTHEAPPHASITSOWN 3ERVLET#ONlG4HE#ONTAINERMAKESA3ERVLET#ONTEXTWHENAWEB APPISDEPLOYEDANDMAKESTHECONTEXTAVAILABLETOEACH3ERVLETAND *30WHICHBECOMESASERVLETINTHEWEBAPP  &RQWDLQHUUHDGVWKH''DQG FUHDWHVDQDPHYDOXH6WULQJSDLU IRUHDFKFRQWH[WSDUDP!  &RQWDLQHUFUHDWHVDQHZLQVWDQFH RI6HUYOHW&RQWH[W  &RQWDLQHUJLYHVWKH 6HUYOHW&RQWH[WDUHIHUHQFHWR HDFKQDPHYDOXHSDLURIWKH FRQWH[WLQLWSDUDPHWHUV  (YHU\VHUYOHWDQG-63GHSOR\HG DVSDUWRIDVLQJOHZHEDSS KDVDFFHVVWRWKDWVDPH 6HUYOHW&RQWH[W :HEDSSLQLWLDOL]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\! MVSVHW3URSHUW\ 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  :DWFKLW ,I\RXUDSSOLFDWLRQLVGLVWULEXWHGDFURVV PXOWLSOHVHUYHUVSUREDEO\LQDFOXVWHUHG HQYLURQPHQW\RXUZHEDSSUHDOO\&28/' KDYHPRUHWKDQRQH6HUYOHW&RQWH[W$ 6HUYOHW&RQWH[WLVRQHSHUDSSEXWRQO\LI WKHDSSLVLQDVLQJOH-90 ,QDGLVWULEXWHGHQYLURQPHQW\RX¶OOKDYHRQH 6HUYOHW&RQWH[WSHU-901RZFKDQFHVDUH WKLVZRQ¶WFUHDWHSUREOHPVEXWLI\RXKDYHD GLVWULEXWHGZHEDSS\RXEHWWHUFRQVLGHUWKH FRQVHTXHQFHVRIKDYLQJGLIIHUHQWFRQWH[WV IRUHDFK-90 ,IWKHDSSLV GLVWULEXWHGWKHUH·V RQH6HUYOHW&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  7HATSWITHTHEINCONSISTENTNAMINGSCHEME (OWCOMETHE$$ELEMENTSARECONTEXTPARAMAND INITPARAMBUTINTHESERVLETCODE"/4(USETHE GET)NIT0ARAMETERMETHOD $  4HEYDIDNTASKUSTOHELPTHEMCOMEUPWITHTHE NAMES)FTHEYHADOFCOURSEWEDHAVESAIDITSHOULDBE GET)NIT0ARAMETERANDGET#ONTEXT0ARAMETERTOMATCH THE8-,ELEMENTSINTHE$$/RTHEYCOULDHAVEUSED DIFFERENT8-,ELEMENTSPERHAPSSERVLETINITPARAM ANDCONTEXTINITPARAM"UTNOTHATWOULDHAVESUCKED ALLTHEFUNOUTOFTRYINGTOKEEPTHEMSTRAIGHT 4  7HYWOULD)EVERUSEINITPARAMANYWAY 7OULDNT)ALWAYSWANTTOUSECONTEXTPARAMSO THATOTHERPARTSOFMYAPPCOULDREUSETHEVALUESAND )WONTHAVETODUPLICATE8-,CODEFOREVERYSERVLET DECLARATION $  )TALLDEPENDSONWHICHPARTOFYOURAPPIS SUPPOSEDTOSEETHEVALUE9OURAPPLICATIONLOGICMIGHT REQUIREYOUTOUSEAVALUETHATYOUWANTTORESTRICTTO ONLYANINDIVIDUALSERVLET"UTTYPICALLYDEVELOPERSFIND APPWIDECONTEXTINITPARAMETERSALOTMOREHELPFULTHAN SERVLETSPECIFICSERVLETINITPARAMETERS0ERHAPSTHEMOST COMMONUSEOFACONTEXTPARAMETERISSTORINGDATABASE LOOKUPNAMES9OUDWANTALLPARTSOFYOURAPPTOHAVE ACCESSTOTHECORRECTNAMEANDWHENITCHANGESYOUWANT TOCHANGEITINONLYONEPLACE 4  7HATHAPPENSIF)GIVEACONTEXTINITPARAMETER THESAMENAMEASASERVLETINITPARAMETERINTHESAME WEBAPP $  4HEMOLECULARSIZEDBLACKHOLEMIRACULOUSLY CREATEDINARESEARCHFACILITYIN.EW*ERSEYWILLSLIPFROMITS CONTAINMENTlELDPLUMMETTOTHEEARTHSCOREANDDESTROY THEPLANET /RMAYBENOTHINGBECAUSETHERESNONAMESPACECONmICT SINCEYOUGETTHEPARAMETERSTHROUGHTWODIFFERENTOBJECTS 3ERVLET#ONTEXTOR3ERVLET#ONlG 4  )FYOUMODIFYTHE8-,TOCHANGETHEVALUEOFAN INITPARAMETEREITHERSERVLETORCONTEXTWHENDOESTHE SERVLETORTHERESTOFTHEWEBAPPSEETHECHANGE $  /.,9WHENTHEWEBAPPISREDEPLOYED 2EMEMBERWETALKEDABOUTTHISBEFORETHESERVLETIS INITIALIZEDONLYONCEATTHEBEGINNINGOFITSLIFEANDTHATS WHENITSGIVENITS3ERVLET#ONFIGAND3ERVLET#ONTEXT4HE #ONTAINERREADSTHEVALUESFROMTHE$$WHENITCREATES THOSETWOOBJECTSANDSETSTHEVALUES 4  #ANT)GETAROUNDTHISBYSETTINGTHEVALUESAT RUNTIME3URELYTHERESAN!0)THATLLLETMECHANGE THOSEVALUESDYNAMICALLY $  .OTHERESNOT,OOKIN3ERVLET#ONTEXTOR 3ERVLET#ONFIGANDYOULLFINDAGETTERGET)NIT0ARAMETER BUTYOUWONTFINDASETTER4HERESNOSET)NIT0ARAMETER 4 4HATSLAME $  4HESEAREINITPARAMETERS)NITFROMTHE,ATINWORD INITIALIZATION)FYOUTHINKOFTHEMPURELYASDEPLOYTIME CONSTANTSYOULLHAVETHERIGHTPERSPECTIVE)NFACTTHATS SOIMPORTANTWEREGOINGTOSAYITAGAININABOLDERWAY 7KLQNRILQLWSDUDPHWHUVDV GHSOR\WLPHFRQVWDQWV <RXFDQ JHW WKHPDWUXQWLPH EXW\RXFDQ·W VHW WKHP7KHUH·V QR VHW ,QLW3DUDPHWHU VHUYOHWDQGFRQWH[WLQLWSDUDPHWHUV 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. DWWULEXWHVDQGOLVWHQHUV \RXDUHKHUH   6RPHSHRSOHXVHWKHSKUDVH³LQLWSDUDPHWHU´WRPHDQ³VHUYOHW LQLWSDUDPHWHU´DQGWKH\XVH³FRQWH[WSDUDPHWHU´RUHYHQ³DS SOLFDWLRQSDUDPHWHU´WRPHDQ³FRQWH[WLQLWSDUDPHWHU´6RHYHQ WKRXJK%27+DUHLQLWLDOL]DWLRQSDUDPHWHUVDQGERWKFRPHIURP WKHJHW,QLW3DUDPHWHUPHWKRGUHPHPEHUWKDWRQO\6(59/(7LQLW SDUDPHWHUVDUHOLVWHGLQWKH''DVLQLWSDUDPHWHUVVRWKHSKUDVH ³LQLWSDUDPHWHU´PHDQV³VHUYOHWLQLWSDUDPHWHU´E\GHIDXOW  :HNQRZWKDWDVDGHYHORSHU\RX¶OOEHNLQGHUWRRWKHUVDQG DOZD\VVD\H[SOLFLWO\ZKHWKHUDQLQLWSDUDPHWHULVDVHUYOHWLQLW SDUDPHWHURUDFRQWH[WLQLWSDUDPHWHU ,I\RXVHH´LQLWSDUDPHWHUµZLWKRXW NQRZLQJLILWPHDQV VHUYOHW RU FRQWH[W LQLWSDUDPHWHUDVVXPH VHUYOHW FRQWH[WSDUDP! FRQWH[WSDUDP! VHUYOHW! VHUYOHW! ZHEDSS! ZHEDSS! SDUDPQDPH! VHUYOHWQDPH! SDUDPQDPH! VHUYOHWFODVV! VHUYOHWFODVV! SDUDPYDOXH! SDUDPYDOXH! IRR FRPZLFNHGO\VPDUW%HHU7HVWHU EDU %HHU7HVWVHUYOHWQDPH! 2EARRANGETHEMAGNETSTOFORMA$$THATDECLARESA PARAMETERTHATMATCHESTHESERVLETCODE 9OUWONTUSEALLOFTHEMAGNETS .OTEWHENYOUSEEWEBAPPREMEMBERTHAT THISISOURSHORTCUTTOSAVESPACEONTHEPAGE9OU CANTDEPLOYAWEBXMLFILEUNLESSTHEWEBAPP TAGHASALLTHEATTRIBUTESITNEEDS ([HUFLVH &RGH0DJQHWV JHW6HUYOHW&RQWH[WJHW,QLW3DUDPHWHU³IRR´ LQLWSDUDP! LQLWSDUDP! VHUYOHWSDUDP! VHUYOHWSDUDP! 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 KIVaW]LW_Q\P aW]Z;MZ^TM\+WV\M`\' !3ERVLET#ONTEXTISA*30ORSERVLETSCONNECTION TOBOTHTHE#ONTAINERANDTHEOTHERPARTSOFTHE WEBAPP(EREARESOMEOFTHE3ERVLET#ONTEXT METHODS7EPUTTHEONESYOUSHOULDKNOWFOR THEEXAMINBOLD Get init parameters and get/set attributes. Get info about the server/container. WeÕll talk about RequestDispatcher later in the chapter. JHW,QLW3DUDPHWHU6WULQJ JHW,QLW3DUDPHWHU1DPHV JHW$WWULEXWH6WULQJ JHW$WWULEXWH1DPHV VHW$WWULEXWH6WULQJ UHPRYH$WWULEXWH6WULQJ JHW0DMRU9HUVLRQ JHW6HUYHU,QIR JHW5HDO3DWK6WULQJ JHW5HVRXUFH$V6WUHDP6WULQJ JHW5HTXHVW'LVSDWFKHU6WULQJ ORJ6WULQJ PRUHPHWKRGV 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[VHUYOHW6HUYOHW&RQWH[W WKH6HUYOHW&RQWH[W 6 H U Y O H W & R Q W H [ W 6HUYOHW <RXFRPSOHWHPH %HIRUH,FDPHLQWR \RXUOLIH\RXZHUH MXVWDQRWKHUORVHUREMHFW LQVWHDGRIDUHDOVHUYOHW 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  :DWFKLW ,IRXUDSSOLFDWLRQLVGLVWULEXWHGDFURVV PXOWLSOHVHUYHUVSUREDEOLQDFOXVWHUHG HQYLURQPHQWRXUZHEDSSUHDOO&28/' KDYHPRUHWKDQRQH6HUYOHW&RQWH[W$ 6HUYOHW&RQWH[WLVRQHSHUDSSEXWRQOLI WKHDSSLVLQDVLQJOH-90 ,QDGLVWULEXWHGHQYLURQPHQWRX¶OOKDYHRQH 6HUYOHW&RQWH[WSHU-901RZFKDQFHVDUH WKLVZRQ¶WFUHDWHSUREOHPVEXWLIRXKDYHD GLVWULEXWHGZHEDSSRXEHWWHUFRQVLGHUWKH FRQVHTXHQFHVRIKDYLQJGLIIHUHQWFRQWH[WV IRUHDFK-90 ,IWKHDSSLV GLVWULEXWHGWKHUH·V RQH6HUYOHW&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. ,QDVHUYOHWFODVV MDYD[VHUYOHW6HUYOHW&RQILJ XVLQJ6HUYOHW&RQ¿J SDFNDJHFRPH[DPSOH LPSRUWMDYD[VHUYOHW LPSRUWMDYD[VHUYOHWKWWS LPSRUWMDYDLR SXEOLFFODVV7HVW,QLW3DUDPVH[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  7HATSWITHTHEINCONSISTENTNAMINGSCHEME (OWCOMETHE$$ELEMENTSARECONTEXTPARAM AND INITPARAMBUTINTHESERVLETCODE"/4(USETHE GET)NIT0ARAMETERMETHOD $  4HEYDIDNTASKUSTOHELPTHEMCOMEUPWITHTHE NAMES)FTHEYHADOFCOURSEWEDHAVESAIDITSHOULDBE GET)NIT0ARAMETER AND GET#ONTEXT0ARAMETERTOMATCH THE8-,ELEMENTSINTHE$$/RTHEYCOULDHAVEUSED DIFFERENT8-,ELEMENTSPERHAPSSERVLETINITPARAM AND CONTEXTINITPARAM"UTNOTHATWOULDHAVESUCKED ALLTHEFUNOUTOFTRYINGTOKEEPTHEMSTRAIGHT 4  7HYWOULD)EVERUSEINITPARAMANYWAY 7OULDNT)ALWAYSWANTTOUSECONTEXTPARAMSO THATOTHERPARTSOFMYAPPCOULDREUSETHEVALUES AND )WONTHAVETODUPLICATE8-,CODEFOREVERYSERVLET DECLARATION $  )TALLDEPENDSONWHICHPARTOFYOURAPPIS SUPPOSEDTOSEETHEVALUE9OURAPPLICATIONLOGICMIGHT REQUIREYOUTOUSEAVALUETHATYOUWANTTORESTRICTTO ONLYANINDIVIDUALSERVLET"UTTYPICALLYDEVELOPERSFIND APPWIDECONTEXTINITPARAMETERSALOTMOREHELPFULTHAN SERVLETSPECIFICSERVLETINITPARAMETERS0ERHAPSTHEMOST COMMONUSEOFACONTEXTPARAMETERISSTORINGDATABASE LOOKUPNAMES9OUDWANTALLPARTSOFYOURAPPTOHAVE ACCESSTOTHECORRECTNAME AND WHENITCHANGESYOUWANT TOCHANGEITINONLYONEPLACE 4  7HATHAPPENSIF)GIVEACONTEXTINITPARAMETER THESAMENAMEASASERVLETINITPARAMETERINTHESAME WEBAPP $  4HEMOLECULARSIZEDBLACKHOLEMIRACULOUSLY CREATEDINARESEARCHFACILITYIN.EW*ERSEYWILLSLIPFROMITS CONTAINMENTlELDPLUMMETTOTHEEARTHSCORE AND DESTROY THEPLANET /RMAYBENOTHINGBECAUSETHERESNONAMESPACECONmICT SINCEYOUGETTHEPARAMETERSTHROUGHTWODIFFERENTOBJECTS 3ERVLET#ONTEXTOR3ERVLET#ONlG 4  )FYOUMODIFYTHE8-,TOCHANGETHEVALUEOFAN INITPARAMETEREITHERSERVLETORCONTEXTWHENDOESTHE SERVLETORTHERESTOFTHEWEBAPPSEETHECHANGE $  /.,9WHENTHEWEBAPPISREDEPLOYED 2EMEMBERWETALKEDABOUTTHISBEFORETHESERVLETIS INITIALIZEDONLYONCEATTHEBEGINNINGOFITSLIFE AND THATS WHENITSGIVENITS3ERVLET#ONFIG AND 3ERVLET#ONTEXT4HE #ONTAINERREADSTHEVALUESFROMTHE$$WHENITCREATES THOSETWOOBJECTS AND SETSTHEVALUES 4  #ANT)GETAROUNDTHISBYSETTINGTHEVALUESAT RUNTIME3URELYTHERESAN!0)THATLLLETMECHANGE THOSEVALUESDYNAMICALLY $  .OTHERESNOT,OOKIN3ERVLET#ONTEXTOR 3ERVLET#ONFIG AND YOULLFINDAGETTERGET)NIT0ARAMETER BUTYOUWONTFINDASETTER4HERESNOSET)NIT0ARAMETER 4 4HATSLAME $  4HESEAREINITPARAMETERS)NITFROMTHE,ATINWORD INITIALIZATION)FYOUTHINKOFTHEMPURELYASDEPLOYTIME CONSTANTSYOULLHAVETHERIGHTPERSPECTIVE)NFACTTHATS SOIMPORTANTWEREGOINGTOSAYITAGAININABOLDERWAY 7KLQNRILQLWSDUDPHWHUVDV GHSORWLPHFRQVWDQWV <RXFDQ JHW WKHPDWUXQWLPH EXWRXFDQ·W VHW WKHP7KHUH·V QR VHW ,QLW3DUDPHWHU VHUYOHWDQGFRQWH[WLQLWSDUDPHWHUV 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,

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

TỪ KHÓA LIÊN QUAN