Listener监听器都能监听什么
• 监听域对象的创建与销毁
• 监听域对象属性值创建更换与销毁
监听器的分类
域对象监听器
• ServletContextListener 监听ServletContext变化
• ServletRequestListener 监听ServletRequest变化
• HttpSessionListener 监听HttpSession变化
域属性监听器
• ServletContextAttributeListener 监听ServletContext中属性变化
• HttpSessionAttributeListener 监听HttpSession中属性变化
• ServletRequestAttributeListener 监听ServletRequest中属性变化
attributeAdded 监听属性添加 —- 当数据范围对象没有该属性,第一次添加时调用执行
attributeRemoved 监听属性移除 —- 从一个数据范围对象删除一个已经存在属性执行
attributeReplaced 监听属性替换 —– 当一个数据范围已经存在一个属性,向数据范围添加相同名称属性触发替换方法
实际应用
统计:会员在线人数:::session的属性监听器
统计:在线人数:::session生死监听器
统计:资源访问量:::request的生死监听器
1、对每个页面访问量的统计
@WebListener()
public class MyListener implements ServletContextListener,
HttpSessionListener, ServletRequestListener {
public MyListener() {
}
//application初始化时就往域中存入map集合,用于存储请求路径及次数
public void contextInitialized(ServletContextEvent sce) {
//因为此数据要被所有资源所共享,所以需要存储到servletContext中
Map<String, Integer> countMap = new HashMap<>();
sce.getServletContext().setAttribute("countMap",countMap);
}
public void contextDestroyed(ServletContextEvent sce) {}
public void sessionCreated(HttpSessionEvent se) { }
public void sessionDestroyed(HttpSessionEvent se) { }
public void requestDestroyed(ServletRequestEvent sre) { }
//实现对每个页面的访问量统计
public void requestInitialized(ServletRequestEvent sre) {
HttpServletRequest req=(HttpServletRequest)sre.getServletRequest();
//获取请求的路径
String path = req.getServletPath();
//获取servletContext中的map,然后向map中存数据
Map<String, Integer> countMap = (Map<String, Integer>)req.getServletContext().getAttribute("countMap");
//将请求路径存储到map中,如果该map中包含该路径了就加1,否则是第一次访问设置为1
countMap.put(path,countMap.containsKey(path)?(countMap.get(path)+1):1);
System.out.println("/test/json的访问次数为:"+countMap.get("/test/json"));
}
}
2、会员在线人数的统计
@WebListener()
public class Demo01AttributeListener implements HttpSessionAttributeListener,ServletRequestAttributeListener,ServletContextAttributeListener{
//HttpSessionAttributeListener 实现 统计在线会员人数
public void attributeAdded(HttpSessionBindingEvent se) {
System.out.println("Demo01SessionAttributeListener:::attributeAdded");
System.out.println("session对象:"+se.getSession().getId()+":::添加属性:"+se.getName()+"="+se.getValue());
//统计人数:需要被整个项目共享 装在servletcontext域中:::要求会员登录 在seesion域中添加属性user
if(se.getName().equals("user")){//会员+1
Object count=se.getSession().getServletContext().getAttribute("count");//获取servletcontext域中的count属性
if(count==null){//你是第一个登录者
se.getSession().getServletContext().setAttribute("count", 1);
}else{//原来的值+1
se.getSession().getServletContext().setAttribute("count", ((Integer)count)+1);
}
}
}
public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("Demo01SessionAttributeListener:::attributeRemoved");
System.out.println("session对象:"+se.getSession().getId()+":::删除属性:"+se.getName()+"="+se.getValue());
//统计人数:需要被整个项目共享 装在servletcontext域中:::要求会员登出:删除session中的域属性user
if(se.getName().equals("user")){//会员+1
Object count=se.getSession().getServletContext().getAttribute("count");//获取servletcontext域中的count属性
//原来的值-1
se.getSession().getServletContext().setAttribute("count", ((Integer)count)-1);
}
}
public void attributeReplaced(HttpSessionBindingEvent se) {
System.out.println("Demo01SessionAttributeListener:::attributeReplaced");
System.out.println("session对象:"+se.getSession().getId()+":::修改属性:"+se.getName());
System.out.println("删除属性"+se.getName()+"旧值="+se.getValue()+",新值="+se.getSession().getAttribute(se.getName()));
}
@Override
public void attributeAdded(ServletContextAttributeEvent scae) {
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scae) {
}
@Override
public void attributeReplaced(ServletContextAttributeEvent scae) {
}
@Override
public void attributeAdded(ServletRequestAttributeEvent srae) {
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent srae) {
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent srae) {
}
}