zoukankan      html  css  js  c++  java
  • Servlet监听器

    一、Servlet所有监听器简介
        在Servlet技术中已经定义了一些事件,并且我们可以针对这些事件来编写相关的事件监听器,从而对事件作出相应处理。
        1、目前Servlet3.0和JSP2.0总共有8个监听器接口和6个Event类, 其中HttpSessionAttributeListener与HttpSessionBindingListener皆使用 HttpSessionBindingEvent;HttpSessionListener和 HttpSessionActivationListener则都使用HttpSessionEvent;其余Listener对应的Event如下所 示:
    Listener接口
    Event类

    ServletContextListener
    ServletContextEvent

    ServletContextAttributeListener
    ServletContextAttributeEvent

    HttpSessionListener
    HttpSessionEvent

    HttpSessionActivationListener
    HttpSessionAttributeListener
    HttpSessionBindingEvent


    HttpSessionBindingListener
    ServletRequestListener
    ServletRequestEvent


    ServletRequestAttributeListener
    ServletRequestAttributeEvent
       2、分别介绍:
      A、 ServletContext相关监听接口
      补充知识:
      通过ServletContext 的实例可以存取应用程序的全局对象以及初始化阶段的变量。
      在JSP文件中,application 是 ServletContext 的实例,由JSP容器默认创建。Servlet 中调用 getServletContext()方法得到 ServletContext 的实例。
    注意:
    全局对象即Application范围对象,初始化阶段的变量指在web.xml中,经由元素所设定的变量,它的范围也是Application范围
    ,例如:

    Name
    browser

    当容器启动时,会建立一个Application范围的对象,若要在JSP网页中取得此变量时:
    String name = (String)application.getInitParameter("Name");
    或者使用EL时:
    ${initPara.name}
    若是在Servlet中,取得Name的值方法:
    String name = (String)ServletContext.getInitParameter("Name");


     1.ServletContextListener:
    用于监听WEB 应用启动和销毁的事件,监听器类需要实现javax.servlet.ServletContextListener 接口。
    ServletContextListener 是 ServletContext 的监听者,如果 ServletContext 发生变化,如服务器启动时 ServletContext 被创建,服务器关闭时 ServletContext 将要被销毁。

    ServletContextListener接口的方法:
    void contextInitialized(ServletContextEvent sce)
    通知正在接受的对象,应用程序已经被加载及初始化。
    void contextDestroyed(ServletContextEvent sce)
    通知正在接受的对象,应用程序已经被载出。

    ServletContextEvent中的方法:
    ServletContext getServletContext()
    取得ServletContext对象


      2.ServletContextAttributeListener:
      用于监听WEB应用属性改变的事件,包括:增加属性、删除属性、修改属性,监听器类需要实现javax.servlet.ServletContextAttributeListener接口。

    ServletContextAttributeListener接口方法:
    void attributeAdded(ServletContextAttributeEvent scab)
    若有对象加入Application的范围,通知正在收听的对象
    void attributeRemoved(ServletContextAttributeEvent scab)
    若有对象从Application的范围移除,通知正在收听的对象
    void attributeReplaced(ServletContextAttributeEvent scab)
    若在Application的范围中,有对象取代另一个对象时,通知正在收听的对象


    ServletContextAttributeEvent中的方法:
    java.lang.String getName()
    回传属性的名称
    java.lang.Object getValue()
    回传属性的值

      B、HttpSession相关监听接口
      1.HttpSessionBindingListener接口
    -------------------------------------------------------------
    注意:HttpSessionBindingListener接口是唯一不需要再web.xml中设定的Listener
    -------------------------------------------------------------

       当我们的类实现了HttpSessionBindingListener接口后,只要对象加入Session范围(即调用HttpSession对象的 setAttribute方法的时候)或从Session范围中移出(即调用HttpSession对象的removeAttribute方法的时候或 Session Time out的时候)时,容器分别会自动调用下列两个方法:
    void valueBound(HttpSessionBindingEvent event)
    void valueUnbound(HttpSessionBindingEvent event)

    思考:如何实现记录网站的客户登录日志, 统计在线人数?

      2.HttpSessionAttributeListener接口
      HttpSessionAttributeListener监听HttpSession中的属性的操作。
    当 在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。这和ServletContextAttributeListener比较类似。

      3.HttpSessionListener接口
    HttpSessionListener 监听HttpSession的操作。当创建一个Session时,激发session Created(HttpSessionEvent se)方法;当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。

       4.HttpSessionActivationListener接口
    主要用于同一个Session转移至不同的JVM的情形,用于监听Http会话active、passivate情况。。

      C、ServletRequest监听接口
      1.ServletRequestListener接口
    和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest
      2.ServletRequestAttributeListener接口
      和ServletContextListener接口类似的,这里由ServletContext改为ServletRequest

    二、监听器开发步骤
       1、创建相应的监听器,实现相应的接口并覆盖相应的方法
    例如:    MyServletContextListener.Java 
        package com; 
        import java.io.FileOutputStream; 
        import java.io.PrintWriter; 
        import javax.servlet.ServletContext; 
        import javax.servlet.ServletContextAttributeEvent; 
        import javax.servlet.ServletContextAttributeListener; 
        import javax.servlet.ServletContextEvent; 
        import javax.servlet.ServletContextListener; 
        public class MyServletContextListener  
        implements ServletContextListener,ServletContextAttributeListener 
        { 
             private ServletContext context = null; 
        //定义一个ServletContext对象变量,赋为null 
             public void contextInitialized(ServletContextEvent s) 
        { 
        //TODO 该方法实现了ServletContextListener接口定义的方法,
        对ServletContext进行初始化 
        this.context = s.getServletContext();                               
        //初始化一个ServletContext对象 
          print("ServletContext初始化......");                                  
        //打印出该方法的操作信息 
         } 
        public void contextDestroyed(ServletContextEvent s)  
        { 
        //TODO 该方法实现了ServletContextListener接口类定义方法,
        用于释放ServletContext对象 
                this.context = null; 
                print("ServletContext被释放......"); 
          } 
        public void attributeAdded(ServletContextAttributeEvent sa) 
         { 
        //TODO 当上下文添加属性时,将调用该方法。这里只是将添加的属性信息打印出来 
                 print("增加ServletContext对象的一个属性:
        attributeAdded('"+sa. getName()+"',' "+sa.getValue()+"')"); 
          } 
          public void attributeRemoved(ServletContextAttributeEvent sa) 
         { 
        //TODO 当把ServletContext中的某个属性删除时,调用该方法 
              print("删除ServletContext对象的某一个属性:
        attributeRemoved('"+sa.getName() +"','")"); 
          } 
          public void attributeReplaced(ServletContextAttributeEvent sa)  
        { 
        //TODO 当上下文进行属性值更新时,将调用该方法 
              print("更改ServletContext对象的某一个属性:
        attributeReplaced('"+sa.getName()+"', '"+sa.getValue()+"')");    
         }         
          private void print(String message) 
        { 
        //调用该方法在txt文件中打印出message字符串信息 
             PrintWriter out = null; 
              Try 
        { 
           out = new PrintWriter(new FileOutputStream("D:\output.txt",true)); 
           out.println(new java.util.Date().toLocaleString()+"
        ContextListener: "+message); 
              out.close(); 
           } 
              catch(Exception e) 
            { 
                 e.printStackTrace(); 
            } 
        } 
        }
      2、在web.xml配置监听器(只有HttpSessionBindingListener不需要在web.xml配置
          <listener> 
          <listener-class>servlet.MyServlet
        ContextListener</listener-class> 
        </listener>

  • 相关阅读:
    ASP.NET教程4
    ASP.NET教程11
    TreeView Demo
    System.Net.Dns.GetHostByAddress(string) 已经过时
    会员注册实例
    ASP.NET教程2
    多表关联与表值函数
    ASP.NET教程6
    BusinessFrameWork
    ASP.NET教程8
  • 原文地址:https://www.cnblogs.com/kabi/p/5182864.html
Copyright © 2011-2022 走看看