监听器的作用是监听Web应用程序中某一个对象,监听客户端的处理动作,然后做出相应的处理。
Java Web应用程序中,Servlet容器提供了多种监听器的接口,使用的时候根据需求选择特定的接口实现就行。这些接口都是继承的java.util包中的EventListener接口。
可以看出,监听器的种类确实很多。
常用的有:
(1)request对象的监听ServletRequestAttributeListener和ServletRequestListener
ServletRequestListener用于监听用户请求的创建和销毁。
ServletRequestAttributeListener则用于监听ServletRequest(request)范围内属性的变化,实现该接口的监听器需要实现attributeAdded、attributeRemoved、attributeReplaced三个方法,分别监听Request的增加,删除和修改。
(2)Session对象的监听:HttpSessionAttributeListener和HttpSessionListener
HttpSessionListener监听HttpSession的操作。当创建一个Session时,激发sessionCreated(SessionEvent se)方法;当销毁一个 Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
HttpSessionAttributeListener监听HttpSession中的属性的操作。当在Session增加一个属性时,激发 attributeAdded(HttpSessionBindingEvent se) 方法;当在Session删除一个属性时,激发 attributeRemoved(HttpSessionBindingEvent se)方法;当在Session属性被重新设置时,激发 attributeReplaced(HttpSessionBindingEvent se) 方法。
(3)ServletContext对象的监听:ServletContextAttributeListener和ServletContextListener。
ServletContextAttributeListener监听对ServletContext属性的操作,比如增加/删除/修改
ServletContextListener监听ServletContext,当创建ServletContext时,激发 contextInitialized (ServletContextEvent sce)方法;当销毁ServletContext时,激发 contextDestroyed(ServletContextEvent sce)方法。
由此可见,ServletRequestAttributeListener,ServletContextAttributeListener和HttpSessionAttributeListener的作用相似,都用于监听属性的改变,只是ServletRequestAttributeListener监听request范围内属性的改变,而ServletContextAttributeListener监听的是application范围内属性的改变而HttpSessionAttributeListener监听Session的改变。
如果需要监听多个范围,可以创建多个实现不同接口的监听器,也可以采用一个监听器类来监听多种事件,只要让该监听器实现类同时实现多个监听器接口。
监听类写了之后,还需要在web.xml中进行配置,让程序知道监听器的存在后才可以使用。 监听器不需要像Filter那样配置Mapping,因为每个监听器监听的内容是特定的。只需配置监听器路径即可。
代码演示:web.xml <listener>
<listener>
<listener-class>
com.wh.drp.util.listener.TestHttpSessionAttributeListener
</listener-class>
</listener>
<listener>
<listener-class>
com.wh.drp.util.listener.TestHttpSessionListener
</listener-class>
</listener>
使用监听器非常方便,监听器自动触发,可以帮我们做很多事。监听器可以监听服务器端和客户端的状态变化,在变化是调用相应的Listener,这种机制可以称为回调机制。我觉得非常类似数据库里的触发器,都是自动触发,功能强大,使用方便。
最后看一个使用Session监听器统计登录人数的例子。
package com.wh.drp.util.listener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
public class TestHttpSessionAttributeListener implements
HttpSessionAttributeListener {
//在Session增加的时候触发
public void attributeAdded(HttpSessionBindingEvent se) {
System.out.println("TestHttpSessionAttributeListener-->>>attributeAdded()");
//判断是否是记录用户的Session
if ("userinfo".equals(se.getName())) {
//取出Context中的记录数
Integer count = (Integer)se.getSession().getServletContext().getAttribute("count");
//如果Context中没有count对象
if (count == null) {
count = 1;
}else {
count++;
}
//将记录数count重新写入到ServletContext中,其他需要该人数的地方可以在ServletContext中通过getServletContext().getAttribute("count")获得了count的具体值
se.getSession().getServletContext().setAttribute("count", count);
}
}
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("TestHttpSessionAttributeListener-->>>attributeRemoved()");
}
public void attributeReplaced(HttpSessionBindingEvent se) {
System.out.println("TestHttpSessionAttributeListener-->>>attributeReplaced()");
}
}
然后在web.xml中配置
<listener>
<listener-class>
com.wh.drp.util.listener.TestHttpSessionAttributeListener
</listener-class>
</listener>