JSP系列五:JSP隐含对象

来源:互联网 发布:js html代码转纯文本 编辑:程序博客网 时间:2024/06/05 01:11


一,jsp隐含对象
 1,jsp隐含对象是JSP容器加载管理的一组类的实例.

  * jsp隐含对象在 _jspService方法 中定义,所以只能在表达式,scriptlet中使用这些对象。
 
 2,jsp隐含对象分为4类:

  * 输入输出对象 : HttpServletRequest的request, HttpServletResponse的response, JspWriter的out;
  * 作用域通信对象 : HttpSession的session, ServletContext的application, PageContext的pageContext;
  * servlet对象 : Object的page, ServletConfig的config;
  * 错误对象 : Throwable的exception;

 3,servlet中的代码片段:
  ... ...
 public void _jspService(HttpServletRequest request, HttpServletResponse response)
          throws java.io.IOException, ServletException {
      PageContext pageContext = null;
      HttpSession session = null;
      ServletContext application = null;
      ServletConfig config = null;
      JspWriter out = null;
      Object page = this;
      JspWriter _jspx_out = null;
      PageContext _jspx_page_context = null;

  ... ...

 }
二,输入输出对象:控制页面的输入输出;

 1,HttpServletRequest request:客户端请求信息的封装。
  * 作用域:
   一次请求到响应的阶段。在这阶段,对于转发和页面包含对象是共享的。
   请求对象对于每一次请求都重新创建。

 2, HttpServletResponse response : 服务器响应信息的封装。
  * 用于设置响应报头,Cooike等响应信息。

 3,JspWriter :以字符流形式输出数据,是PrintWriter的缓存版本,使用page指令的buffer属性来设置缓冲区的大小。
  
  * 几个和缓存操作有关的方法:
   flush : 刷新缓冲区;
   isAutoFlush:判断out对象是否自动刷新。设置自动刷新在page指令的buffer属性中。
   clear和clearBuffer:清除缓冲区中的内容.如果缓冲区被"刷新",  前者抛出IO异常,后者不会。
   
  * servlet中的代码片段:
   out = pageContext.getOut();
         _jspx_out = out;
 
三,作用域通信对象:JSP页面和servlet的通信信息;

 1, PageContext pageContext :提供访问其他隐含对象及其属性和自身属性的统一入口,来保持同一页面不同组件之间的数据共享。

  * javax.servlet.jsp.PageContext;

  * 作用域:
   page的作用域。每个页面和请求都有不同的PageContext对象。
   使用include指令包含页面时,被包含文件共享该pageContext 对象。jsp:include动作则不可以。
   对于这样的属性那就放到request中。
   servlet和jsp页面不共享PageContext对象。

  * 访问,设置和移除 其他隐含对象的属性:scope为PageContext常量.
   getAttribute(string name, int scope);
   setAttribute(string name, int scope);
   removeAttribute(string name, int scope);
   removeAttribute(string name)方法将移除所有范围类的指定属性。

  * 搜索所有范围内指定的属性,按page,request,session,application的顺序来搜索。
   findAttribute(string name);

  * servlet中的代码片段:
    ... ...
  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
    ... ...
  public void _jspService(HttpServletRequest request, HttpServletResponse response)
    ... ...
   pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true);
   PageContext _jspx_page_context = null;
         _jspx_page_context = pageContext;

   application = pageContext.getServletContext();
         config = pageContext.getServletConfig();
         session = pageContext.getSession();
         out = pageContext.getOut();
    ... ...
   _jspxFactory.releasePageContext(_jspx_page_context);
    ... ...
  }

 2,HttpSession session :封装当前请求的会话信息,实现会话跟踪。

  * servlet中的代码片段:
   session = pageContext.getSession();


 2,ServletContext application

  * 作用域: web应用程序运行期间,所有页面都共享该对象。
  * servlet中的代码片段:
    application = pageContext.getServletContext();


四,servlet对象:JSP页面的环境信息;

 1,Object  page :当前请求期间servlet对象的引用。

  * 可以使用page访问生成的servlet的类成员和方法。page指令就是对该对象的应用。

  * 作用域:
   每次请求jsp页面时创建,在请求转发或响应后被销毁。
   这个范围的对象只能在创建对象的页面中访问。
   pageContext对象属于page范围。
   
  * servlet中的代码片段:
   Object page = this;

 2,ServletConfig  config :servlet初始化信息。


五,错误对象:处理页面的错误;

  * java.lang.Throwable exception : 表示JSP页面运行时产生的异常。
 
 ** 该对象只能在错误页面(page指令中isErrorPage=true的页面)中使用。
 
 

原创粉丝点击