Jsp中的PageContext隐式对象详解
2015-10-08 12:20
591 查看
问题?关于jsp的九大隐式对象中的PageContext详解
pageContext
javax.servlet.jsp.PageContext非常重要
一、pageContext
有三大作用:
1、本身是一个域对象。同时还能操作其他三个域对象(PageContext ServletRequest HttpSession
ServletContext)本身表示的域范围是本页面。
void setAttribute(String name,Object value)
void removeAttribute(String name)
Object getAttribute(String name)
操作其他的三个域对象
void setAttribute(String name,Object value,int scope)//scope操作于其他三大域对象
void removeAttribute(String name,int scope)
Object getAttribute(String name,int scope)
2.参数int scope是由PageContext类提供的静态变量规定的有以下参数:
PageContext.PAGE_SCOPE:页面范围(是PageContext本身中的那个Map,代号page)
PageContext.REQUEST_SCOPE:请求范围(是ServletRequest中的那 个Map,代号request)
PageContext.SESSION_SCOPE:请求范围(是HttpSession中的那个 Map,代号session)
PageContext.APPLICATION_SCOPE:请求范围(是ServletContext中 的那个Map,代号application)
举个例子(jsp页面):
PageContext1.jsp
PageContext2.jsp
运行看看结果如何?
注意: 转发 请求重定向 关浏览器 关服务器 结果都会变(这个很重要)
(非常有用)Object findAttribute(String name):依次按照page request session application范围搜索指定名称的对象,找到为止。
2、获取其他8个隐式对象
举个例子(普通类中):
3、提供了转发和包含的方便方法
RequestDispatcher rd = request.getRequestDispatcher("/url");//“/”绝对路径
rd.forward(request,response);
pageContext.forward("url");//重定向
pageContext.include("url");//包含
pageContext
javax.servlet.jsp.PageContext非常重要
一、pageContext
有三大作用:
1、本身是一个域对象。同时还能操作其他三个域对象(PageContext ServletRequest HttpSession
ServletContext)本身表示的域范围是本页面。
void setAttribute(String name,Object value)
void removeAttribute(String name)
Object getAttribute(String name)
操作其他的三个域对象
void setAttribute(String name,Object value,int scope)//scope操作于其他三大域对象
void removeAttribute(String name,int scope)
Object getAttribute(String name,int scope)
2.参数int scope是由PageContext类提供的静态变量规定的有以下参数:
PageContext.PAGE_SCOPE:页面范围(是PageContext本身中的那个Map,代号page)
PageContext.REQUEST_SCOPE:请求范围(是ServletRequest中的那 个Map,代号request)
PageContext.SESSION_SCOPE:请求范围(是HttpSession中的那个 Map,代号session)
PageContext.APPLICATION_SCOPE:请求范围(是ServletContext中 的那个Map,代号application)
举个例子(jsp页面):
PageContext1.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'PageContext1.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% //pageContext只在本页面有效,在其他页面使用pageContext是不能取不到值的 //pageContext.setAttribute("p1", "p的第一个值"); pageContext.setAttribute("p1", "p的第二个值",PageContext.PAGE_SCOPE); //可是如果用HttpRequest呢? //request.setAttribute("p2", "p2"); pageContext.setAttribute("p2", "p22",PageContext.REQUEST_SCOPE); // session.setAttribute("p3", "p3"); pageContext.setAttribute("ps","p33",PageContext.SESSION_SCOPE); //application.setAttribute("p4", "p4"); pageContext.setAttribute("p4", "p44",PageContext.APPLICATION_SCOPE); pageContext.forward("/PageContext2.jsp"); //response.sendRedirect("/day09/PageContext2.jsp"); %> </body> </html>
PageContext2.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'PageContext2.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%=pageContext.getAttribute("p")//得到PageContext1.jsp页面p的值?试问可以得到吗?大家想想 %> <%=application.getAttribute("p") %><!-- 那大家想想这里取得到上一页面的值吗? --> <%=pageContext.findAttribute("p") %> </body> </html>
运行看看结果如何?
注意: 转发 请求重定向 关浏览器 关服务器 结果都会变(这个很重要)
(非常有用)Object findAttribute(String name):依次按照page request session application范围搜索指定名称的对象,找到为止。
2、获取其他8个隐式对象
举个例子(普通类中):
package itcat; import javax.servlet.ServletContext; import javax.servlet.jsp.PageContext; public class pagecontext { public void m(PageContext pag){ //如果你要传参9大隐式对象,不可能写9个参量吧! //这个时候我们的Context的作用就可以了,它可以调用其他8个隐式对象,直接可定义这一个 //这样就方便很多了。 ServletContext ser=pag.getServletContext(); pag.getServletConfig(); pag.getOut(); pag.getPage(); pag.getRequest(); pag.getResponse(); pag.getSession(); pag.getException(); //这就是其他8个隐式对象了,都可以通过PageContext得到,非常的方便 } }
3、提供了转发和包含的方便方法
RequestDispatcher rd = request.getRequestDispatcher("/url");//“/”绝对路径
rd.forward(request,response);
pageContext.forward("url");//重定向
pageContext.include("url");//包含
相关文章推荐
- JS实现省市二级联动
- [JavaScript高级程序设计]语法、数据类型和一些区别注意
- JS数组方法汇总 ARRAY数组元素的添加和删除
- (" use strict")Javascript 严格模式详解
- jsp九大隐含对象的运用方法总结
- LINK - Paging for Azure Mobile Service Custom API (JS)
- JavaScript 中 onload 事件绑定多个方法
- jsp中的多行注释,在注释js文件内容的时候与注释body里的内容时有区别
- JavaScript 的 BOM
- js 笛卡尔积算法与多重数组笛卡尔积的例子
- [JSP]JSP中include指令和include动作的区别
- IE下 js文件上传时拒绝访问问题
- firefox调试js时发现js的中文显示乱码
- javascript中十进制转十六进制代码
- 动态创建script在IE中缓存js文件时导致编码不正确bug
- bzoj 1013: [JSOI2008]球形空间产生器sphere
- JSON字符串与JSON对象的区别
- js小数运算出现多位小数如何解决
- 企业IM (或业务系统)web api的json格式设计思考(原创)
- js实现超酷的照片墙展示效果图附源码下载