JSP的九大隐式对象之------PageContext

PageContext:

pageContext对象是JSP中很重要的一个内置对象,不过在一般的JSP程序中,很少用到它。它是javax.servlet.jsp.PageContext类的实例对象,可以使用PageContext类的方法。实际上,pageContext对象提供了对JSP页面所有的对象及命名空间的访问。
pageContext对象能够存取其他隐含对象。
1.pageContext对象存取其他隐含对象属性的方法,此时需要指定范围的参数。
void setAttribute(String name, Object value, int scope)
范围参数有四个,分别代表四种范围:PAGE_SCOPE、REQUEST_SCOPE、SESSION_SCOPE、APPLICATION_SCOPE
例:
Demo10中存放的四个域对象:
 <body>
      <%
      放到页面范围,这是最小的域对象
      pageContext.setAttribute("name1","钊钊");
      Object o=pageContext.getAttribute("name1");
      out.println(o);
      //向请求范围存放数据
      pageContext.setAttribute("name2","钊钊REQUEST",PageContext.REQUEST_SCOPE);
      //pageContext.forward("Demo9.jsp");
      //向会话范围存放数据
      pageContext.setAttribute("name3","钊钊的sess",PageContext.SESSION_SCOPE);
      //向应用范围存放数据
       pageContext.setAttribute("name4","钊钊的app",PageContext.APPLICATION_SCOPE);
      %>
</body>
在Demo11中取出来:
 <body>
<%
      Object o =pageContext.findAttribute("name2");
      out.println(o);
      Object o1 =pageContext.findAttribute("name3");
      out.println(o1);
      Object o2 =pageContext.findAttribute("name4");
      out.println(o2);
   %>
</body>

2.PageContext对象取得其他隐含对象的方法
Exception getException( ) 回传网页的异常,不过此网页要为error page,
JspWriter getOut( ) 回传网页的输出流,例如:out
Object getPage( ) 回传网页的Servlet 实体(instance),例如:page
ServletRequest getRequest( ) 回传网页的请求,例如:request
ServletResponse getResponse( ) 回传网页的响应,例如:response
ServletConfig getServletConfig( ) 回传此网页的ServletConfig 对象,例如:config
ServletContext getServletContext( ) 回传此网页的执行环境(context),例如:application
HttpSession getSession( ) 回传和网页有联系的会话(session),例如:session
3.PageContext对象提供取得属性的方法
Object getAttribute(String name, int scope) 回传name 属性,范围为scope的属性对象,回传类型为Object
Enumeration getAttributeNamesInScope(int scope) 回传所有属性范围为scope 的属性名称,回传类型为Enumeration
int getAttributesScope(String name) 回传属性名称为name 的属性范围
void removeAttribute(String name) 移除属性名称为name 的属性对象
void removeAttribute(String name, int scope)移除属性名称为name,范围为scope 的属性对象
void setAttribute(String name, Object value, int scope) 指定属性对象的名称为name、值为value、范围为scope
Object findAttribute(String name) 寻找在所有范围中属性名称为name 的属性对象

2
常见的用法
在使用struts时,需要从jsp页面中指向struts中定义的方法时,可以使用如下的写法
(这里是在一个表单中请求action方法的示例)
<form action= "${pageContext.request.contextPath} /pathOfYourAction"
enctype= "multipart/form-data"method = "post">
file: <input type = "file"name= "upload_file">
<input type = "submit"value= "upload file"/>
</form >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值