pageContext
常用方法
PageContext
abstract public HttpSession getSession();
abstract public Object getPage();
abstract public ServletRequest getRequest();
abstract public ServletResponse getResponse();
abstract public Exception getException();
abstract public ServletConfig getServletConfig();
abstract public ServletContext getServletContext();
abstract public void forward(String relativeUrlPath) throws ServletException, IOException;
abstract public void include(String relativeUrlPath) throws ServletException, IOException;
abstract public void include(String relativeUrlPath, boolean flush) throws ServletException, IOException;
abstract public void handlePageException(Exception e) throws ServletException, IOException;
public BodyContent pushBody() {}
public ErrorData getErrorData() {...}
JspContext
abstract public Object getAttribute(String name); // 返回与页面范围中的名称关联的对象;如果未找到,则返回null
abstract public Object getAttribute(String name, int scope);
abstract public void setAttribute(String name, Object value);
abstract public void setAttribute(String name, Object value, int scope);
abstract public Object findAttribute(String name); // 在页面,请求,会话(如果有效)和应用程序范围内按顺序搜索命名属性,并返回关联的值或null
abstract public void removeAttribute(String name);
abstract public void removeAttribute(String name, int scope);
abstract public int getAttributesScope(String name);
abstract public JspWriter getOut();
public abstract ELContext getELContext();
public JspWriter pushBody( java.io.Writer writer ) {}
public JspWriter popBody() {}
session
application
config
out
page
request
response
exception
存数据
<body>
<%
pageContext.setAttribute("name1", "value1");
request.setAttribute("name2", "value2");
session.setAttribute("name3", "value3");
application.setAttribute("name4", "value4");
application.setAttribute("name1", "value11");
%>
<%
// 从底层到高层寻找,优先用底层同名值
String name1 = (String) application.getAttribute("name1");
String name2 = (String) application.getAttribute("name2");
String name3 = (String) application.getAttribute("name3");
String name4 = (String) application.getAttribute("name4");
String name5 = (String) application.getAttribute("name5");
%>
<h1>${name1} + ${pageContext.getAttributesScope("name1")}</h1>
<h1>${name2} + ${pageContext.getAttributesScope("name2")}</h1>
<h1>${name3} + ${pageContext.getAttributesScope("name3")}</h1>
<h1>${name4} + ${pageContext.getAttributesScope("name4")}</h1>
<h1>${name5} + ${pageContext.getAttributesScope("name5")}</h1>
<%=name5%> <%--没有值是会显示null--%>
<h1>${pageScope.get("name1")}</h1>
<h1>${applicationScope.get("name1")}</h1> <%--优先用本作用域里的值--%>
</body>
一个是直接获取,一个从pageContext里拿: