按监听的对象划分
1.用于监听应用程序环境对象(ServletContext
)的事件监听器,实现ServletContextListener
接口
1 | public class MyServletContextListener implements ServletContextListener { |
2.用于监听用户会话对象(HttpSession
)的事件监听器,实现HttpSessionListener
接口
1 | public class MyHttpSessionListener implements HttpSessionListener { |
3.用于监听请求消息对象(ServletRequest
)的事件监听器,实现ServletRequestListener
接口
1 | public class MyServletRequestListener implements ServletRequestListener { |
按监听的事件划分
1.监听域对象自身的创建和销毁的事件监听器
即按监听对象划分的那几种
2.监听域对象的属性的增加和删除的事件监听器,实现ServletContextAttributeListener
,HttpSessionAttributeListener
或 ServletRequestAttributeListener
接口
1 | public class MyServletContextAttributeListener implements ServletContextAttributeListener { |
HttpSession
和ServletRequest
同理,只是方法参数类型不同
3.监听绑定到HttpSession
域中的某个对象的状态的事件监听器
这种情况不需要专门设计一个作为监听器的类,可以作为一个实体类,然后继承需要的接口:
1 | public class User implements HttpSessionBindingListener,HttpSessionActivationListener,Serializable { |
绑定和解除绑定:实现HttpSessionBindingListener
接口
钝化和活化:实现HttpSessionActivationListener
和Serializable
接口
实现
Serializable
接口是因为钝化时需要将seesion序列化存储到文件或者数据库,活化时需要反序列化