JSP隐含对象收藏

来源:互联网 发布:linux安装教程 编辑:程序博客网 时间:2024/04/28 10:03
分类: java 29人阅读 评论(0) 收藏 举报
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1.  JSP系列五:JSP隐含对象收藏  
  2.   
  3.   
  4. 一,jsp隐含对象  
  5.  1,jsp隐含对象是JSP容器加载管理的一组类的实例.  
  6.   
  7.   
  8.   * jsp隐含对象在 _jspService方法 中定义,所以只能在表达式,scriptlet中使用这些对象。  
  9.    
  10.  2,jsp隐含对象分为4类:  
  11.   
  12.   
  13.   * 输入输出对象 : HttpServletRequest的request, HttpServletResponse的response, JspWriter的out;  
  14.   * 作用域通信对象 : HttpSession的session, ServletContext的application, PageContext的pageContext;  
  15.   * servlet对象 : Object的page, ServletConfig的config;  
  16.   * 错误对象 : Throwable的exception;  
  17.   
  18.   
  19.  3,servlet中的代码片段:  
  20.   ... ...  
  21.  public void _jspService(HttpServletRequest request, HttpServletResponse response)  
  22.           throws java.io.IOException, ServletException {  
  23.       PageContext pageContext = null;  
  24.       HttpSession session = null;  
  25.       ServletContext application = null;  
  26.       ServletConfig config = null;  
  27.       JspWriter out = null;  
  28.       Object page = this;  
  29.       JspWriter _jspx_out = null;  
  30.       PageContext _jspx_page_context = null;  
  31.   
  32.   
  33.   ... ...  
  34.   
  35.   
  36.  }  
  37. 二,输入输出对象:控制页面的输入输出;  
  38.   
  39.   
  40.  1,HttpServletRequest request:客户端请求信息的封装。  
  41.   * 作用域:   
  42.    一次请求到响应的阶段。在这阶段,对于转发和页面包含对象是共享的。  
  43.    请求对象对于每一次请求都重新创建。  
  44.   
  45.   
  46.  2, HttpServletResponse response : 服务器响应信息的封装。  
  47.   * 用于设置响应报头,Cooike等响应信息。  
  48.   
  49.   
  50.  3,JspWriter :以字符流形式输出数据,是PrintWriter的缓存版本,使用page指令的buffer属性来设置缓冲区的大小。  
  51.     
  52.   * 几个和缓存操作有关的方法:  
  53.    flush : 刷新缓冲区;  
  54.    isAutoFlush:判断out对象是否自动刷新。设置自动刷新在page指令的buffer属性中。  
  55.    clear和clearBuffer:清除缓冲区中的内容.如果缓冲区被"刷新",  前者抛出IO异常,后者不会。  
  56.      
  57.   * servlet中的代码片段:  
  58.    out = pageContext.getOut();  
  59.          _jspx_out = out;   
  60.    
  61. 三,作用域通信对象:JSP页面和servlet的通信信息;  
  62.   
  63.   
  64.  1, PageContext pageContext :提供访问其他隐含对象及其属性和自身属性的统一入口,来保持同一页面不同组件之间的数据共享。  
  65.   
  66.   
  67.   * javax.servlet.jsp.PageContext;  
  68.   
  69.   
  70.   * 作用域:  
  71.    page的作用域。每个页面和请求都有不同的PageContext对象。  
  72.    使用include指令包含页面时,被包含文件共享该pageContext 对象。jsp:include动作则不可以。  
  73.    对于这样的属性那就放到request中。  
  74.    servlet和jsp页面不共享PageContext对象。  
  75.   
  76.   
  77.   * 访问,设置和移除 其他隐含对象的属性:scope为PageContext常量.  
  78.    getAttribute(string name, int scope);  
  79.    setAttribute(string name, int scope);  
  80.    removeAttribute(string name, int scope);  
  81.    removeAttribute(string name)方法将移除所有范围类的指定属性。  
  82.   
  83.   
  84.   * 搜索所有范围内指定的属性,按page,request,session,application的顺序来搜索。  
  85.    findAttribute(string name);  
  86.   
  87.   
  88.   * servlet中的代码片段:  
  89.     ... ...  
  90.   private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();  
  91.     ... ...  
  92.   public void _jspService(HttpServletRequest request, HttpServletResponse response)  
  93.     ... ...  
  94.    pageContext = _jspxFactory.getPageContext(this, request, response, null, true, 8192, true);  
  95.    PageContext _jspx_page_context = null;  
  96.          _jspx_page_context = pageContext;  
  97.   
  98.   
  99.    application = pageContext.getServletContext();  
  100.          config = pageContext.getServletConfig();  
  101.          session = pageContext.getSession();  
  102.          out = pageContext.getOut();  
  103.     ... ...  
  104.    _jspxFactory.releasePageContext(_jspx_page_context);  
  105.     ... ...  
  106.   }  
  107.   
  108.   
  109.  2,HttpSession session :封装当前请求的会话信息,实现会话跟踪。  
  110.   
  111.   
  112.   * servlet中的代码片段:  
  113.    session = pageContext.getSession();  
  114.   
  115.   
  116.   
  117.   
  118.  2,ServletContext application  
  119.   
  120.   
  121.   * 作用域: web应用程序运行期间,所有页面都共享该对象。  
  122.   * servlet中的代码片段:  
  123.     application = pageContext.getServletContext();  
  124.   
  125.   
  126.   
  127.   
  128. 四,servlet对象:JSP页面的环境信息;  
  129.   
  130.   
  131.  1,Object  page :当前请求期间servlet对象的引用。  
  132.   
  133.   
  134.   * 可以使用page访问生成的servlet的类成员和方法。page指令就是对该对象的应用。  
  135.   
  136.   
  137.   * 作用域:   
  138.    每次请求jsp页面时创建,在请求转发或响应后被销毁。  
  139.    这个范围的对象只能在创建对象的页面中访问。  
  140.    pageContext对象属于page范围。  
  141.      
  142.   * servlet中的代码片段:  
  143.    Object page = this;  
  144.   
  145.   
  146.  2,ServletConfig  config :servlet初始化信息。  
  147.   
  148.   
  149.   
  150.   
  151. 五,错误对象:处理页面的错误;  
  152.   
  153.   
  154.   * java.lang.Throwable exception : 表示JSP页面运行时产生的异常。  
  155.    
  156.  ** 该对象只能在错误页面(page指令中isErrorPage=true的页面)中使用。  

0 0
原创粉丝点击