本系列博客汇总在这里:JSP 汇总
pageContext 详细分析
-
pageContext 对象是 PageContext 类型,它不只是域对象,而且还可以操作所有域对象,还可以获取其他隐藏对象。
-
本身是域对象:pageContext 是 JSP 中的域对象,而在 Servlet 中不能使用它!它表示的当前页面中可以使用,是最小的范围!
void setAttribute(String name, Object value); Object getAttrbiute(String name, Object value); void removeAttribute(String name, Object value);
-
操作所有域(四个域):可以使用 pageContext 对象操作所有域对象,在 getAttribute()、setAttribute()、removeAttribute() 三个方法中多添加一个参数,int scope 来指定范围。
在 PageContext 类中包含四个 int 类型的常量表示四个范围:
PAGE_SCOPE
:pageContext 范围;
REQUEST_SCOPE
:request 范围;
SESSION_SCOPE
:session 范围;
APPLICATION_SCOPE
:application 范围;void setAttribute(String name, Object value, int scope)
:设置数据到指定的范围中。例如:pageContext.setAttribute(“hello”, “hello world”, PageContext.REQUEST)
,
这个方法调用等同:request.setAttribute(“hello”, “hello world”);
Object getAttribute(String name, int scope)
:获取指定范围的数据;
void removeAttribute(String name, int scope)
:移除指定范围的数据;
Object findAttribute(String name)
:在所有范围内查找数据,依次为 page、request、session、application。如果在前一个范围内查找到数据,那么就返回,而不会在到下一个范围中去查找! -
获取其他隐藏对象:可以使用 pageContext 获取其他隐藏对象。
JspWriter getOut()
:获取 out 隐藏对象;
ServletConfig getServletConfig()
:获取 config 隐藏对象;
Object getPage()
:获取 page 隐藏对象;
HttpServletRequest getRequest()
:获取 request 隐藏对象;
HttpServletResponse getResponse
:获取 response 隐藏对象;
HttpSession getSession()
:获取 session 隐藏对象;
ServletContext getServletContext()
:获取 application 隐藏对象;
JspException getException()
:获取 exception 隐藏对象转换后的 JspException 对象。
如有错误,欢迎指正!