Vous êtes sur la page 1sur 3

Chapter 43 JavaServer Pages

1. AJSPpagemustfirstbeprocessedbyaWebserverbefore
itcanbedisplayedinaWebbrowser.TheWebservermust
supportJSP,andtheJSPpagemustbestoredinafile
witha.jspextension.TheWebservertranslatesthe.jsp
fileintoaJavaservlet,compilestheservlet,and
executesit.Theresultoftheexecutionissenttothe
browserfordisplay.Figure35.2showshowaJSPpageis
processedbyaWebserver.

2. ForaJSPfiletorunfromTomcat,itmustbeplacedin
c:\jakartatomcat4.1.27\wepapps\examples\jsp(oryoucan
specifyanotherlocationintheTomcatconfiguration
file.)

3. YoucannotdisplayaJSPfilebysimplytypingthefile
name,becauseJSPmustbecompiledintoservletsand
executedbyaJSPenabledWebserver.

4. AJSPexpressionisusedtoinsertaJavaexpression
directlyintotheoutput.Ithasthefollowingform:

<%=Javaexpression%>

Theexpressionisevaluated,convertedintoastring,andsentto
theoutputstreamoftheservlet.

AJSPscriptletenablesyoutoinsertaJavastatementintothe
servletsjspServicemethod,whichisinvokedbytheservice
method.AJSPscriptlethasthefollowingform:

<%Javastatement%>

AJSPdeclarationisfordeclaringmethodsorfieldsintothe
servlet.Ithasthefollowingform:

5.
Threeerrors:
(1)<%!intk%>musthaveasemicolonforJavastatementtoend
(2)shouldhavebracestoenclosethestatementinsidetheloopbodyeven
thoughthereisonlyonestatement.
(3)<%=j;%><br>shouldhavenosemicolonforJSPexpression.

6.
<%!intk;%>kisaninstancevariablebecauseitisdefinedinthe
declaration
<%!inti;%>iisaninstancevariablebecauseitisdefinedinthe
declaration
<%for(intj=1;j<=9;j++)k+=1;%>kisalocalvariablebecauseit
isdefinedinthestatementtag.

<%=k><br><%=i><br><%=getTime()><br>
<%privatelonggetTime(){
longtime=System.currentTimeMillis();timeisalocalvariable
returntime;}%>

7. You can use variables in JSP. For convenience, JSP provides eight predefined
variables from the servlet environment that can be used with JSP expressions and
scriptlets. These variables are also known as JSP implicit objects. The predefined
variables are request, response, out, session, application, config, and page.

8. No. Because they are local variables in the service


method. The predefined variables (e.g., request, response, out) correspond to local
variables defined in the servlet methods doGet and doPost. They must appear in JSP
scriplet, not in JSP declaration.

9.Samereasonasin8.

10.AJSPdirectiveisastatementthatgivestheJSPengine
informationabouttheJSPpage.Thepagedirectiveletsyou
provideinformationforthepage,suchasimportingclasses
andsettingupcontenttype.Thepagedirectivecanappear
anywhereintheJSPfile.

11.No.JDK1.2orhigherrequiresaclasstobeinapackage
forittobeimported.

12.IfyouuseacustomclassfromaJSP,itshould
beplacedinc:\jakartatomcat
4.1.27\wepapps\examples\WEB
INF\classes\packageNameOfTheClass(oryoumay
reconfigureTomcattospecifyanewlocationfor
classfiles).
13.Thedifferenceliesinthescope.JSPallowsyoutoshare
theobjectofaclassamongdifferentpages.However,the
scopeofobjectcreatedusingthenewoperatorislimitedto
thepageonly.

14.Thescopeattributespecifiesthescopeoftheobject.
Fourscopesarepage,application,session,andrequest.

15.When<jsp:useBeanid="objectName"scope="scopeAttribute"
class="ClassName"/>isprocessed,theJSPenginefirst
searchesfortheobjectoftheclasswiththesameidand
scope.Iffound,thepreexistingbeanisused;otherwise,a
newbeaniscreated.
16.Use<jsp:setPropertyname="beanId"property="*"/>for
example.

17.WebapplicationsdevelopedusingJSPgenerallyconsistof
manypageslinkedtogether.JSPprovidesaforwardingtagin
thefollowingsyntaxthatcanbeusedtoforwardapageto
anotherpage.

<jsp:forwardpage="destination"/>

Vous aimerez peut-être aussi