• JSP的九个隐式对象


    JSP使用脚本元素作为一种简易方法在模板数据中嵌入java代码,这些脚本元素在JSP翻译成Servlet的阶段,都被转化为Servlet中的java代码。

    JSP引擎在调用JSP对应的_jspServlet时,会传递或创建9个与web开发相关的对象供_jspServlet使用。JSP技术的设计者为便于开发人员在编写JSP页面时获得这些web对象的引用,特意定义了9个相应的变量,开发人员在JSP页面中通过这些变量就可以快速获得这9大对象的引用。

    JSP翻译成Servlet代码都存在有:

     1  public void _jspService(HttpServletRequest request, HttpServletResponse response)
     2         throws java.io.IOException, ServletException {
     3 
     4     PageContext pageContext = null;
     5     HttpSession session = null;
     6     ServletContext application = null;
     7     ServletConfig config = null;
     8     JspWriter out = null;
     9     Object page = this;
    10     JspWriter _jspx_out = null;
    11     PageContext _jspx_page_context = null;
    12     ......
    13 }

    在上面的代码中就存在九个隐式对象

    对象名

    描述

    作用域

    request

    代表与请求相关的HttpServletRequest对象

    request

    response

    代表与响应相关的HttpServletResponse对象

    page

    pageContext

    代表封装请求某个JSP页面时请求环境的pageContext对象

    page

    session

    代表特定用户请求会话的HttpSession对象。该对象只有在JSP页面参与一个HTTP会话时才有意义

    session

    application

    代表Web应用程序的ServletContext对象

    application

    out

    代表与响应输出流相关的JspWriter对象

    page

    config

    代表JSP 页面的Servlet相关的ServletConfig对象

    page

    page

    等于Java编程语言中的this变量

    page

    exception

    代表JSP页面抛出的Trowable对象。这个对象只能在JSP错误页面中使用

    page

    request,response,session,application,config这些对象之前都讲过了的。还有out , page,pageContext没讲过!下面就主要介绍着三个对象。

    一 out  对象

    out对象是javax.servlet.jsp.JspWriter的实例,用于发送内容到响应中。JspWriter相当于一种带缓存功能的PrintWriter,设置JSP页面的page指令的buffer属性可以调整它的缓存大小。

    只有向out对象中写入了内容,且满足如下任何一个条件时,out对象才去调用ServletResponse.getWriter方法,并通过该方法返回的PrintWriter对象将out对象的缓冲区中的内容真正写入到Servlet引擎提供的缓冲区中:

    • 设置page指令的buffer属性关闭了out对象的缓存功能
    • out对象的缓冲区已满
    • 整个JSP页面结束

    out对象的使用方法,只要简单的调用out.print()或者out.println()方法即可。

     out对象的其他方法:

    abstract void clear()
    Clear the contents of the buffer.
    abstract void clearBuffer()
    Clears the current contents of the buffer.
    abstract void close()
    Close the stream, flushing it first.
    abstract void flush()
    Flush the stream.
    int getBufferSize()
    This method returns the size of the buffer used by the JspWriter.
    abstract int getRemaining()
    This method returns the number of unused bytes in the buffer.
    boolean isAutoFlush()
    This method indicates whether the JspWriter is autoFlushing.

    示例:

     1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>out隐式对象演示</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22   </head>
    23   
    24   <body>
    25    <%out.print("演示out隐式对象方法的使用"); %><br/>
    26    <%int getBufferSize=out.getBufferSize();
    27      int getRemaining=out.getRemaining();
    28      out.print("当前缓冲区的大小:"+getBufferSize+"<br/>");
    29      out.print("当前可使用的缓冲区大小:"+getRemaining+"<br/>");
    30      /* out.clear();
    31      out.close(); */
    32     %>   
    33   </body>
    34 </html>
     

    显示结果:

     

    二  page  对象

    page对象表示当前一个JSP页面,可以理解为一个对象本身,即:把一个JSP当作一个对象来看待。page对象在开发中几乎不用。

    三  PageContext 对象

    PageContext 是javax.servlet.jsp.PageContext 的实例。pageContext对象是JSP技术中最重要的一个对象,它代表JSP页面的运行环境,这个对象不仅封装了对其它8大隐式对象的引用,它自身还是一个域对象(容器),可以用来保存数据。他有三个主要的功能

    3.1 用它可以存取其他的隐式对象;

    3.2 用它可以对四个作用域空间进行数据的存取;

    3.3 可以用它进行页面的转发和包含。

    PageContext 对象中用于存取其他隐式对象的方法:

    abstract Exception getException()
    The current value of the exception object (an Exception).
    abstract Object getPage()
    The current value of the page object (In a Servlet environment, this is an instance of javax.servlet.Servlet).
    abstract ServletRequest getRequest()
    The current value of the request object (a ServletRequest).
    abstract ServletResponse getResponse()
    The current value of the response object (a ServletResponse).
    abstract ServletConfig getServletConfig()
    The ServletConfig instance.
    abstract ServletContext getServletContext()
    The ServletContext instance.
    abstract HttpSession getSession()
    The current value of the session object (an HttpSession).

    PageContext 对象中用于对作用域空间进行数据存取的方法:

    1 public void setAttribute(java.lang.String name,java.lang.Object value)
    2 public java.lang.Object getAttribute(java.lang.String name)
    3 public void removeAttribute(java.lang.String name)
    4 public java.lang.Object findAttribute(java.lang.String name)

    PageContext 类提供了四个常量,用来表示四个作用域的范围:

    PAGE_SCOPE 表示存储在PageContext 对象中,只在当前页面有效。

    REQUEST_SCOPE 表示存储在request对象中,在request作用域有效。

    SESSION_SCOPE 表示存储在session对象中,在session作用域有效。

    APPLICATION_SCOPE 表示存储在application对象中,在application作用域有效。

    PageContext引入和跳转到其他资源

    PageContext类中定义了一个forward方法(用来跳转页面)和两个include方法(用来引入页面)来分别简化和替代RequestDispatcher.forward方法和include方法。
      方法接收的资源如果以“/”开头, “/”代表当前web应用。

  • 相关阅读:
    关于MySql 数据库InnoDB存储引擎介绍
    .netcore 中使用开源的AOP框架 AspectCore
    C#关于反序列化实例时,接收实体字段少于或大于原实体对象 解析测试
    PostgreSQL TIMESTAMP类型 时间戳
    C# 新特性 操作符单?与??和 ?. 的使用
    PostgreSQL 常用函数
    AutoCAD.Net/C#.Net QQ群:193522571 previewicon生成的块图标太小,CMLContentSearchPreviews生成大的图片
    C#winform中OpenFileDialog的用法
    C# winform datagridview 无需点击两次即可编辑内嵌控件的方法和删除默认的空行的方法
    C# winform datagridview 内嵌控件值改变后立即触发事件,而不需要离开该单元格时才触发,此时需要用到dgv_CurrentCellDirtyStateChanged事件
  • 原文地址:https://www.cnblogs.com/hellokitty1/p/4951389.html
Copyright © 2020-2023  润新知