JSP 的内置对象还包括 pageContext、page、config,在接下来的教程中我们将介绍这些内置对象的语法与应用。
pageContext 是页面上下文对象,这个特殊的对象提供了 JSP 程序执行时所需要用到的所有属性和方法,如 session、application、config、out 等对象的属性,也就是说,它可以访问本页所有的 session,也可以取本页所在的 application 的某一属性值,它相当于页面中所有其他对象功能的集大成者,可以用它访问本页中所有的其他对象。
pageContext 对象是 javax.servlet.jsp.pageContext 类的一个实例,它的创建和初始化都是由容器来完成的,JSP 页面里可以直接使用 pageContext 对象的句柄,pageContext 对象的 getXxx()、setXxx() 和 findXxx() 方法可以根据不同的对象范围实现对这些对象的管理。下表列出了 pageContext 对象的常用方法
pageContext对象的常用方法
方法 | 说明 |
void forward(String relativeUrlPath) | 把页面转发到另一个页面或者 Servlet 组件上 |
Exception getException() | 返回当前页的 Exception 对象 |
ServletRequest getRequest() | 返回当前页的 request 对象 |
ServletResponse getResponse() | 返回当前页的 response 对象 |
ServletConfig getServletConfig() | 返回当前页的 ServletConfig 对象 |
HttpSession getSession() | 返回当前页的 session 对象 |
Object getPage() | 返回当前页的 page 对象 |
ServletContext getServletContext() | 返回当前页的 application 对象 |
public Object getAttribute(String name) | 获取属性值 |
Object getAttribute(String name,int scope) | 在指定的范围内获取属性值 |
void setAttribute(String name,Object attribute) | 设置属性及属性值 |
void setAttribute(String name,Object obj,int scope) | 在指定范围内设置属性及属性值 |
void removeAttribute(String name) | 删除某属性 |
void removeAttribute(String name,int scope) | 在指定范围内删除某属性 |
void invalidate() | 返回 servletContext 对象,全部销毁 |
pageContext 对象的主要作用是提供一个单一界面,以管理各种公开对象(如 session、application、config、request、response 等),提供一个单一的 API 来管理对象和属性。
例如:
1、实现页面跳转
代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//实现服务器端跳转
pageContext.forward("test.jsp?mess=this is pagecontext");
%>
</body>
</html>
Test.jsp文件代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<br>mess:<%=request.getParameter("mess") %>
</body>
</html>
2、可获取request、response、config和applaction对象的接口代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
//获取session对象
HttpSession sessiona= pageContext.getSession();
sessiona.setAttribute("name","lisi");
//获取request对象
ServletRequest requesta= pageContext.getRequest(); //获取config对象的接口
requesta.setAttribute("age","18");
//获取response对象
ServletResponse responsea=pageContext.getResponse();
responsea.setBufferSize(16384);
//获取application对象
ServletContext applicationa=pageContext.getServletContext();
applicationa.setAttribute("sex","nan");
//获取config对象
ServletConfig configa=pageContext.getServletConfig();
%>
<br>name=<%=sessiona.getAttribute("name")%>
<br>age=<%=requesta.getAttribute("age")%>
<br>buffersize=<%=responsea.getBufferSize()%>
<br>sex=<%=applicationa.getAttribute("sex")%>
</body>
</html>
3、通过 pageContext 对象取得不同范围的属性值
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setAttribute("info","value of request scope");
session.setAttribute("info","value of session scope");
application.setAttribute("info","value of application scope");
%>
利用 pageContext 取出以下范围内各值(方法一):<br>
request 设定的值:<%=pageContext.getRequest().getAttribute("info") %> <br>
session 设定的值:<%=pageContext.getSession().getAttribute("info") %> <br>
application 设的值:<%=pageContext.getServletContext().getAttribute("info") %> <hr>
利用pageContext取出以下范围内各值(方法二):<br>
范围1(page)内的值:<%=pageContext.getAttribute("info",1) %> <br>
范围2(request)内的值:<%=pageContext.getAttribute("info",2) %> <br>
范围3(session)内的值:<%=pageContext.getAttribute("info",3) %> <br>
范围4(application)内的值:<%=pageContext.getAttribute("info",4) %> <hr>
利用 pageContext 修改或删除某个范围内的值:
<% pageContext.setAttribute("info","value of request scope is modified by pageContext",2); %> <br>
修改 request 设定的值:<br>
<%=pageContext.getRequest().getAttribute("info") %> <br>
<% pageContext.removeAttribute("info"); %>
删除 session 设定的值:<%=session.getAttribute("info") %>
</body>
</html>