pageContext简介
pageContext对象是JSP中很重要的一个内置对象。它是javax.servlet.jsp.PageContext类的实例对象,可以使用PageContext类的方法。本身也是一个域对象:它可以操作其它三个域对象(request session application)的数据
pageContext方法
void setAttribute(String name,Object o);
Object getAttribute(String name);
void removeAttribute(String name);
操作其它域对象的方法
void setAttribute(String name,Object o,int Scope);
Object getAttribute(String name,int Scope);
void removeAttribute(String name,int Scope);
scpoe的值:
PageContext.PAGE_SCOPE
PageContext.REQUEST_SCOPE
PageContext.SESSION_SCOPE
PageContext.APPLICATION_SCOPE
findAttribute(String name); 自动从page request session application依次查找,找到了就取值,结束查找。
它可以创建其它的8个隐式对象
在普通类中可以通过PageContext获取其他JSP隐式对象。自定义标签时就使用。
提供了的简易方法
pageContext.forward(“2.jsp”);
pageContext.include(“2.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>
<%
pageContext.setAttribute("p", "p");
request.setAttribute("p", "request");
application.setAttribute("p", "application");
pageContext.forward("/2.jsp");
%>
</body>
</html>
<%@ 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>
<%-- <%=session.getAttribute("p") %> --%>
<%-- <%=application.getAttribute("p") %> --%>
<%=pageContext.findAttribute("p") %>
</body>
</html>