一、Listener简介
监听器是JavaWeb中三大组件(Servlet、Filter、Listener)之一,JavaWeb的组件的共同特点:
1️⃣都需要实现某个接口
2️⃣都需要在web.xml文件中进行注册
学习监听器要先搞清楚监听器是监听谁的?JavaWeb的监听器主要是监听域对象ServletRequest、HttpSession、ServletContext,只监听这三个域对象,pageContext不会被监听。
二、监听器分类
监听器一共有三组:
1、生命周期变化监听器
监听ServletRequest、HttpSession、ServletContext的创建和销毁
①ServletContextListener:ServletContext对象会在项目启动时创建,在项目卸载时销毁
//这个方法会在ServletContext创建之后调用
void contextInitialized(ServletContextEvent sce);
//这个方法会在ServletContext销毁前调用
void contextDestroyed(ServletContextEvent sce);
ServletContextEvent通过getServletContext()方法获取ServletContext对象
ServletContext getServletContext ();//可以获取到ServletContext对象
使用步骤:
1️⃣创建一个类实现ServletContextListener接口
2️⃣在web.xml文件进行配置
<listener>
<listener-class>监听器的全类名</listener-class>
</listener>
②HttpSessionListener
//session创建之后调用
void sessionCreated(HttpSessionEvent se);
//session对象销毁前调用
void sessionDestroyed(HttpSessionEvent se);
HttpSessionEvent可通过该类的getSession()方法获取到HttpSession对象:
//可以获取到当前Session对象
HttpSession getSession ();
③ServletRequestListener
//Request对象销毁前调用
void requestDestroyed(ServletRequestEvent sre);
//Request对象创建之后调用
void requestInitialized(ServletRequestEvent sre);
ServletRequestEvent可通过该类的getServletRequest()方法获取到ServletRequest对象,该类的对象也可以通过getServletContext()方法获取到ServletContext对象
//可以获取当前的request对象
ServletRequest getServletRequest ();
//可以获取ServletContext对象
ServletContext getServletContext ();
2、属性变化监听器
监听ServletRequest、HttpSession、ServletContext的属性的变化,包括属性的添加,移除和替换
①ServletContextAttributeListener:监听ServletContext域中的属性变化
//当向ServletContext中添加属性时,调用该方法
void attributeAdded(ServletContextAttributeEvent scab);
//当从ServletContext中移除一个属性时,调用该方法
void attributeRemoved(ServletContextAttributeEvent scab);
//当ServletContext中的一个属性被替换时,调用该方法
void attributeReplaced(ServletContextAttributeEvent scab);
通过ServletContextAttributeEvent可以:
String getName();//获取属性名
Object getValue();//获取属性值,注意当替换属性值,获取到的是旧值
ServletContext getServletContext();//获取ServletContext对象
②HttpSessionAttributeListener:监听Session域中的属性变化
//向session中添加属性时调用
void attributeAdded(HttpSessionBindingEvent se);
//从session中移除属性时调用
void attributeRemoved(HttpSessionBindingEvent se);
//替换session中属性时调用
void attributeReplaced(HttpSessionBindingEvent se);
HttpSessionBindingEvent的作用:
String getName();//获取属性名
Object getValue();//获取属性值,注意当替换属性值,获取到的是旧值
HttpSession getSession();//获取session对象
③ServletRequestAttributeListener:监听request域中的属性变化
//向request中添加属性时调用
void attributeAdded(ServletRequestAttributeEvent srae);
//从request中移除属性时调用
void attributeRemoved(ServletRequestAttributeEvent srae);
//替换request中属性时调用
void attributeReplaced(ServletRequestAttributeEvent srae);
ServletRequestAttributeEvent的作用:
String getName();//获取属性名
Object getValue();//获取属性值,注意当替换属性值,获取到的是旧值
ServletRequest getServletRequest ();//可以获取当前的request对象
ServletContext getServletContext ();//可以获取ServletContext对象
3、对象在Session中添加和钝化的监听器
以下两个监听器不需要我们创建一个专门的类来实现,已经实现了,同样也不需要在web.xml文件进行配置,这两个监听器主要是监听某个对象在Session中的的变化
①HttpSessionBindingListener:该监听器主要监听某个类的实例在session中添加和移除
//活化或者新添加:当该类的实例设置进session对象时调用
void valueBound(HttpSessionBindingEvent event);
//钝化,当该类的实例从session中移除时调用
void valueUnbound(HttpSessionBindingEvent event);
HttpSessionBindingEvent的作用:
String getName();//获取属性名
Object getValue();//获取属性值,注意当替换属性值,获取到的是旧值
HttpSession getSession();//获取session对象
②HttpSessionActivationListener:监听某个类的实例是否和Session一起活化或钝化(需要实现Serializable接口)
//当该类的实例和session一起钝化到硬盘时调用
void sessionWillPassivate(HttpSessionEvent se);
//当该类的实例和session一起活化到内存时调用
void sessionDidActivate(HttpSessionEvent se);
HttpSessionEvent的作用:
HttpSession getSession ();//可以获取到当前Session对象