三(9)、JSP九大内置对象——pageContext

pageContext

pageContext对象提供了对jsp页面内所有的对象及名字空间访问,比如可以访问本页面的session,取本页面所在的application的某一属性值,它是页面所有功能集大成者。pageContext对象可以访问application对象、exception对象和session对象。还可以直接访问绑定在application对象、page对象、request对象、session对象上的Java对象。


一、API中

 JspWriter getOut()  返回当前客户端响应被使用的JspWriter流(out)  
 HttpSession getSession()  返回当前页中的HttpSession对象(session)  
 Object getPage()  返回当前页的Object对象(page)  
 ServletRequest getRequest()  返回当前页的ServletRequest对象(request)  
 ServletResponse getResponse()  返回当前页的ServletResponse对象(response)  
 Exception getException()  返回当前页的Exception对象(exception)  
 ServletConfig getServletConfig()  返回当前页的ServletConfig对象(config)  
 ServletContext getServletContext()  返回当前页的ServletContext对象(application)  
 void setAttribute(String name,Object attribute)  设置属性及属性值 ,在page范围内有效  
 void setAttribute(String name,Object obj,int scope)  在指定范围内设置属性及属性值 ,int1=page,2=request,3=session,4=application  
 public Object getAttribute(String name)  取属性的值  
 Object getAttribute(String name,int scope)  在指定范围内取属性的值  
 public Object findAttribute(String name)  寻找一属性,返回起属性值或NULL  
 void removeAttribute(String name)  删除某属性  
 void removeAttribute(String name,int scope)  在指定范围删除某属性  
 int getAttributeScope(String name)  返回某属性的作用范围  
 Enumeration getAttributeNamesInScope(int scope)  返回指定范围内可用的属性名枚举  
 void release() 释放pageContext所占用的资源  
 void forward(String relativeUrlPath)  使当前页面重导到另一页面  
 void include(String relativeUrlPath)  在当前位置包含另一文件  

二、用法

1.获取已经保存的在session中的username

<%=pageContext.getSession().getAttribute("username")
%>

2.跳转到login.jsp页面

<%
pageContext.forward("login.jsp");
%>

3.在当前页面包含“welcome.jsp页面的内容”

<% 
pageContext.include("welcome.jsp");
%>

4.查找各个域中的”username”属性

<% 
pageContext.findAttribute("username");
%>

5.直接解除application对象、session对象、request对象和page对象某个特定的参数或者java对象的绑定关系

<!--在application中解除username值的绑定-->
<%
pageContext.removeAttribute(“username”,pageContext.APPLICATION_SCOPE);
%>
<!--在session中解除username值的绑定-->
<%
pageContext.removeAttribute(“username”,pageContext.SESSION_SCOPE);
%>

6.void setAttribute(String name,Object obj,int scope) 在指定范围内设置属性及属性值 1=page, 2=request, 3=session, 4=application

(1)int scop是由PageContext类提供的静态变量规定的有以下参数:
1).pageContext.PAGE_SCOPE:页面范围page
2).pageContext.REQUEST_SCOPE:请求范围request
3).pageContext.SESSION_SCOPE:请求范围session
4).pageContext.APPLICATION_SCOPE:请求范围application

(2)Object obj是参数,String name。将obj值赋值给name中
(3)例子

  <body>
<%
/* 等同pageContext.setAttribute("name","我是76101",pageContext.PAGE_SCOPE)*/
pageContext.setAttribute("name","我是76101");

/* 等同pageContext.setAttribute("name","我是76102",pageContext.REQUEST_SCOPE)*/
request.setAttribute("name","我是76102");

/*等同pageContext.setAttribute("name","我是76103",pageContext.SESSION_SCOPE)*/
session.setAttribute("name","我是76103");

/*等同pageContext.setAttribute("name","我是76104",pageContext.APPLICATION_SCOPE)*/
application.setAttribute("name","我是76104");
%>
<!-- 取不同的值需要用到相应的方法 -->
page设置的值:<%=pageContext.getAttribute("name") %><br>
request设置的值:<%=pageContext.getRequest().getAttribute("name") %> <br> 
session设置的值:<%=pageContext.getSession().getAttribute("name") %> <br>
application设置的值:<%=pageContext.getServletContext().getAttribute("name") %><br>

<!-- 1=page, 2=request, 3=session, 4=application  -->
范围1内的值:<%=pageContext.getAttribute("name",1)%><br>
范围2内的值:<%=pageContext.getAttribute("name",2)%><br>
范围3内的值:<%=pageContext.getAttribute("name",3)%><br>
范围4内的值:<%=pageContext.getAttribute("name",4)%><br> 

<!-- 查找的时候是第一个位置 -->
值的查找:<%=pageContext.findAttribute("name") %><br>
属性name的范围:<%=pageContext.getAttributesScope("name")%>
</body>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值