将Servlet和JSP组合使-毕业论文外文翻译xWord下载.docx
《将Servlet和JSP组合使-毕业论文外文翻译xWord下载.docx》由会员分享,可在线阅读,更多相关《将Servlet和JSP组合使-毕业论文外文翻译xWord下载.docx(18页珍藏版)》请在冰点文库上搜索。
EarlydynamicpagesmainlyCGI(CommonGatewayInterface,publicGatewayInterface)technology,youcanusedifferentlanguagesoftheCGIprograms,suchasVB,C
/C++orDelphi,andsoon.ThoughthetechnologyofCGIisdevelopedandpowerful,
becauseofdifficultiesinprogramming,andlowefficiency,modifycomplexshortcomings,
11
itisgraduallybeingreplacedbythetrend.Ofallthenewtechnology,JSP/Servletwithmoreefficientandeasytoprogram,morepowerful,moresecureandhasagoodportability,theyhavebeenmanypeoplebelievethatthefutureisthemostdynamicsiteofthefuturedevelopmentoftechnology.
SimilartoCGI,Servletsupportrequest/responsemodel.Whenacustomersubmitarequesttotheserver,theserverpresentedtherequestServlet,Servletresponsibleforhandlingrequestsandgeneratearesponse,andthengavetheserver,andthenfromtheserversenttothecustomer.AndtheCGIisdifferent,Servletnotgenerateanewprocess,butwithHTTPServeratthesameprocess.Itthreadsthroughtheuseoftechnology,reducetheservercosts.Servlethandlingoftherequestprocessisthis:
Whenreceivedfromtheclient'
srequest,callingservicemethods,themethodofServletarrivalofthefirstjudgementiswhattypeofrequest(GET/POST/HEAD…),thencallstheappropriatetreatment(DoGet/doPost/doHead…)andgeneratearesponse.
Althoughsuchacomplex,infact,simplysaidtoServletisaJavaclass.AndthegeneralcategoryofthedifferenceisthatthistypeoperatinginaServletcontainer,whichcanprovidesessionmanagementandtargetedlife-cyclemanagement.SothatwhenyouusetheServlet,youcangetallthebenefitsoftheJavaplatform,includingthesafetyofthemanagement,useJDBCaccessthedatabaseandcross-platformcapability.Moreover,Servletusingthread,andcandevelopmoreefficientWebapplications.
JSPtechnologyisakeyJ2EEtechnology,itatahigherlevelofabstractionofa
Servlet.ItallowsconventionalstaticanddynamicHTMLcontentgeneratedbycombininganHTMLpagelookslike,butasaServlettorun.TherearemanycommercialapplicationserversupportJSPtechnology,suchasBEAWebLogic,IBMWebSphere,JRun,andsoon.JSPandServletusemorethansimple.IfyouhaveaJSPsupportforWebservers,andaJSPdocument,youcanputitFangdaoanystaticHTMLfilescanbeplaced,donothavetocompile,donothavetopack,donothavetoClassPathsettings,youcanvisitasordinaryWebItdidvisit,theserverwillautomaticallyhelpyoutodootherwork.
JSPdocumentlookslikeanordinarystaticHTMLdocument,butinsidecontainsanumberofJavacode.Ituses.Jspthesuffix,usedtotelltheserverthisdocumentinneedofspecialtreatment.WhenwevisitaJSPpage,thedocumentwillfirstbetranslatedintoaJSPengineJavasourcefiles,isactuallyaServlet,andcompiler,andthen,likeotherServlet,fromServletenginetohandle.Servletengineofthistypeloading,handlingrequestsfromcustomers,andtheresultsreturnedtothecustomer,asshownbelow:
Figure1:
CallingtheprocessofJSPpages
Afteranothervisitthispagetothecustomer,aslongasthepapertherehavebeennochanges,JSPenginehasbeenloadeddirectlycalltheServlet.Ifyouhavealreadybeenmodified,itwillbeonceagaintheimplementationoftheaboveprocess,translate,compileandload.Infact,thisistheso-called"
firstpersontopunishment."
Becausewhenthefirstvisittotheimplementationofaseriesoftheaboveprocess,sowillspendsometimeaftersuchavisitwouldnot.
JavaservletsofferapowerfulAPIthatprovidesaccesstoalltheinformationabouttherequest,thesession,andtheapplication.combiningJSPwithservletsletsyouclearlyseparatetheapplicationlogicfromthepresentationoftheapplication;
inotherwords,itletsyouusethemostappropriatecomponenttypefortherolesofModel,ViewandController.
Servlets,Filters,andListeners
AservletisaJavaclassthatextendsaserverwithfunctionalityforprocessingarequestandproducingaresponse.It'
simplementedusingtheclassesandinterfacesdefinedbytheServletAPI.TheAPIconsistsoftwopackages:
thejavax.servletpackagecontainsclassesandinterfacesthatareprotocol-independent,whilethejavax.servlet.httppackageprovidesHTTP-specificextensionsandutilityclasses.
Whatmakesaservletaservletisthattheclassimplementsaninterfacenamedjavax.servlet.Servlet,eitherdirectlyorbyextendingoneofthesupportclasses.Thisinterfacedefinesthemethodsusedbythewebcontainertomanageandinteractwiththe
servlet.AservletforprocessingHTTPrequeststypicallyextendsthejavax.servlet.http.HttpServletclass.ThisclassimplementstheServletinterfaceandprovidesadditionalmethodssuitableforHTTPprocessing.
ServletLifecycle
Thewebcontainermanagesallaspectsoftheservlet'
slifecycle.Itcreatesaninstanceoftheservletclasswhenneeded,passesrequeststotheinstanceforprocessing,andeventuallyremovestheinstance.ForanHttpServlet,thecontainercallsthefollowingmethodsattheappropriatetimesintheservletlifecycle.
BesidesthedoGet()anddoPost()methods,therearemethodscorrespondingtotheotherHTTPmethods:
doDelete(),doHead(),doOptions(),doPut(),anddoTrace().Typicallyyoudon'
timplementthesemethods;
theHttpServletclassalreadytakescareofHEAD,OPTIONS,andTRACErequestsinawaythat'
ssuitableformostservlets,andtheDELETEandPUTHTTPmethodsarerarelyusedinawebapplication.
It'
simportanttorealizethatthecontainercreatesonlyoneinstanceofeachservlet.Thismeansthattheservletmustbethreadsafe--abletohandlemultiplerequestsatthesametime,eachexecutingasaseparatethreadthroughtheservletcode.Withoutgettinglostindetails,yousatisfythisrequirementwithregardstoinstancevariablesifyoumodifythereferencedobjectsonlyintheinit()anddestroy()methods,andjustreadthemintherequestprocessingmethods.
CompilingandInstallingaServlet
Tocompileaservlet,youmustfirstensurethatyouhavetheJARfilecontainingallServletAPIclassesintheCLASSPATHenvironmentvariable.TheJARfileisdistributedwithallwebcontainers.Tomcatincludesitinafilecalledservlet.jar,locatedinthecommon/libdirectory.OnaWindowsplatform,youincludetheJARfileintheCLASSPATH.
.ReadingaRequest
OneoftheargumentspassedtothedoGet()anddoPost()methodsisanobjectthatimplementstheHttpServletRequestinterface.Thisinterfacedefinesmethodsthatprovideaccesstoawealthofinformationabouttherequest.
GeneratingaResponse
Besidestherequestobject,thecontainerpassesanobjectthatimplementstheHttpServletResponseinterfaceasanargumenttothedoGet()anddoPost()methods.Thisinterfacedefinesmethodsforgettingawriterorstreamfortheresponsebody.Italsodefinesmethodsforsettingtheresponsestatuscodeandheaders.
UsingFiltersandListeners
Theservletspecificationdefinestwocomponenttypesbesideservlets:
filtersandlisteners.ThesetwotypeswereintroducedintheServlet2.3specification,soifyou'
reusingacontainerthatdoesn'
tyetsupportthisversionofthespecification,I'
mafraidyou'
re
outofluck.
Filters
Afilterisacomponentthatcaninterceptarequesttargetedforaservlet,JSPpage,orstaticpage,aswellastheresponsebeforeit'
ssenttotheclient.Thismakesiteasytocentralizetasksthatapplytoallrequests,suchasaccesscontrol,logging,andchargingforthecontentortheservicesofferedbytheapplication.Afilterhasfullaccesstothebodyandheadersoftherequestandresponse,soitcanalsoperformvarioustransformations.OneexampleiscompressingtheresponsebodyiftheAccept-Languagerequestheaderindicatesthattheclientcanhandleacompressedresponse.
AfiltercanbeappliedtoeitheraspecificservletortoallrequestsmatchingaURLpattern,suchasURLsstartingwiththesamepathelementsorhavingthesameextension.
Listeners
Listenersallowyourapplicationtoreacttocertainevents.PriortoServlet2.3,youcouldhandleonlysessionattributebindingevents(triggeredwhenanobjectwasaddedorremovedfromasession).Youcoulddothisbylettingtheobjectsavedasasessionattribute(usingtheHttpSession.setAttribute()method)implementtheHttpSessionBindingListenerinterface.Withthenewinterfacesintroducedinthe2.3versionofthespecification,youcan