您好,欢迎访问三七文档
UsingScriptingElementsImplicitJSPScriptingObjectsScriptingelementscanusepredefinedvariablesthatthecontainerassignsasreferencestoimplicitobjects(Table16-1)toaccessrequestandapplicationdata.TheseobjectsareinstancesofclassesdefinedbytheservletandJSPspecifications.AppendixDcontainscompletedescriptionsofallmethodsforeachclass,andtheyarebrieflyintroducedhereandusedinanumberofexamplesinthischapter.Table16-1.ImplicitJSPobjectsVariablenameJavatypeapplicationjavax.servlet.ServletContextconfigjavax.servlet.ServletConfigexceptionjava.lang.Throwableoutjavax.servlet.jsp.JspWriterpagejava.lang.ObjectpageContextjavax.servlet.jsp.PageContextrequestjavax.servlet.http.HttpServletRequestresponsejavax.servlet.http.HttpServletResponsesessionjavax.servlet.http.HttpSessionTheseobjectsprovideaccesstothesameinformation(andmore)astheimplicitvariablesyoucanuseinELexpressions,butit'snotaone-to-onematch:pageContextThepageContextvariablecontainsareferencetoaninstanceoftheclassnamedjavax.servlet.jsp.PageContext.Itprovidesmethodsforaccessingreferencestoalltheotherobjectsandattributesforholdingdatathatissharedbetweencomponentsinthesamepage.It'sthesameobjectthatyoucanaccesswiththe${pageContext}ELexpression.Attributevaluesforthisobjectrepresentthepagescope;theyarethesameobjectsasareavailabletotheELworldasaMaprepresentedbythe${pageScope}expression.requestTherequestvariablecontainsareferencetoaninstanceofaclassthatimplementsaninterfacenamedjavax.servlet.http.HttpServletRequest.Itprovidesmethodsforaccessingalltheinformationthat'savailableaboutthecurrentrequest,suchasrequestparameters,attributes,headers,andcookies.It'sthesameobjectthatyoucanaccesswiththe${pageContext.request}ELexpression.Attributevaluesforthisobjectrepresenttherequestscope;theyarethesameobjectsasareavailabletotheELworldasaMaprepresentedbythe${requestScope}expression.responseTheresponsevariablecontainsareferencetoanobjectrepresentingthecurrentresponsemessage.It'saninstanceofaclassthatimplementsthejavax.servlet.http.HttpServletResponseinterface,withmethodsforsettingheadersandthestatuscode,andaddingcookies.Italsoprovidesmethodsrelatedtosessiontracking.Thesemethodsaretheresponsemethodsyou'remostlikelytouse.Thesameobjectcanbeaccessedwiththe${pageContext.response}ELexpression.sessionThesessionvariableallowsyoutoaccesstheclient'ssessiondata,managedbytheserver.It'sassignedareferencetoaninstanceofaclassthatimplementsthejavax.servlet.http.HttpSessioninterface,whichprovidesaccesstosessiondataaswellasinformationaboutthesession,suchaswhenitwascreatedandwhenarequestforthesessionwaslastreceived.It'sthesameobjectthatyoucanaccesswiththe${pageContext.session}ELexpression.Attributevaluesforthisobjectrepresentthesessionscope;theyarethesameobjectsasareavailabletotheELworldasaMaprepresentedbythe${sessionScope}expression.applicationTheapplicationvariablecontainsareferencetotheinstanceofaclassthatimplementsthejavax.servlet.ServletContextinterfacethatrepresentstheapplication.Thisobjectholdsreferencestootherobjectsthatmorethanoneusermayrequireaccessto,suchasadatabaseconnectionpoolsharedbyallapplicationusers.Italsocontainslog()methodsyoucanusetowritemessagestothecontainer'slogfile.It'sthesameobjectthatyoucanaccesswiththe${pageContext.servletContext}ELexpression.Attributevaluesforthisobjectrepresenttheapplicationscope;theyarethesameobjectsasareavailabletotheELworldasaMaprepresentedbythe${applicationScope}expression.outTheoutobjectisaninstanceofjavax.servlet.jsp.JspWriter.Youcanusetheprint()andprintln()methodsprovidedbythisobjecttoaddtexttotheresponsemessagebody.Inmostcases,however,youwilljustusetemplatetextandJSPactionelementsinsteadofexplicitlyprintingtotheoutobject.exceptionTheexceptionobjectisavailableonlyinerrorpagesandcontainsinformationaboutaruntimeerror.It'sthesameobjectthatyoucanaccesswiththe${pageContext.exception}ELexpression.Theremainingtwoimplicitobjects(configandpage)aresorarelyusedinscriptingelementsthatIdon'tdiscussthemhere.Ifyou'reinterested,youcanreadabouttheminAppendixD.AllvariablenameslistedinTable16-1arereservedfortheimplicitobjectreferences.IfyoudeclareyourownvariablesinaJSPpage,youmustnotusethesereservedvariablenames.UsingScriptletsThescriptletelementcanbeusedtoaddawholeblockofcodetoapage,includingvariabledeclarations.Thecodeblockmustbeenclosedbyascriptletstart-identifier,%,andanend-identifier,%.Example16-1showsascriptletthatcreatestestdataforactionelements.Example16-1.Scriptletcreatingtestdata(scriptlet.jsp)%@pagelanguage=javacontentType=text/html%%@pageimport=java.util.*%%@taglibprefix=curi=@taglibprefix=fnuri==newArrayList();Mapauthor1=newHashMap();author1.put(name,JohnIrving);author1.put(id,newInteger(1));list.add(author1);Mapauthor2=newHashMap();author2.put(name,WilliamGibson);author2.put(id,newInteger(2));list.add(author2);Mapauthor3=newHashMap();author3.put(name,DouglasAdams);author3.put(id,newInteger(3));list.add(author3);pageContext.setAttribute(aut
本文标题:计算机专业英文翻译
链接地址:https://www.777doc.com/doc-5019703 .html