JAVAWEB的Listener

学习监听器
  1监听器是什么 :监听对象的变化或者监听事件的触发
  2有什么作用:当被监听的对象状态改变时,触发对应的方法
  3怎么用:
    ①声明监听器,(继承对应的监听器)
    ②重写监听方法,并实现自己需要的功能
    ③注册监听器
JavaWeb的监听器
1运行原理
  ①自定义监听器,并实现对应的监听器(ServletContextAttributeListener, HttpSessionAttributeListener 和ServletRequestAttributeListener)
  ②实现了对应的监听器,就代表自定义的监听监听那个对象
  ③覆写对应的监听方法,并实现自己的功能
  ④在web.xml中注册监听器

<listener>
<description>监听器名字--一般是类名</description>
<listener-class>监听器全类名</listener-class>
</listener>

2JavaWeb对应的监听器介绍
Servlet监听器的分类:在Servlet规范中定义了多种类型的监听器,它们用于监听的事件源分别为ServletContext,HttpSession和ServletRequest这三个域对象
Servlet规范针对这三个对象上的操作,又把多种类型的监听器划分为三种类型:
①监听域对象自身的创建和销毁的事件监听器。
  I 监听ServletContext域对象的创建和销毁
    ServletContextListener接口用于监听ServletContext对象的创建和销毁事件。(一般用于服务器启动时就静态化系统所需要的资源)
    实现了ServletContextListener接口的类都可以对ServletContext对象的创建和销毁进行监听。
        当ServletContext对象被创建时,激发contextInitialized (ServletContextEvent sce)方法。
        当ServletContext对象被销毁时,激发contextDestroyed(ServletContextEvent sce)方法。
    ServletContext域对象创建和销毁时机:
       创建:服务器启动针对每一个Web应用创建ServletContext
       销毁:服务器关闭前先关闭代表每一个web应用的ServletContext
  II 监听HttpSession域对象的创建和销毁
    HttpSessionListener 接口用于监听HttpSession对象的创建和销毁
      创建一个Session时,激发sessionCreated (HttpSessionEvent se) 方法
      销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se) 方法。
  III 监听ServletRequest域对象的创建和销毁
    ServletRequestListener接口用于监听ServletRequest 对象的创建和销毁
      Request对象被创建时,监听器的requestInitialized(ServletRequestEvent sre)方法将会被调用
      Request对象被销毁时,监听器的requestDestroyed(ServletRequestEvent sre)方法将会被调用

    ServletRequest域对象创建和销毁时机:
      创建:用户每一次访问都会创建request对象
      销毁:当前访问结束,request对象就会销毁

②监听域对象中的属性的增加和删除的事件监听器。
  I ServletContextAttributeListener:监听ServletContext对象的属性变化
  II HttpSessionAttributeListener :监听session对象的属性变化
  III ServletRequestAttributeListener:监听Request对象的属性变化
   (I,II,III都是监听域对象中属性的变更的监听器,就是监听属性的增删改并且都有以下方法)
   1.1、attributeAdded方法
     当向被监听对象中增加一个属性时,web容器就调用事件监听器的attributeAdded方法进行响应,这个方法接收一个事件类型的参数,
    监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象各个域属性监听器中的完整语法定义为:
    1 public void attributeAdded(ServletContextAttributeEvent scae)
    2 public void attributeAdded(HttpSessionBindingEvent hsbe)
    3 public void attributeAdded(ServletRequestAttributeEvent srae)
  1.2、attributeRemoved 方法
      当删除被监听对象中的一个属性时,web容器调用事件监听器的attributeRemoved方法进行响应
      各个域属性监听器中的完整语法定义为:
    1 public void attributeRemoved(ServletContextAttributeEvent scae)
    2 public void attributeRemoved (HttpSessionBindingEvent hsbe)
    3 public void attributeRemoved (ServletRequestAttributeEvent srae)
  1.3、attributeReplaced 方法
      当监听器的域对象中的某个属性被替换时,web容器调用事件监听器的attributeReplaced方法进行响应
      各个域属性监听器中的完整语法定义为:
    1 public void attributeReplaced(ServletContextAttributeEvent scae)
    2 public void attributeReplaced (HttpSessionBindingEvent hsbe)
    3 public void attributeReplaced (ServletRequestAttributeEvent srae)
notice:
  XXXEvent对象具有的方法
  XXXEvent.getName()---被修改的属性名
  XXXEvetn.getValue()--属性修改后的值
问题又来了,监听那个特定属性呢??还是监听全部属性呢??
当前是监听对象的全部属性啦
③监听绑定到HttpSession域中的某个对象的状态的事件监听器。
  HttpSessionBindingListener接口
   实现了HttpSessionBindingListener接口的JavaBean对象可以感知自己被绑定到Session中和 Session中删除的事件
   当对象被绑定(setAttribute())到HttpSession对象中时,web服务器调用该对象的void valueBound(HttpSessionBindingEvent event)方法
   当对象从HttpSession对象中解除绑定(removeAttribute())时,web服务器调用该对象的void valueUnbound(HttpSessionBindingEvent event)方法

 

posted @ 2015-04-12 11:48  Jeremy_software  阅读(177)  评论(0编辑  收藏  举报