servlet事件监听器
按监听的对象划分:servlet2.4规范定义的事件有三种:
1.用于监听应用程序环境对象(ServletContext)的事件监听器
2.用于监听用户会话对象(HttpSession)的事件监听器
3.用于监听请求消息对象(ServletRequest)的事件监听器
按监听的事件类项划分
1.用于监听域对象自身的创建和销毁的事件监听器
2.用于监听域对象中的属性的增加和删除的事件监听器
3.用于监听绑定到HttpSession域中的某个对象的状态的事件监听器
servlet 规范中为每种事件监听器都定义了相应的接口,在编写事件监听器程序时只需实现这些接口就可以了。一些Servlet事件监听器需要在web应用程序的部署 文件描述符文件(web.xml)中进行注册,一个web.xml可以注册多个servlet事件监听器。web服务器按照它们在web.xml中注册顺 序来加载和注册这些servlet事件监听器。servlet事件监听器的注册和调用过程都是由web容器自动完成的,当发生被监听对象被创建,修改,销 毁等事件时,web容器将调用与之相关的servlet事件监听器对象的相应方法,用户在这些方法中编写的事件处理代码即被执行。由于在一个web应用程 序中只会为每个事件监听器类创建一个实例对象,有可能出现多个线程同时调用一个事件监听对象的情况,所以要注意多线程安全问题。
监听域对象的创建和销毁
在一个web应用程序的整个运行周期内,web容器会创建和销毁三个重要的对象,ServletContext,HttpSession,ServletRequest。
概述:
Servlet监听器用于监听一些重要事件的发生,监听器对象可以在事情发生前、发生后可以做一些必要的处理。
接口:
目前Servlet2.4和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 |
1.在ServletContextListener接口中定义了两个事件处理方法,分别是contextInitialized()和contextDestroyed()
public void contextInitialized(ServletcontextEvent sce)
这个方法接受一个ServletContextEvent类型参数,在contextInitialized可以通过这个参数获得当前被创建的ServletContext对象。
public void contextDestroyed(ServletContextEvent sce)
2.在HttpSessionListneter接口中共定义了两个事件处理方法,分别是sessionCreated()和sessionDestroyed()
public void sessionCreated(HttpSessionEvent se)
这个方法接受一个(HttpSessionEvent 类型参数,在sessionCreated可以通过这个参数获得当前被创建的HttpSession对象。
public void sessionDestroyed(HttpSessionEvent se)
3.在ServletRequestListener接口中定义了两个事件处理方法,分别是requestInitialized()和requestDestroyed()
public void requestInitialized(ServletRequestEvent sre)
这个方法接受一个(ServletRequestEvent 类型参数,在requestInitialized可以通过这个参数获得当前被创建的ServletRequest对象。
public void requestDestroyed(ServletRequestEvent sre)
可 以看出三个监听器接口中定义的方法非常相似,执行原理与应用方式也相似,在web应用程序中可以注册一个或者多个实现了某一接口的事件监听器,web容器 在创建或销毁某一对象(如ServletContext,HttpSession)时就会产生相应的事件对象(如ServletcontextEvent ,或者HttpSessionEvent),接着依次调用每个事件监听器中的相应处理方法,并将产生的事件对象传递给这些方法。
4. ServletContextAttributeListener:用于监听WEB应用属性改变的事件,包括:增加属性、删除属性、修改属性,监听器类需要实现javax.servlet.ServletContextAttributeListener接口。
ServletContextAttributeListener接口方法:
void attributeAdded(ServletContextAttributeEvent scab)
若有对象加入Application的范围,通知正在收听的对象
void attributeRemoved(ServletContextAttributeEvent scab)
若有对象从Application的范围移除,通知正在收听的对象
void attributeReplaced(ServletContextAttributeEvent scab)
若在Application的范围中,有对象取代另一个对象时,通知正在收听的对象
5. HttpSessionBindingListener接口
注意:HttpSessionBindingListener接口是唯一不需要再web.xml中设定的Listener
当 我们的类实现了HttpSessionBindingListener接口后,只要对象加入Session范围(即调用HttpSession对象的 setAttribute方法的时候)或从Session范围中移出(即调用HttpSession对象的removeAttribute方法的时候或 Session Time out的时候)时,容器分别会自动调用下列两个方法:
void valueBound(HttpSessionBindingEvent event)
void valueUnbound(HttpSessionBindingEvent event)
6. HttpSessionAttributeListener接口
HttpSessionAttributeListener监听HttpSession中的属性的操作。
当在Session增加一个属性时,激发attributeAdded(HttpSessionBindingEvent
se) 方法;当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEvent
se)方法;当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent
se) 方法。这和ServletContextAttributeListener比较类似。
7.
HttpSessionActivationListener接口
主要用于同一个Session转移至不同的JVM的情形。
8. ServletRequestAttributeListener接口
和ServletContextAttributeListener接口类似。