jsp内置对象

来源:互联网 发布:艾宾浩斯记忆软件 编辑:程序博客网 时间:2024/06/08 08:42
JSP网页本身,page对象是当前页面转换后的Servlet类的实例。从转换后的Servlet类的代码中,可以看到这种关系:Object page = this;在JSP页面中,很少使用page对象。 
pageContext  javax.servlet.jsp.PageContext 的实例,该对象代表该JSP 页面上下文,使用该对象可以访问页面中的共享数据。常用的方法有getServletContext和getServletConfig等. 
   //使用pageContext 设置属性,该属性默认在page 范围内   
  pageContext. setAttribute ("page" , "hello") ;   
  
  //使用request 设置属性,该属性默认在request 范围内   
  request. setAttribute ("request" , "hello");   
  
  //使用pageContext将属性设置在request 范围中   
  pageContext.setAttribute("request2″ , "hello" , pageContext.REQUEST_SCOPE);   
  
  //使用session将属性设置在session 范围中   
  session.setAttribute("session" , "hello");   
  
  //使用pageContext将属性设置在session范围中   
  pageContext.setAttribute("session2″ , "hello" , pageContext.SESSION_SCOPE);   
  
  //使用application将属性设置在application范围中   
  application. setAttribute ("app" , "hello") ;   
  
  //使用pageContext 将属性设置在application 范围中   
  pageContext.setAttribute("app2″ , "hello" , pageContext.APPLICATION_SCOPE) ; 
---------------------------------------------------------------- 
jsp九大内置对象:    对应servlet中的java对象 
page             this 
pageContext      PageContext 
request          HttpServletRequest 
response         HttpServletResponse 
config           ServletConfig 
exception        Throwable 
out              JspWriter 
session          HttpSession 
application      ServletContext 
------------------------(以下为代码掩饰)----------------------------------- 
<body> 
<h3 style="color: red;"> 
pageContext对象 这个对象代表页面上下文,该对象主要用于访问JSP之间的共享数据。 
<br /> 
pageContext是PageContext类的实例,使用pageContext可以访问page、request、session、application范围的变量。 
<br /> 
getAttribute(String name):取得page范围内的name属性。 getAttribute(String 
name,int scope):取得指定范围内的name属性,其中scope可以是如下4个值: 
<br /> 

PageContext.PAGE_SCOPE:对应于page范围。 
<br /> 

PageContext.REQUEST_SCOPE:对应于request范围。 
<br /> 

PageContext.SESSION_SCOPE:对应于session范围。 
<br /> 

PageContext.APPLICATION_SCOPE:对应于application范围。 
<br /> 
</h3> 
<% 
//使用pageContext设置属性,该属性默认在page范围内 
pageContext.setAttribute("name", "testPageContext"); 
request.setAttribute("name", "testRequest"); 
session.setAttribute("name", "testSession"); 
//session.putValue("name","test"); 
application.setAttribute("name", "testApplication"); 
%> 
page设定的值:<%=pageContext.getAttribute("name")%><br> 
request设定的值:<%=pageContext.getRequest().getAttribute("name")%><br> 
session设定的值:<%=pageContext.getSession().getAttribute("name")%><br> 
application设定的值:<%=pageContext.getServletContext().getAttribute("name")%><br> 

范围1内的值:<%=pageContext.getAttribute("name", 1)%> 
PageContext.PAGE 的值: 
<%=PageContext.PAGE_SCOPE%><br> 
范围2内的值:<%=pageContext.getAttribute("name", 2)%><br> 
范围3内的值:<%=pageContext.getAttribute("name", 3)%><br> 
范围4内的值:<%=pageContext.getAttribute("name", 4)%><br> 

<!--从最小的范围page开始,然后是reques、session以及application--> 
<hr/> 
<% 
pageContext.removeAttribute("name", 3); 
%> 
pageContext修改后的session设定的值:<%=session.getValue("name")%><br> 

<% 
pageContext.setAttribute("name", "test", 4); 
%> 

pageContext修改后的application设定的值:<%=pageContext.getServletContext().getAttribute("name")%><br> 

值的查找:<%=pageContext.findAttribute("name")%><br> 

属性name的范围:<%=pageContext.getAttributesScope("name")%><br> 
<br> 
<hr> 
<hr/> 
<br/> 
</body> 
原创粉丝点击