原文翻译.docx
- 文档编号:28148115
- 上传时间:2023-07-08
- 格式:DOCX
- 页数:21
- 大小:113.46KB
原文翻译.docx
《原文翻译.docx》由会员分享,可在线阅读,更多相关《原文翻译.docx(21页珍藏版)》请在冰豆网上搜索。
原文翻译
TheAnatomyofa
JavaServerPage
TheJavaPlatform,EnterpriseEdition5(JavaEE5)hastwodifferentbutcomplementary
technologiesforproducingdynamicwebcontentinthepresentationtier—namelyJavaServletandJavaServerPages(JSP).
JavaServlet,thefirstofthesetechnologiestoappear,wasinitiallydescribedasextensions
toawebserverforproducingdynamicwebcontent.JSP,ontheotherhand,isanewertechnology
butisequallycapableofgeneratingthesamedynamiccontent.However,thewayinwhichaservletandaJSPpageproducetheircontentisfundamentallydifferent;servletsembedcontentintologic,whereasJSPpagesembedlogicintocontent.
JSPpagescontainmarkupinterlacedwithspecialJSPelementsthatprovidelogicforcontrolling
thedynamiccontent.ServletsarebuiltusingJavaclassesthatcontainstatementsto
outputmarkupcode.Ofthesetwoparadigms,JSPpagesarepreferredforpresentingdynamic
contentinthepresentationtierduetotheirgreaterreadability,maintainability,andsimplicity.FurtherincreasingthesimplicityandeaseofuseofJSPpageswasoneofthemainobjectivesoftheJSP2.0specification,whichincludedseveralnewfeaturestomakeiteasierthanevertoembraceJSPtechnology,especiallyfordeveloperswhoaren’tfluentintheJavasyntax.
Theinclusionofanewexpressionlanguage(EL)enablesJavaScript-styleJSPcodetobe
embeddedwithinpages,whichmakesitmucheasierforwebdevelopersnotfamiliarwiththe
JavasyntaxtounderstandtheJSPlogic.AlibraryofstandardactionsknownastheJavaServer
PagesStandardTagLibrary(JSTL)isalsoincludedtoprovideahostofuseful,reusableactions
suchasconditionalstatements,iteration,andXMLintegrationtonameafew.Theseactions
areapplicableinsomeshapeorformtomostJSPwebapplications,andtheirusewillgreatly
improvethereliabilityandeaseofdevelopmentforJSPpageauthors.Customactions(also
knownascustomtags)alsobenefitfromchangesintheJSPspecification,andit’snowpossible
towriteacustomactionentirelyinJSPsyntaxinsteadofJavasyntax!
BeforeYouBegin
TobeginexaminingthebasicsofJSPtechnology,it’sessentialthatyouhaveacursoryfamiliarity
withthealternativeandcomplementarypresentation-tierwebcomponent,Javaservlets.
Thenextchapterwilldiscussservletsinmoredetail.
JavaServlets
Asmentionedearlier,servletscanmostsimplybedescribedascustomweb-serverextensions,
whosejobsaretoprocessrequestsanddynamicallyconstructappropriateresponses.Inpractice,
suchresponsesareusuallyreturnedintheformofHTMLorXMLandaretheresultofa
usermakinganHTTPrequestviaawebbrowser.Servlettechnologyhasbeenanextremely
popularchoiceforbuildingdynamicwebapplicationssuchase-commercesites,onlinebanking,
andnewsportals,forreasonsofsimplicity,extensibility,efficiency,andperformanceover
alternativetechnologiessuchasCommonGatewayInterface(CGI)scripts.
Someofthemostbasicadvantagesofservlettechnologyareasfollows:
•Simplicity:
Servletsareeasytowrite,andallthecomplicatedthreadingandrequest
delegatingismanagedbytheservletcontainer.
•Extensibility:
TheServletAPIiscompletelyprotocolindependent.
•Efficiency:
UnlikeCGIscripts,theexecutionofaservletdoesn’trequireaseparate
processtobespawnedbythewebservereachtime.
•Performance:
Servletsarepersistent,andtheirlifecycleextendsbeyondthatofeach
HTTPrequest.
ServletsaresimplyJavaclassesthatinheritfromthejavax.servlet.Servletinterface.
Theseservletsarecompiledanddeployedinsideaservletcontainer,whichisaJavaenvironment
thatmanagesthelifecycleoftheservletanddealswiththelower-levelsocket-based
communication.TheservletcontainermaybepartofanexistingJava-enabledwebserver
itselformaybeusedasastand-aloneproductthatisintegratedwithathird-partywebserver.
TheservletReferenceImplementationcontainer,ApacheJakartaTomcatforexample,maybe
usedasastand-alonewebserverorasaseparateservletcontainerinsidealargercommercial
webserversuchastheApachewebserver.
Servletsaretypicallyusedforreturningtext-basedcontentsuchasHTML,XML,WML,
andsoon.However,theyareequallyathomereturningbinarydatasuchasimagesorserialized
Javaobjects,whichareoftenusedbyfurtherservletstogeneratesomeappropriate
dynamicresponse.
JSPUndertheHood
AJSPpageissimplyaregulartextfilethatcontainsmarkup(usuallyHTML)suitablefor
displayinsideabrowser.WithinthismarkuparespecialJSPelementsthatyou’lllearnmore
aboutlater.Theseareusedtoprovideprocessinglogicthatenablesdynamiccontenttobe
producedonarequest-by-requestbasis.
InJSPterms,anymarkupthatisn’taJSPelementisknownastemplatetext,andthisreally
canbeanyformoftext-basedcontentsuchasHTML,WML,XML,orevenplaintext!
Ofcourse
themixtureofJSPelementsandtemplatetextcannotsimplybesenttothebrowserwithout
anyformofprocessingbytheserver.WementionedearlierhowJSPtechnologyisanextension
ofservlettechnology,andsoyouprobablywon’tbesurprisedtolearnthateachJSPpage
is,infact,convertedintoaservletinordertoprovidethisprocessinglogic.Figure1-1showsaJSPpagebeingtranslatedandcompiledintoaservletinresponsetoarequest.ThisservletisknownastheJSPimplementationservlet.
Figure1-1.TheJSPcontainertranslatesandcompilestheJSPsourceintoanimplementation
class,whichisusedtoprocessallrequests.
ArequestforaJSPpageishandledinitiallybythewebserver,whichthendelegatesthe
requesttotheJSPcontainer.TheJSPenginewilltranslatethecontentsoftheJSPintoits
implementationservlet,whichthecontainerthenusestoservicetherequest.UsuallyaJSP
containerwillcheckwhetherthecontentsofaJSPpagehavechangedbeforedecidingifit
needstoretranslatethepageinresponsetoarequest.Thisfeaturecanmakeon-the-spot
changestoJSPpageseasybecausethenextrequestwillautomaticallycausearetranslation
andthemostup-to-datecontentwillbereturned.Comparethiswithapurelyservlet-based
approach,whichwouldrequiretheservletcontainertobeshutdowninordertohavethenecessary
changesmade,suchasrecompilation,testing,andfinally,arestart!
Let’stakeacloserlookattheprocessoftakingaplainJSPtextfileandturningitintoa
dynamicwebcomponent;thisprocessisalsoknownastheJSPlifecycle.
TheJSPLifeCycle
Asyou’vejustseen,JSPpagesdon’tdirectlyreturncontenttotheclientbrowserthemselves.
Instead,theyrelyonsomeinitialserver-sideprocessingthatconvertstheJSPpageintotheJSPpageimplementationclass(seeFigure1-2),whichhandlesallrequestsmadeoftheJSP.
Figure1-2.Beforeprocessingarequest,thecontainerdetermineswhethertheJSPsourceisnew
orhaschanged.Ifso,thecontainertranslatesandcompilestheJSPpageintoaservletclass,orpageimplementationclass,beforepassingtherequesttotheservletforprocessing.
AsyoucanseeinFigure1-2,theJSPservletcontainerdecideswhethertheJSPpagehasbeentranslatedbefore.Ifnot,theJSPcontainerstartsthetranslationphasetogeneratetheJSPpageimplementationservlet,whichisthencompiled,loadedandinitialized,andusedtoservicetherequest.IftheJSPcontainerdetectsthataJSPpagehasalreadybeentranslatedandhasn’tsubsequentlychanged,therequestissimplyservicedbytheimplementationservletthatalreadyexistsinsidethecontainer.
ThelifecycleofaJSPpagecanbesplitintofourphases:
translation,initialization,
execution,andfinalization.
Translation
ThefirststageinthelifecycleofaJSPpageisknownasthetranslationphase.
WhenarequestisfirstmadeforaJSPpage(assumingithasn’tbeenprecompiled),theJSPenginewillexaminetheJSPfiletocheckthatit’scorrectlyformedandthattheJSPsyntaxiscorrect.Ifthesyntaxcheckissuccessful,theJSPenginewilltranslatetheJSPpageintoitspageimplementationclass,whichtakestheformofastandardJavaservlet.Afterthepage’simplementationservlethasbeencreated,itwillbecompiledintoaclassfilebytheJSPengineandwillbereadyforuse.Eachtimeacontainerreceivesarequest,itfirstcheckswhethertheJSPfilehaschangedsinceitwaslasttranslated.Ifithas,it’sretranslatedsothattheresponseisalwaysgeneratedbythemostup-to-dateimplementationoftheJSPfile.
Initialization
Afterthetranslationphasehasbeencompleted,theJSPenginewillneedtoloadthegeneratedclassfileandcreateaninstanceoftheservletinordertocontinueprocessingtheinitialrequest.
Therefore,theJSPengineworksverycloselywiththeservletcontainerandtheJSPpageimplementationservletandwilltypicallyloadasingleinstanceoftheservletintomemory.ThissingleinstancewillbeusedtoserviceallrequestsfortheJSPpage.Inareal-worldwebapplication,thoserequestswillmostlikelyhappenconcurrently,soyourJSPpagemustbemultithreaded.
PriortotheServlet2.5specification,theJavaServletspecificationprovidedtwoseparatethreadingmodelsthatcouldbeusedforaservlet.Themodelsdeterminewhethersingleormultipleinstancesofaservletcanexist.Thedefaultthreadingmodelforanyservletisthemultithreadedonethatrequiresnoadditionalworkforthedeveloper.Inthismodel,thecontainercreatesonlyasingleinstanceoftheservletclassandsendsmultiplerequeststotheinstanceconcurrently.
Toselectthesingle-threadedmodelforyourJSP,youmustsetanattributeofthepagedirectiveca
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 原文 翻译
![提示](https://static.bdocx.com/images/bang_tan.gif)