问题?关于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
<%@ 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>
<%@ 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");//包含