Listener和Filter

1 Listener

1.1 Listener的概述

  • 监听器就是一个实现特定接口的普通java程序,这个程序专门用来监听另一个java对象的方法调用或者属性改变,当被监听对象发生上述事件后,监听器某个方法将立即被执行。

1.2 Servlet的监听器

  • 在Servlet规范中,定义了很多种类型的监听器,它们分别用于监听的事件源分别是request、session和application这三个域对象。
  • Servlet规范针对上面三个对象的操作,又把这多种监听器划分为三种类型:
    • ①监听三个域对象的创建和销毁的事件监听器。
    • ②监听三个域对象中属性的增加和删除的事件监听器。
    • ③监听绑定到session域的某个对象的状态的事件监听器。

1.3 监听域对象的创建和销毁的事件监听器

  • 监听request对象的创建和销毁的事件监听器:ServletRequestLinstener
    • ①监听初始化的方法  
void requestInitialized(ServletRequestEvent sre)
    • ②监听销毁的方法  
void requestDestroyed(ServletRequestEvent sre)
  • 监听session对象的创建和销毁的事件监听器:HttpSessionListener:
    • ①监听对象的创建的方法  
void sessionCreated(HttpSessionEvent se)
    • ②监听对象的销毁的方法  
void sessionDestroyed(HttpSessionEvent se)
  • 监听application对象的创建和销毁的事件监听器:ServletContextListener
    • ①监听初始化的方法  
void contextInitialized(ServletContextEvent sce)
    • ②监听销毁的方法  
void contextDestroyed(ServletContextEvent sce)

1.4 监听域对象的属性的增加和删除的事件监听器

  • 监听request域对象的属性增加和删除的事件监听器:ServletRequestAttributeListener
    • ①监听属性的增加的方法  
void attributeAdded(ServletRequestAttributeEvent srae)
    • ②监听属性的删除的方法  
void attributeReplaced(ServletRequestAttributeEvent srae)
  • 监听session域对象的属性的增加和删除的事件监听器:HttpSessionAttributeListener
    • ①监听属性增加的方法    
void attributeAdded(HttpSessionBindingEvent se)
    • ②监听属性删除的方法  
void attributeRemoved(HttpSessionBindingEvent se)
  • 监听application域对象的属性的增加和删除的事件监听器:ServletContextAttributeListener
    • ①监听属性增加的方法  
void attributeAdded(ServletContextAttributeEvent scab)
    • ②监听属性删除的方法  
void attributeRemoved(ServletContextAttributeEvent scab)

2 Filter

猜你喜欢

转载自www.cnblogs.com/xuweiweiwoaini/p/9828178.html