JSP整理总结--9大内置对象、4个作用域

    xiaoxiao2021-03-25  13

    来源url;http://blog.csdn.net/u012068523/article/details/46794027

    JSP中有四大域对象     > application:整个应用程序     > session:整个会话(一个会话中只有一个用户)     > request:一个请求链!     > pageContext:PageContext类型       * 域对象:只在当前jsp页面中有效的域,通常用来让jsp与当前jsp中标签之间共享数据       * 获取其他8个内置对象:可以获取其他8个内置对象       * 代理其他域对象:可以用pageContext来操作其他3个域。

    九大内置对象

    无需在jsp中声明即可使用的9个对象

    * out(JspWriter):等同与response.getWriter(),用来向客户端发送文本数据;

    * config(ServletConfig):对应“真身”中的ServletConfig; * page(当前JSP的真身类型):当前JSP页面的“this”,即当前对象,引用为Object类型; * exception(Throwable):只有在错误页面中可以使用这个对象; * request(HttpServletRequest):即HttpServletRequest类的对象; * response(HttpServletResponse):即HttpServletResponse类的对象; * application(ServletContext):即ServletContext类的对象;

    * session(HttpSession):即HttpSession类的对象,不是每个JSP页面中都可以使用,

    如果在某个JSP页面中设置<%@page session=”false”%>,说明这个页面不能使用session。

    * pageContext(PageContext):页面上下文对象、域对象,一个顶九个;

    pageContext对象   pageContext 对象是PageContext类型,它的主要功能有: * 域对象功能; * 代理其它域对象功能; * 获取其他内置对象; 1、域对象功能(范围是当前页面,是四个域对象中最小的) * void setAttribute(String name, Object value); * Object getAttrbiute(String name, Object value); * void removeAttribute(String name, Object value);

    2、代理其它域对象功能 还可以使用pageContext来代理其它3个域对象的功能,也就是说可以使用

    pageContext向request、session、application对象中存取数据,例如: * pageContext.setAttribute("x", "X"); 

    * pageContext.setAttribute("x", "XX", PageContext.REQUEST_SCOPE); 

    * pageContext.setAttribute("x", "XXX", PageContext.SESSION_SCOPE); 

    * pageContext.setAttribute("x", "XXXX", PageContext.APPLICATION_SCOPE); 

    * void setAttribute(String name, Object value, int scope):在指定范围中添加数据;

    * Object getAttribute(String name, int scope):获取指定范围的数据;

    * void removeAttribute(String name, int scope):移除指定范围的数据;

    * Object findAttribute(String name):依次在page、request、session、application

    范围查找名称为name的数据,如果找到就停止查找。这说明在这个范围内有相同名称的数据,

    那么page范围的优先级最高!

    3 获取其他内置对象

    * JspWriter getOut():获取out内置对象;

    * ServletConfig getServletConfig():获取config内置对象;

    * Object getPage():获取page内置对象;

    * ServletRequest getRequest():获取request内置对象;

    * ServletResponse getResponse():获取response内置对象;

    * HttpSession getSession():获取session内置对象;

    * ServletContext getServletContext():获取application内置对象;

    * Exception getException():获取exception内置对象;

    转载请注明原文地址: https://ju.6miu.com/read-300274.html

    最新回复(0)