【JSP程序设计】作用域通信对象 — pageContext对象
文章目录
- 1.pageContext对象概述
- 2.实例:pageContext对象方法运用
- (1)实例一:与request对象作用域比较
- (2)实例二:与session、application对象作用域比较
1.pageContext对象概述
pageContext对象是JSP页面本身的上下文,它的作用范围是在同一页面使用它可以访问页面作用域中定义的所有隐式对象。pageContext自身还是一个域对象,可以用来保存数据,同时可以通过pageContext这个域对象操作另外三个域 (Request域、Session域、ServletContext域),它是PageContext类的实例。
pageContext封装了Web开发中经常涉及的一些常用操作,例如包含和跳转到其他资源、检索其他域对象中保存的值等。pageContext对象常用的方法如下所示。
方法名 | 功能介绍 |
---|---|
void forward(String relativeUrlPath) | 将当前页面转发到另外一个页面或者 Servlet 组建上 |
ServletRequest getRequest() | 返回当前页面的 request 对象 |
ServletResponse getResponse() | 返回当前页面的 response 对象 |
HttpSession getSession() | 返回当前页面的 session 对象 |
JspWriterout getOut() | 返回 out 隐式对象 |
ServletConfig getServetConfig() | 返回当前页面的 servletConfig 对象 |
ServletContext getServletContext() | 返回当前页面的 servletContext 对象,这个对象是所有的页面共享的 |
Object findAttribute() | 按照页面、请求、会话,以及应用程序范围的属性实现对某个属性的搜索 |
void removeAttribute() | 删除默认页面对象或特定对象范围之中的已命名对象 |
2.实例:pageContext对象方法运用
(1)实例一:与request对象作用域比较
pageContextDemo01.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>pageContext对象方法运用</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"></head><body><%//可以访问其他的隐式对象pageContext.getRequest();//获取requestpageContext.getResponse();//获取responsepageContext.getSession();//获取sessionpageContext.getServletContext();//获取applicationpageContext.getOut();//获取out//可以在当前jsp页面范围中存放信息pageContext.setAttribute("username", "Merry");//和request进行对比request.setAttribute("username","Jack");%><%=pageContext.getAttribute("username") %><%//演示把请求跳转到另一个页面,再另一个页面是否还能获取page范围中的信息。request.getRequestDispatcher("pageContextDemo01_01.jsp").forward(request, response);%></body>
</html>
pageContextDemo01_01.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>pageContext对象请求转发页面</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"></head><body bgcolor="#99CCFF"><h3>观察pageContext与request的作用域有何区别?</h3><%="page范围中的信息: "+pageContext.getAttribute("username") %><br/><%="request范围中的信息: "+request.getAttribute("username") %></body>
</html>
运行结果:
当前实例可以说明,程序中可以使用pageContext对象获取其他隐式对象,例如request、response、session 等。此外,pageContext 对象保存的属性只在当前 page 页面有效,页面跳转后属性获取值为null,而request对象的属性值在当前request请求范围内有效。
(2)实例二:与session、application对象作用域比较
pageContextDemo02.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>pageContext对象运用:页面计数器</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"></head><%//初始化处理:if (pageContext.getAttribute("pageCount")==null) {pageContext.setAttribute("pageCount", newInteger(0));}if (session.getAttribute("sessionCount")==null) {session.setAttribute("sessionCount",newInteger(0));}if (application.getAttribute("appCount")==null) {application.setAttribute("appCount",newInteger(0));}//信息累加的处理Integer count = (Integer)pageContext.getAttribute("pageCount");pageContext.setAttribute("pageCount", new Integer(count.intValue()+1));Integer count2 = (Integer)session.getAttribute("sessionCount");session.setAttribute("sessionCount",new Integer(count2.intValue()+1));Integer count3 = (Integer)application.getAttribute("appCount");application.setAttribute("appCount",new Integer(count3.intValue()+1));%><body bgcolor="#CC9999"><b>页面计数= </b><%=pageContext.getAttribute("pageCount")%><br/><b>会话计数= </b><%=session.getAttribute("sessionCount")%><br/><b>应用程序计数= </b><%=application.getAttribute("appCount")%></body>
</html>
运行结果:
从这个实例可以看出,每刷新一次当前页面,pageContext 对象的属性计数器不变,而 session、application对象的属性计数器在递增,也说明了session对象、application对象比pageContext对象的生命周期要长,而application对象的生命周期最长。
pageContext 对象的范围只适用于当前页面范围,即超过这个页面就不能够使用了,所以使用pageContext对象向其他页面传递参数是不可能的。session的作用范围为一段用户持续和服务器所连接的时间,但与服务器断线后,这个属性就无效,例如断网或者关闭浏览器。session 可以通过使用属性maxInactiveInterval来获取或设定其超时时间。而application的范围是从服务器一开始执行服务到服务器关闭为止。它的范围最大,生存周期最长。