JSP基本用法(二)隐含对象

一、摘要

  在JSP容器中生成的Servlet类的_jspService()方法中,定义了几个对象,在编写JSP页面时我们可以使用这些隐含对象。

  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;

  注意:上面的代码没有exception内置对象,只有当页面的page指令的isErrorPage属性为true才会使用exception对象。

  下面将对这些对象进行逐一介绍。

二、application对象

  application对象代表Web应用本身,该对象通常有两个作用:

  1.在整个Web应用的多个JSP、Servlet之间共享数据

  在JSP中设置属性和对应的值:

<html>
    <head>
            <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
            <title>在此处插入标题</title>
    </head>
    
    <body>

    <%
        application.setAttribute("name", "xujian");
    %>
    </body>
    
</html>

  在Servlet中利用ServletContext对象的getInitParameter()方法即可获取该属性值:

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
    System.out.print(this.getServletContext().getInitParameter("name"));
    }

  2.访问Web应用的配置参数

  在web.xml文件中进行参数配置:

 <context-param>
        <param-name>name</param-name>
        <param-value>xujian</param-value>
    </context-param>

  JSP中可利用application对象的getInitParameter()方法获取配置参数值:

<body>
    <%= application.getInitParameter("name")%>
</body>

三、config对象

  Config对象代表当前JSP配置信息,该对象在JSP页面较为少用。我们可以在web.xml中配置信息:

<servlet>
      <servlet-name>config</servlet-name>
      <jsp-file>/Demo.jsp</jsp-file>
      
      <init-param>
          <param-name>age</param-name>
          <param-value>20</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>config</servlet-name>
    <url-pattern>/config</url-pattern>
  </servlet-mapping>

  然后利用Config对象的getInitParameter()方法获取配置信息:

<body>
    <%= config.getInitParameter("age")%>
</body>

四、pageContext对象

  pageContext对象代表页面上下文,主要用于访问JSP之间的共享数据。

  • a single API to manage the various scoped namespaces
  • a number of convenience API's to access various public objects
  • a mechanism to obtain the JspWriter for output
  • a mechanism to manage session usage by the page
  • a mechanism to expose page directive attributes to the scripting environment
  • mechanisms to forward or include the current request to other active components in the application
  • a mechanism to handle errorpage exception processing 

  注意:PageContext对象实际上是为我们提供了访问其他隐含对象的统一入口。

五、out对象

  out对象以字符流的形式输出数据,实际上是PrintWriter对象的带缓冲的版本,可以通过page指令的buffer属性来调整缓冲区的大小。

  所有使用out的地方都可以使用输出表达式来代替,<%= ...%>表达式的实质就是out.write(...)。

 六、exception对象

  exception对象表示了JSP页面运行时产生的异常,该对象只在错误页面中才能使用。

  注意:如果一个JSP页面使用errorPage属性定义了错误页面,那么在web.xml文件中定义的错误页面将不会被使用。

七、session对象

  session对象代表一次用户会话(从客户端浏览器连接服务器开始,到客户端浏览器与服务器断开为止),session通常用于跟踪用户的会话信息,如判断用户是否登录系统或者跟踪用户购买商品信息等。

        String getId();       //获取session的ID    
        long getCreationTime();//   获取session的生成时间    
        long getLashAccessedTime();   // 获取用户最后通过session发送请求时间    
        long getMaxInactiveInterval();    //获取session生命周期,如果超过这个时间则失效    
        void invalidate();    // 清空session内容    
        boolean isNew();      // 判断session是否为“新”的    
        void setMaxInactiveInterval();  // 设置session生命周期,如果超过这个时间则失效 

 

posted @ 2015-06-09 15:53  温布利往事  阅读(1493)  评论(1编辑  收藏  举报