Servlet 监听器Listener详解

转自:http://blog.csdn.net/u012228718/article/details/41730799

一、简介

(一)概述

1、Listener 用于监听 Java web程序中的事件,例如创建、修改、删除Session、request、context等,并触发响应的事件。

2、 Listener 对应观察者模式,事件发生的时候会自动触发该事件对应的Listeer。 Listener 主要用于对 Session、request、context 进行监控。servlet2.5 规范中共有 8 种Listener  。

(二)实现

1、不同功能的Listener 需要实现不同的 Listener  接口,一个Listener也可以实现多个接口,这样就可以多种功能的监听器一起工作。

2、8种监听器可以分为三类:

1)监听 Session、request、context 的创建于销毁,分别为  

HttpSessionLister、ServletContextListener、ServletRequestListener

2)监听对象属性变化,分别为:

HttpSessionAttributeLister、ServletContextAttributeListener、ServletRequestAttributeListener 

3)监听Session 内的对象,分别为HttpSessionBindingListener 和 HttpSessionActivationListener。与上面六类不同,这两类 Listener 监听的是Session 内的对象,而非 Session 本身,不需要在 web.xml中配置。

2、实现web.xml的Listener配置。

1)<listener>标签与 <listener-class>

2)<listener>一般配置在 <servlet>便签的前面。

[java] view plain copy
 
  1. package servlet.listener;  
  2.   
  3. import javax.servlet.ServletContextEvent;  
  4. import javax.servlet.ServletContextListener;  
  5. /** 
  6.  *  
  7.  * MyListener.java 
  8.  * 
  9.  * @title Context监听器 
  10.  * @description 
  11.  * @author SAM-SHO  
  12.  * @Date 2014-9-25 
  13.  */  
  14. public class MyListener implements ServletContextListener {  
  15.   
  16.     public void contextDestroyed(ServletContextEvent sce) {  
  17.           
  18.     }  
  19.   
  20.     public void contextInitialized(ServletContextEvent sce) {  
  21.           
  22.     }  
  23.   
  24.       
  25. }  
[html] view plain copy
 
  1. <!--监听器 -->  
  2. <listener>  
  3.     <listener-class>servlet.listener.MyListener</listener-class>  
  4. </listener>  

二、八种类型监听器

(一)监听 Session、request、context 的创建于销毁。

 HttpSessionLister、ServletContextListener、ServletRequestListener

1、三种监听器的触发时机及使用:

2、实例:实现监听对象的创建与销毁

[java] view plain copy
 
  1. package servlet.listener;  
  2.   
  3. import javax.servlet.ServletContext;  
  4. import javax.servlet.ServletContextEvent;  
  5. import javax.servlet.ServletContextListener;  
  6. import javax.servlet.ServletRequestEvent;  
  7. import javax.servlet.ServletRequestListener;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpSession;  
  10. import javax.servlet.http.HttpSessionEvent;  
  11. import javax.servlet.http.HttpSessionListener;  
  12.   
  13. import org.apache.commons.logging.Log;  
  14. import org.apache.commons.logging.LogFactory;  
  15.   
  16. /** 
  17.  *  
  18.  * ListenerTest.java 
  19.  *  
  20.  * @title 监听对象的创建与销毁 
  21.  * @description 
  22.  * @author SAM-SHO 
  23.  * @Date 2014-12-10 
  24.  */  
  25. public class ListenerTest implements HttpSessionListener, ServletContextListener, ServletRequestListener {  
  26.   
  27.     Log log = LogFactory.getLog(getClass());  
  28.   
  29.     // 创建 session  
  30.     public void sessionCreated(HttpSessionEvent se) {  
  31.         HttpSession session = se.getSession();  
  32.         log.info("新创建一个session, ID为: " + session.getId());  
  33.     }  
  34.   
  35.     // 销毁 session  
  36.     public void sessionDestroyed(HttpSessionEvent se) {  
  37.         HttpSession session = se.getSession();  
  38.         log.info("销毁一个session, ID为: " + session.getId());  
  39.     }  
  40.   
  41.     // 加载 context  
  42.     public void contextInitialized(ServletContextEvent sce) {  
  43.         ServletContext servletContext = sce.getServletContext();  
  44.         log.info("即将启动" + servletContext.getContextPath());  
  45.     }  
  46.   
  47.     // 卸载 context  
  48.     public void contextDestroyed(ServletContextEvent sce) {  
  49.         ServletContext servletContext = sce.getServletContext();  
  50.         log.info("即将关闭" + servletContext.getContextPath());  
  51.     }  
  52.   
  53.     // 创建 request  
  54.     public void requestInitialized(ServletRequestEvent sre) {  
  55.   
  56.         HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();  
  57.   
  58.         String uri = request.getRequestURI();  
  59.         uri = request.getQueryString() == null ? uri : (uri + "?" + request.getQueryString());  
  60.   
  61.         request.setAttribute("dateCreated", System.currentTimeMillis());  
  62.   
  63.         log.info("IP " + request.getRemoteAddr() + " 请求 " + uri);  
  64.     }  
  65.   
  66.     // 销毁 request  
  67.     public void requestDestroyed(ServletRequestEvent sre) {  
  68.   
  69.         HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();  
  70.   
  71.         long time = System.currentTimeMillis() - (Long) request.getAttribute("dateCreated");  
  72.   
  73.         log.info(request.getRemoteAddr() + "请求处理结束, 用时" + time + "毫秒. ");  
  74.     }  
  75.   
  76. }  



(二)监听对象属性变化,分别为HttpSessionAttributeLister、ServletContextAttributeListener、ServletRequestAttributeListener 

1、三种监听器的触发时机及使用:

2、实例:实现对象属性的监听

[java] view plain copy
 
  1. package servlet.listener;  
  2.   
  3. import javax.servlet.http.HttpSession;  
  4. import javax.servlet.http.HttpSessionAttributeListener;  
  5. import javax.servlet.http.HttpSessionBindingEvent;  
  6.   
  7. import org.apache.commons.logging.Log;  
  8. import org.apache.commons.logging.LogFactory;  
  9. /** 
  10.  *  
  11.  * SessionAttributeListenerTest.java 
  12.  * 
  13.  * @title 监听Session对象的属性 
  14.  * @description 
  15.  * @author SAM-SHO  
  16.  * @Date 2014-12-10 
  17.  */  
  18. public class SessionAttributeListenerTest implements HttpSessionAttributeListener {  
  19.   
  20.     Log log = LogFactory.getLog(getClass());  
  21.   
  22.     // 添加属性  
  23.     public void attributeAdded(HttpSessionBindingEvent se) {  
  24.         HttpSession session = se.getSession();  
  25.         String name = se.getName();  
  26.         log.info("新建session属性:" + name + ", 值为:" + se.getValue());  
  27.     }  
  28.   
  29.     // 删除属性  
  30.     public void attributeRemoved(HttpSessionBindingEvent se) {  
  31.         HttpSession session = se.getSession();  
  32.         String name = se.getName();  
  33.         log.info("删除session属性:" + name + ", 值为:" + se.getValue());  
  34.     }  
  35.   
  36.     // 修改属性  
  37.     public void attributeReplaced(HttpSessionBindingEvent se) {  
  38.         HttpSession session = se.getSession();  
  39.         String name = se.getName();  
  40.         Object oldValue = se.getValue();  
  41.         log.info("修改session属性:" + name + ", 原值:" + oldValue + ", 新值:" + session.getAttribute(name));  
  42.     }  
  43. }  



(三)监听Session 内的对象,分别为HttpSessionBindingListener 和 HttpSessionActivationListener

1、触发时机及使用:对象必须实现Listener接口,不需要在web.xml中配置

2、实例:实现对象属性的监听

[java] view plain copy
 
  1. package servlet.listener;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. import javax.servlet.http.HttpSession;  
  7. import javax.servlet.http.HttpSessionActivationListener;  
  8. import javax.servlet.http.HttpSessionBindingEvent;  
  9. import javax.servlet.http.HttpSessionBindingListener;  
  10. import javax.servlet.http.HttpSessionEvent;  
  11.   
  12. import org.apache.commons.logging.Log;  
  13. import org.apache.commons.logging.LogFactory;  
  14. /** 
  15.  *  
  16.  * PersonInfo.java 
  17.  * 
  18.  * @title 同时实现多个接口 
  19.  * 被串行化,需要实现Serializable接口 
  20.  * @description 
  21.  * @author SAM-SHO  
  22.  * @Date 2014-12-10 
  23.  */  
  24. public class PersonInfo implements HttpSessionActivationListener, HttpSessionBindingListener, Serializable {  
  25.   
  26.     private static final long serialVersionUID = -4780592776386225973L;  
  27.   
  28.     Log log = LogFactory.getLog(getClass());  
  29.   
  30.     private String name;  
  31.     private Date dateCreated;  
  32.   
  33.   
  34.     // 从硬盘加载后  
  35.     public void sessionDidActivate(HttpSessionEvent se) {  
  36.         HttpSession session = se.getSession();  
  37.         log.info(this + "已经成功从硬盘中加载。sessionId: " + session.getId());  
  38.     }  
  39.   
  40.     // 即将被钝化到硬盘时  
  41.     public void sessionWillPassivate(HttpSessionEvent se) {  
  42.         HttpSession session = se.getSession();  
  43.         log.info(this + "即将保存到硬盘。sessionId: " + session.getId());  
  44.     }  
  45.   
  46.     // 被放进session前  
  47.     public void valueBound(HttpSessionBindingEvent event) {  
  48.         HttpSession session = event.getSession();  
  49.         String name = event.getName();  
  50.         log.info(this + "被绑定到session "" + session.getId() + ""的" + name + "属性上");  
  51.   
  52.         // 记录放到session中的时间  
  53.         this.setDateCreated(new Date());  
  54.     }  
  55.   
  56.     // 从session中移除后  
  57.     public void valueUnbound(HttpSessionBindingEvent event) {  
  58.         HttpSession session = event.getSession();  
  59.         String name = event.getName();  
  60.         log.info(this + "被从session "" + session.getId() + ""的" + name + "属性上移除");  
  61.     }  
  62.   
  63.     @Override  
  64.     public String toString() {  
  65.         return "PersonInfo(" + name + ")";  
  66.     }  
  67.       
  68.     public String getName() {  
  69.         return name;  
  70.     }  
  71.   
  72.     public void setName(String name) {  
  73.         this.name = name;  
  74.     }  
  75.   
  76.     public Date getDateCreated() {  
  77.         return dateCreated;  
  78.     }  
  79.   
  80.     public void setDateCreated(Date dateCreated) {  
  81.         this.dateCreated = dateCreated;  
  82.     }  
  83.   
  84.   
  85. }  



三、Listener 实例

(一)单态登录:一个账号只能在一台机器上登录。

1、Listener 的代码:

[java] view plain copy
 
  1. package servlet.listener.singleton;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import javax.servlet.http.HttpSession;  
  7. import javax.servlet.http.HttpSessionAttributeListener;  
  8. import javax.servlet.http.HttpSessionBindingEvent;  
  9.   
  10. import org.apache.commons.logging.Log;  
  11. import org.apache.commons.logging.LogFactory;  
  12.   
  13. /** 
  14.  *  
  15.  * LoginSessionListener.java 
  16.  * 
  17.  * @title 实现单态登录的监听器 
  18.  * @description 
  19.  * @author SAM-SHO  
  20.  * @Date 2014-12-10 
  21.  */  
  22. public class LoginSessionListener implements HttpSessionAttributeListener {  
  23.   
  24.     Log log = LogFactory.getLog(this.getClass());  
  25.   
  26.     Map<String, HttpSession> map = new HashMap<String, HttpSession>();  
  27.   
  28.     public void attributeAdded(HttpSessionBindingEvent event) {  
  29.   
  30.         String name = event.getName();  
  31.   
  32.         // 登录  
  33.         if (name.equals("personInfo")) {  
  34.   
  35.             PersonInfo personInfo = (PersonInfo) event.getValue();  
  36.   
  37.             if (map.get(personInfo.getAccount()) != null) {  
  38.   
  39.                 // map 中有记录,表明该帐号在其他机器上登录过,将以前的登录失效  
  40.                 HttpSession session = map.get(personInfo.getAccount());  
  41.                 PersonInfo oldPersonInfo = (PersonInfo) session.getAttribute("personInfo");//map已经存在的旧的信息  
  42.   
  43.                 log.info("帐号" + oldPersonInfo.getAccount() + "在" + oldPersonInfo.getIp() + "已经登录,该登录将被迫下线。");  
  44.   
  45.                 session.removeAttribute("personInfo");  
  46.                 session.setAttribute("msg", "您的帐号已经在其他机器上登录,您被迫下线。");  
  47.             }  
  48.   
  49.             // 将session以用户名为索引,放入map中  
  50.             map.put(personInfo.getAccount(), event.getSession());  
  51.             log.info("帐号" + personInfo.getAccount() + "在" + personInfo.getIp() + "登录。");  
  52.         }  
  53.     }  
  54.   
  55.     public void attributeRemoved(HttpSessionBindingEvent event) {  
  56.   
  57.         String name = event.getName();  
  58.   
  59.         // 注销  
  60.         if (name.equals("personInfo")) {  
  61.             // 将该session从map中移除  
  62.             PersonInfo personInfo = (PersonInfo) event.getValue();  
  63.             map.remove(personInfo.getAccount());  
  64.             log.info("帐号" + personInfo.getAccount() + "注销。");  
  65.         }  
  66.     }  
  67.   
  68.     public void attributeReplaced(HttpSessionBindingEvent event) {  
  69.   
  70.         String name = event.getName();  
  71.   
  72.         // 没有注销的情况下,用另一个帐号登录  
  73.         if (name.equals("personInfo")) {  
  74.   
  75.             // 移除旧的的登录信息  
  76.             PersonInfo oldPersonInfo = (PersonInfo) event.getValue();  
  77.             map.remove(oldPersonInfo.getAccount());  
  78.   
  79.             // 新的登录信息  
  80.             PersonInfo personInfo = (PersonInfo) event.getSession().getAttribute("personInfo");  
  81.   
  82.             // 也要检查新登录的帐号是否在别的机器上登录过  
  83.             if (map.get(personInfo.getAccount()) != null) {  
  84.                 // map 中有记录,表明该帐号在其他机器上登录过,将以前的登录失效  
  85.                 HttpSession session = map.get(personInfo.getAccount());  
  86.                 session.removeAttribute("personInfo");  
  87.                 session.setAttribute("msg", "您的帐号已经在其他机器上登录,您被迫下线。");  
  88.             }  
  89.             map.put("personInfo", event.getSession());  
  90.         }  
  91.   
  92.     }  
  93.   
  94. }  
[java] view plain copy
 
  1. package servlet.listener.singleton;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. /** 
  7.  *  
  8.  * PersonInfo.java 
  9.  * 
  10.  * @title  
  11.  * @description 
  12.  * @author SAM-SHO  
  13.  * @Date 2014-12-10 
  14.  */  
  15. public class PersonInfo implements Serializable {  
  16.   
  17.     private static final long serialVersionUID = 4063725584941336123L;  
  18.   
  19.     // 帐号  
  20.     private String account;  
  21.   
  22.     // 登录IP地址  
  23.     private String ip;  
  24.   
  25.     // 登录时间  
  26.     private Date loginDate;  
  27.   
  28.     public String getAccount() {  
  29.         return account;  
  30.     }  
  31.   
  32.     public void setAccount(String account) {  
  33.         this.account = account;  
  34.     }  
  35.   
  36.     public String getIp() {  
  37.         return ip;  
  38.     }  
  39.   
  40.     public void setIp(String ip) {  
  41.         this.ip = ip;  
  42.     }  
  43.   
  44.     public Date getLoginDate() {  
  45.         return loginDate;  
  46.     }  
  47.   
  48.     public void setLoginDate(Date loginDate) {  
  49.         this.loginDate = loginDate;  
  50.     }  
  51.   
  52.     @Override  
  53.     public int hashCode() {  
  54.         final int prime = 31;  
  55.         int result = 1;  
  56.         result = prime * result + ((account == null) ? 0 : account.hashCode());  
  57.         result = prime * result + ((ip == null) ? 0 : ip.hashCode());  
  58.         return result;  
  59.     }  
  60.   
  61.     @Override  
  62.     public boolean equals(Object obj) {  
  63.         if (this == obj)  
  64.             return true;  
  65.         if (obj == null)  
  66.             return false;  
  67.         if (getClass() != obj.getClass())  
  68.             return false;  
  69.         PersonInfo other = (PersonInfo) obj;  
  70.         if (account == null) {  
  71.             if (other.account != null)  
  72.                 return false;  
  73.         } else if (!account.equals(other.account))  
  74.             return false;  
  75.         if (ip == null) {  
  76.             if (other.ip != null)  
  77.                 return false;  
  78.         } else if (!ip.equals(other.ip))  
  79.             return false;  
  80.         return true;  
  81.     }  
  82.   
  83.   
  84.   
  85. }  
[html] view plain copy
 
  1. <!-- 单态登录监听器 -->  
  2. <listener>  
  3.     <listener-class>servlet.listener.singleton.LoginSessionListener</listener-class>  
  4. </listener>  



(二)显示在线人数:会需要3个监听器。

1、ContextListener:获取服务启动的时间等。

2、RequestListener:获取客户端的IP、访问地址,访问次数等。

3、SessionListener:需要监听 Session 的创建与属性变化。

4、代码如下:

[java] view plain copy
 
  1. package com.helloweenvsfei.listener;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import javax.servlet.ServletContextEvent;  
  6. import javax.servlet.ServletContextListener;  
  7.   
  8. import com.helloweenvsfei.util.ApplicationConstants;  
  9.   
  10. public class MyContextListener implements ServletContextListener {  
  11.   
  12.     public void contextInitialized(ServletContextEvent event) {  
  13.         // 启动时,记录服务器启动时间  
  14.         ApplicationConstants.START_DATE = new Date();  
  15.     }  
  16.   
  17.     public void contextDestroyed(ServletContextEvent event) {  
  18.         // 关闭时,将结果清除。也可以将结果保存到硬盘上。  
  19.         ApplicationConstants.START_DATE = null;  
  20.         ApplicationConstants.MAX_ONLINE_COUNT_DATE = null;  
  21.     }  
  22. }  



[java] view plain copy
 
  1. package com.helloweenvsfei.listener;  
  2.   
  3. import javax.servlet.ServletRequestEvent;  
  4. import javax.servlet.ServletRequestListener;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpSession;  
  7.   
  8. public class MyRequestListener implements ServletRequestListener {  
  9.   
  10.     public void requestDestroyed(ServletRequestEvent event) {  
  11.     }  
  12.   
  13.     public void requestInitialized(ServletRequestEvent event) {  
  14.   
  15.         HttpServletRequest request = (HttpServletRequest) event  
  16.                 .getServletRequest();  
  17.   
  18.         HttpSession session = request.getSession(true);  
  19.   
  20.         // 记录IP地址  
  21.         session.setAttribute("ip", request.getRemoteAddr());  
  22.   
  23.         // 记录访问次数,只记录访问 .html, .do, .jsp, .action 的累计次数  
  24.         String uri = request.getRequestURI();  
  25.         String[] suffix = { ".html", ".do", ".jsp", ".action" };  
  26.         for (int i=0; i<suffix.length; i++) {  
  27.             if (uri.endsWith(suffix[i])) {  
  28.                 break;  
  29.             }  
  30.             if(i == suffix.length-1)  
  31.                 return;  
  32.         }  
  33.   
  34.         Integer activeTimes = (Integer) session.getAttribute("activeTimes");  
  35.   
  36.         if (activeTimes == null) {  
  37.             activeTimes = 0;  
  38.         }  
  39.   
  40.         session.setAttribute("activeTimes", activeTimes + 1);  
  41.     }  
  42. }  
[java] view plain copy
 
    1. package com.helloweenvsfei.listener;  
    2.   
    3. import java.util.Date;  
    4.   
    5. import javax.servlet.http.HttpSession;  
    6. import javax.servlet.http.HttpSessionAttributeListener;  
    7. import javax.servlet.http.HttpSessionBindingEvent;  
    8. import javax.servlet.http.HttpSessionEvent;  
    9. import javax.servlet.http.HttpSessionListener;  
    10.   
    11. import com.helloweenvsfei.util.ApplicationConstants;  
    12.   
    13. public class MySessionListener implements HttpSessionListener,  
    14.         HttpSessionAttributeListener {  
    15.   
    16.     public void sessionCreated(HttpSessionEvent sessionEvent) {  
    17.   
    18.         HttpSession session = sessionEvent.getSession();  
    19.   
    20.         // 将 session 放入 map  
    21.         ApplicationConstants.SESSION_MAP.put(session.getId(), session);  
    22.         // 总访问人数++  
    23.         ApplicationConstants.TOTAL_HISTORY_COUNT++;  
    24.   
    25.         // 如果当前在线人数超过历史记录,则更新最大在线人数,并记录时间  
    26.         if (ApplicationConstants.SESSION_MAP.size() > ApplicationConstants.MAX_ONLINE_COUNT) {  
    27.             ApplicationConstants.MAX_ONLINE_COUNT = ApplicationConstants.SESSION_MAP  
    28.                     .size();  
    29.             ApplicationConstants.MAX_ONLINE_COUNT_DATE = new Date();  
    30.         }  
    31.     }  
    32.   
    33.     public void sessionDestroyed(HttpSessionEvent sessionEvent) {  
    34.         HttpSession session = sessionEvent.getSession();  
    35.         // 将session从map中移除  
    36.         ApplicationConstants.SESSION_MAP.remove(session.getId());  
    37.     }  
    38.   
    39.     public void attributeAdded(HttpSessionBindingEvent event) {  
    40.   
    41.         if (event.getName().equals("personInfo")) {  
    42.   
    43.             // 当前登录用户数++  
    44.             ApplicationConstants.CURRENT_LOGIN_COUNT++;  
    45.             HttpSession session = event.getSession();  
    46.   
    47.             // 查找该帐号有没有在其他机器上登录  
    48.             for (HttpSession sess : ApplicationConstants.SESSION_MAP.values()) {  
    49.   
    50.                 // 如果该帐号已经在其他机器上登录,则以前的登录失效  
    51.                 if (event.getValue().equals(sess.getAttribute("personInfo"))  
    52.                         && session.getId() != sess.getId()) {  
    53.                     sess.invalidate();  
    54.                 }  
    55.             }  
    56.         }  
    57.     }  
    58.   
    59.     public void attributeRemoved(HttpSessionBindingEvent event) {  
    60.   
    61.         // 注销 当前登录用户数--  
    62.         if (event.getName().equals("personInfo")) {  
    63.             ApplicationConstants.CURRENT_LOGIN_COUNT--;  
    64.         }  
    65.     }  
    66.   
    67.     public void attributeReplaced(HttpSessionBindingEvent event) {  
    68.   
    69.         // 重新登录  
    70.         if (event.getName().equals("personInfo")) {  
    71.             HttpSession session = event.getSession();  
    72.             for (HttpSession sess : ApplicationConstants.SESSION_MAP.values()) {  
    73.                 // 如果新帐号在其他机器上登录过,则以前登录失效  
    74.                 if (event.getValue().equals(sess.getAttribute("personInfo"))  
    75.                         && session.getId() != sess.getId()) {  
    76.                     sess.invalidate();  
    77.                 }  
    78.             }  
    79.         }  
    80.     }  
    81.   
    82. }  

一、简介

(一)概述

1、Listener 用于监听 Java web程序中的事件,例如创建、修改、删除Session、request、context等,并触发响应的事件。

2、 Listener 对应观察者模式,事件发生的时候会自动触发该事件对应的Listeer。 Listener 主要用于对 Session、request、context 进行监控。servlet2.5 规范中共有 8 种Listener  。

(二)实现

1、不同功能的Listener 需要实现不同的 Listener  接口,一个Listener也可以实现多个接口,这样就可以多种功能的监听器一起工作。

2、8种监听器可以分为三类:

1)监听 Session、request、context 的创建于销毁,分别为  

HttpSessionLister、ServletContextListener、ServletRequestListener

2)监听对象属性变化,分别为:

HttpSessionAttributeLister、ServletContextAttributeListener、ServletRequestAttributeListener 

3)监听Session 内的对象,分别为HttpSessionBindingListener 和 HttpSessionActivationListener。与上面六类不同,这两类 Listener 监听的是Session 内的对象,而非 Session 本身,不需要在 web.xml中配置。

2、实现web.xml的Listener配置。

1)<listener>标签与 <listener-class>

2)<listener>一般配置在 <servlet>便签的前面。

[java] view plain copy
 
  1. package servlet.listener;  
  2.   
  3. import javax.servlet.ServletContextEvent;  
  4. import javax.servlet.ServletContextListener;  
  5. /** 
  6.  *  
  7.  * MyListener.java 
  8.  * 
  9.  * @title Context监听器 
  10.  * @description 
  11.  * @author SAM-SHO  
  12.  * @Date 2014-9-25 
  13.  */  
  14. public class MyListener implements ServletContextListener {  
  15.   
  16.     public void contextDestroyed(ServletContextEvent sce) {  
  17.           
  18.     }  
  19.   
  20.     public void contextInitialized(ServletContextEvent sce) {  
  21.           
  22.     }  
  23.   
  24.       
  25. }  

[html] view plain copy
 
  1. <!--监听器 -->  
  2. <listener>  
  3.     <listener-class>servlet.listener.MyListener</listener-class>  
  4. </listener>  

二、八种类型监听器

(一)监听 Session、request、context 的创建于销毁。

 HttpSessionLister、ServletContextListener、ServletRequestListener

1、三种监听器的触发时机及使用:

2、实例:实现监听对象的创建与销毁

[java] view plain copy
 
  1. package servlet.listener;  
  2.   
  3. import javax.servlet.ServletContext;  
  4. import javax.servlet.ServletContextEvent;  
  5. import javax.servlet.ServletContextListener;  
  6. import javax.servlet.ServletRequestEvent;  
  7. import javax.servlet.ServletRequestListener;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpSession;  
  10. import javax.servlet.http.HttpSessionEvent;  
  11. import javax.servlet.http.HttpSessionListener;  
  12.   
  13. import org.apache.commons.logging.Log;  
  14. import org.apache.commons.logging.LogFactory;  
  15.   
  16. /** 
  17.  *  
  18.  * ListenerTest.java 
  19.  *  
  20.  * @title 监听对象的创建与销毁 
  21.  * @description 
  22.  * @author SAM-SHO 
  23.  * @Date 2014-12-10 
  24.  */  
  25. public class ListenerTest implements HttpSessionListener, ServletContextListener, ServletRequestListener {  
  26.   
  27.     Log log = LogFactory.getLog(getClass());  
  28.   
  29.     // 创建 session  
  30.     public void sessionCreated(HttpSessionEvent se) {  
  31.         HttpSession session = se.getSession();  
  32.         log.info("新创建一个session, ID为: " + session.getId());  
  33.     }  
  34.   
  35.     // 销毁 session  
  36.     public void sessionDestroyed(HttpSessionEvent se) {  
  37.         HttpSession session = se.getSession();  
  38.         log.info("销毁一个session, ID为: " + session.getId());  
  39.     }  
  40.   
  41.     // 加载 context  
  42.     public void contextInitialized(ServletContextEvent sce) {  
  43.         ServletContext servletContext = sce.getServletContext();  
  44.         log.info("即将启动" + servletContext.getContextPath());  
  45.     }  
  46.   
  47.     // 卸载 context  
  48.     public void contextDestroyed(ServletContextEvent sce) {  
  49.         ServletContext servletContext = sce.getServletContext();  
  50.         log.info("即将关闭" + servletContext.getContextPath());  
  51.     }  
  52.   
  53.     // 创建 request  
  54.     public void requestInitialized(ServletRequestEvent sre) {  
  55.   
  56.         HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();  
  57.   
  58.         String uri = request.getRequestURI();  
  59.         uri = request.getQueryString() == null ? uri : (uri + "?" + request.getQueryString());  
  60.   
  61.         request.setAttribute("dateCreated", System.currentTimeMillis());  
  62.   
  63.         log.info("IP " + request.getRemoteAddr() + " 请求 " + uri);  
  64.     }  
  65.   
  66.     // 销毁 request  
  67.     public void requestDestroyed(ServletRequestEvent sre) {  
  68.   
  69.         HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();  
  70.   
  71.         long time = System.currentTimeMillis() - (Long) request.getAttribute("dateCreated");  
  72.   
  73.         log.info(request.getRemoteAddr() + "请求处理结束, 用时" + time + "毫秒. ");  
  74.     }  
  75.   
  76. }  


(二)监听对象属性变化,分别为HttpSessionAttributeLister、ServletContextAttributeListener、ServletRequestAttributeListener 

1、三种监听器的触发时机及使用:

2、实例:实现对象属性的监听
[java] view plain copy
 
  1. package servlet.listener;  
  2.   
  3. import javax.servlet.http.HttpSession;  
  4. import javax.servlet.http.HttpSessionAttributeListener;  
  5. import javax.servlet.http.HttpSessionBindingEvent;  
  6.   
  7. import org.apache.commons.logging.Log;  
  8. import org.apache.commons.logging.LogFactory;  
  9. /** 
  10.  *  
  11.  * SessionAttributeListenerTest.java 
  12.  * 
  13.  * @title 监听Session对象的属性 
  14.  * @description 
  15.  * @author SAM-SHO  
  16.  * @Date 2014-12-10 
  17.  */  
  18. public class SessionAttributeListenerTest implements HttpSessionAttributeListener {  
  19.   
  20.     Log log = LogFactory.getLog(getClass());  
  21.   
  22.     // 添加属性  
  23.     public void attributeAdded(HttpSessionBindingEvent se) {  
  24.         HttpSession session = se.getSession();  
  25.         String name = se.getName();  
  26.         log.info("新建session属性:" + name + ", 值为:" + se.getValue());  
  27.     }  
  28.   
  29.     // 删除属性  
  30.     public void attributeRemoved(HttpSessionBindingEvent se) {  
  31.         HttpSession session = se.getSession();  
  32.         String name = se.getName();  
  33.         log.info("删除session属性:" + name + ", 值为:" + se.getValue());  
  34.     }  
  35.   
  36.     // 修改属性  
  37.     public void attributeReplaced(HttpSessionBindingEvent se) {  
  38.         HttpSession session = se.getSession();  
  39.         String name = se.getName();  
  40.         Object oldValue = se.getValue();  
  41.         log.info("修改session属性:" + name + ", 原值:" + oldValue + ", 新值:" + session.getAttribute(name));  
  42.     }  
  43. }  


(三)监听Session 内的对象,分别为HttpSessionBindingListener 和 HttpSessionActivationListener

1、触发时机及使用:对象必须实现Listener接口,不需要在web.xml中配置

2、实例:实现对象属性的监听

[java] view plain copy
 
  1. package servlet.listener;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. import javax.servlet.http.HttpSession;  
  7. import javax.servlet.http.HttpSessionActivationListener;  
  8. import javax.servlet.http.HttpSessionBindingEvent;  
  9. import javax.servlet.http.HttpSessionBindingListener;  
  10. import javax.servlet.http.HttpSessionEvent;  
  11.   
  12. import org.apache.commons.logging.Log;  
  13. import org.apache.commons.logging.LogFactory;  
  14. /** 
  15.  *  
  16.  * PersonInfo.java 
  17.  * 
  18.  * @title 同时实现多个接口 
  19.  * 被串行化,需要实现Serializable接口 
  20.  * @description 
  21.  * @author SAM-SHO  
  22.  * @Date 2014-12-10 
  23.  */  
  24. public class PersonInfo implements HttpSessionActivationListener, HttpSessionBindingListener, Serializable {  
  25.   
  26.     private static final long serialVersionUID = -4780592776386225973L;  
  27.   
  28.     Log log = LogFactory.getLog(getClass());  
  29.   
  30.     private String name;  
  31.     private Date dateCreated;  
  32.   
  33.   
  34.     // 从硬盘加载后  
  35.     public void sessionDidActivate(HttpSessionEvent se) {  
  36.         HttpSession session = se.getSession();  
  37.         log.info(this + "已经成功从硬盘中加载。sessionId: " + session.getId());  
  38.     }  
  39.   
  40.     // 即将被钝化到硬盘时  
  41.     public void sessionWillPassivate(HttpSessionEvent se) {  
  42.         HttpSession session = se.getSession();  
  43.         log.info(this + "即将保存到硬盘。sessionId: " + session.getId());  
  44.     }  
  45.   
  46.     // 被放进session前  
  47.     public void valueBound(HttpSessionBindingEvent event) {  
  48.         HttpSession session = event.getSession();  
  49.         String name = event.getName();  
  50.         log.info(this + "被绑定到session "" + session.getId() + ""的" + name + "属性上");  
  51.   
  52.         // 记录放到session中的时间  
  53.         this.setDateCreated(new Date());  
  54.     }  
  55.   
  56.     // 从session中移除后  
  57.     public void valueUnbound(HttpSessionBindingEvent event) {  
  58.         HttpSession session = event.getSession();  
  59.         String name = event.getName();  
  60.         log.info(this + "被从session "" + session.getId() + ""的" + name + "属性上移除");  
  61.     }  
  62.   
  63.     @Override  
  64.     public String toString() {  
  65.         return "PersonInfo(" + name + ")";  
  66.     }  
  67.       
  68.     public String getName() {  
  69.         return name;  
  70.     }  
  71.   
  72.     public void setName(String name) {  
  73.         this.name = name;  
  74.     }  
  75.   
  76.     public Date getDateCreated() {  
  77.         return dateCreated;  
  78.     }  
  79.   
  80.     public void setDateCreated(Date dateCreated) {  
  81.         this.dateCreated = dateCreated;  
  82.     }  
  83.   
  84.   
  85. }  


三、Listener 实例

(一)单态登录:一个账号只能在一台机器上登录。

1、Listener 的代码:

[java] view plain copy
 
  1. package servlet.listener.singleton;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import javax.servlet.http.HttpSession;  
  7. import javax.servlet.http.HttpSessionAttributeListener;  
  8. import javax.servlet.http.HttpSessionBindingEvent;  
  9.   
  10. import org.apache.commons.logging.Log;  
  11. import org.apache.commons.logging.LogFactory;  
  12.   
  13. /** 
  14.  *  
  15.  * LoginSessionListener.java 
  16.  * 
  17.  * @title 实现单态登录的监听器 
  18.  * @description 
  19.  * @author SAM-SHO  
  20.  * @Date 2014-12-10 
  21.  */  
  22. public class LoginSessionListener implements HttpSessionAttributeListener {  
  23.   
  24.     Log log = LogFactory.getLog(this.getClass());  
  25.   
  26.     Map<String, HttpSession> map = new HashMap<String, HttpSession>();  
  27.   
  28.     public void attributeAdded(HttpSessionBindingEvent event) {  
  29.   
  30.         String name = event.getName();  
  31.   
  32.         // 登录  
  33.         if (name.equals("personInfo")) {  
  34.   
  35.             PersonInfo personInfo = (PersonInfo) event.getValue();  
  36.   
  37.             if (map.get(personInfo.getAccount()) != null) {  
  38.   
  39.                 // map 中有记录,表明该帐号在其他机器上登录过,将以前的登录失效  
  40.                 HttpSession session = map.get(personInfo.getAccount());  
  41.                 PersonInfo oldPersonInfo = (PersonInfo) session.getAttribute("personInfo");//map已经存在的旧的信息  
  42.   
  43.                 log.info("帐号" + oldPersonInfo.getAccount() + "在" + oldPersonInfo.getIp() + "已经登录,该登录将被迫下线。");  
  44.   
  45.                 session.removeAttribute("personInfo");  
  46.                 session.setAttribute("msg", "您的帐号已经在其他机器上登录,您被迫下线。");  
  47.             }  
  48.   
  49.             // 将session以用户名为索引,放入map中  
  50.             map.put(personInfo.getAccount(), event.getSession());  
  51.             log.info("帐号" + personInfo.getAccount() + "在" + personInfo.getIp() + "登录。");  
  52.         }  
  53.     }  
  54.   
  55.     public void attributeRemoved(HttpSessionBindingEvent event) {  
  56.   
  57.         String name = event.getName();  
  58.   
  59.         // 注销  
  60.         if (name.equals("personInfo")) {  
  61.             // 将该session从map中移除  
  62.             PersonInfo personInfo = (PersonInfo) event.getValue();  
  63.             map.remove(personInfo.getAccount());  
  64.             log.info("帐号" + personInfo.getAccount() + "注销。");  
  65.         }  
  66.     }  
  67.   
  68.     public void attributeReplaced(HttpSessionBindingEvent event) {  
  69.   
  70.         String name = event.getName();  
  71.   
  72.         // 没有注销的情况下,用另一个帐号登录  
  73.         if (name.equals("personInfo")) {  
  74.   
  75.             // 移除旧的的登录信息  
  76.             PersonInfo oldPersonInfo = (PersonInfo) event.getValue();  
  77.             map.remove(oldPersonInfo.getAccount());  
  78.   
  79.             // 新的登录信息  
  80.             PersonInfo personInfo = (PersonInfo) event.getSession().getAttribute("personInfo");  
  81.   
  82.             // 也要检查新登录的帐号是否在别的机器上登录过  
  83.             if (map.get(personInfo.getAccount()) != null) {  
  84.                 // map 中有记录,表明该帐号在其他机器上登录过,将以前的登录失效  
  85.                 HttpSession session = map.get(personInfo.getAccount());  
  86.                 session.removeAttribute("personInfo");  
  87.                 session.setAttribute("msg", "您的帐号已经在其他机器上登录,您被迫下线。");  
  88.             }  
  89.             map.put("personInfo", event.getSession());  
  90.         }  
  91.   
  92.     }  
  93.   
  94. }  

[java] view plain copy
 
  1. package servlet.listener.singleton;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.Date;  
  5.   
  6. /** 
  7.  *  
  8.  * PersonInfo.java 
  9.  * 
  10.  * @title  
  11.  * @description 
  12.  * @author SAM-SHO  
  13.  * @Date 2014-12-10 
  14.  */  
  15. public class PersonInfo implements Serializable {  
  16.   
  17.     private static final long serialVersionUID = 4063725584941336123L;  
  18.   
  19.     // 帐号  
  20.     private String account;  
  21.   
  22.     // 登录IP地址  
  23.     private String ip;  
  24.   
  25.     // 登录时间  
  26.     private Date loginDate;  
  27.   
  28.     public String getAccount() {  
  29.         return account;  
  30.     }  
  31.   
  32.     public void setAccount(String account) {  
  33.         this.account = account;  
  34.     }  
  35.   
  36.     public String getIp() {  
  37.         return ip;  
  38.     }  
  39.   
  40.     public void setIp(String ip) {  
  41.         this.ip = ip;  
  42.     }  
  43.   
  44.     public Date getLoginDate() {  
  45.         return loginDate;  
  46.     }  
  47.   
  48.     public void setLoginDate(Date loginDate) {  
  49.         this.loginDate = loginDate;  
  50.     }  
  51.   
  52.     @Override  
  53.     public int hashCode() {  
  54.         final int prime = 31;  
  55.         int result = 1;  
  56.         result = prime * result + ((account == null) ? 0 : account.hashCode());  
  57.         result = prime * result + ((ip == null) ? 0 : ip.hashCode());  
  58.         return result;  
  59.     }  
  60.   
  61.     @Override  
  62.     public boolean equals(Object obj) {  
  63.         if (this == obj)  
  64.             return true;  
  65.         if (obj == null)  
  66.             return false;  
  67.         if (getClass() != obj.getClass())  
  68.             return false;  
  69.         PersonInfo other = (PersonInfo) obj;  
  70.         if (account == null) {  
  71.             if (other.account != null)  
  72.                 return false;  
  73.         } else if (!account.equals(other.account))  
  74.             return false;  
  75.         if (ip == null) {  
  76.             if (other.ip != null)  
  77.                 return false;  
  78.         } else if (!ip.equals(other.ip))  
  79.             return false;  
  80.         return true;  
  81.     }  
  82.   
  83.   
  84.   
  85. }  

[html] view plain copy
 
  1. <!-- 单态登录监听器 -->  
  2. <listener>  
  3.     <listener-class>servlet.listener.singleton.LoginSessionListener</listener-class>  
  4. </listener>  


(二)显示在线人数:会需要3个监听器。

1、ContextListener:获取服务启动的时间等。

2、RequestListener:获取客户端的IP、访问地址,访问次数等。

3、SessionListener:需要监听 Session 的创建与属性变化。

4、代码如下:

[java] view plain copy
 
  1. package com.helloweenvsfei.listener;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import javax.servlet.ServletContextEvent;  
  6. import javax.servlet.ServletContextListener;  
  7.   
  8. import com.helloweenvsfei.util.ApplicationConstants;  
  9.   
  10. public class MyContextListener implements ServletContextListener {  
  11.   
  12.     public void contextInitialized(ServletContextEvent event) {  
  13.         // 启动时,记录服务器启动时间  
  14.         ApplicationConstants.START_DATE = new Date();  
  15.     }  
  16.   
  17.     public void contextDestroyed(ServletContextEvent event) {  
  18.         // 关闭时,将结果清除。也可以将结果保存到硬盘上。  
  19.         ApplicationConstants.START_DATE = null;  
  20.         ApplicationConstants.MAX_ONLINE_COUNT_DATE = null;  
  21.     }  
  22. }  


[java] view plain copy
 
  1. package com.helloweenvsfei.listener;  
  2.   
  3. import javax.servlet.ServletRequestEvent;  
  4. import javax.servlet.ServletRequestListener;  
  5. import javax.servlet.http.HttpServletRequest;  
  6. import javax.servlet.http.HttpSession;  
  7.   
  8. public class MyRequestListener implements ServletRequestListener {  
  9.   
  10.     public void requestDestroyed(ServletRequestEvent event) {  
  11.     }  
  12.   
  13.     public void requestInitialized(ServletRequestEvent event) {  
  14.   
  15.         HttpServletRequest request = (HttpServletRequest) event  
  16.                 .getServletRequest();  
  17.   
  18.         HttpSession session = request.getSession(true);  
  19.   
  20.         // 记录IP地址  
  21.         session.setAttribute("ip", request.getRemoteAddr());  
  22.   
  23.         // 记录访问次数,只记录访问 .html, .do, .jsp, .action 的累计次数  
  24.         String uri = request.getRequestURI();  
  25.         String[] suffix = { ".html", ".do", ".jsp", ".action" };  
  26.         for (int i=0; i<suffix.length; i++) {  
  27.             if (uri.endsWith(suffix[i])) {  
  28.                 break;  
  29.             }  
  30.             if(i == suffix.length-1)  
  31.                 return;  
  32.         }  
  33.   
  34.         Integer activeTimes = (Integer) session.getAttribute("activeTimes");  
  35.   
  36.         if (activeTimes == null) {  
  37.             activeTimes = 0;  
  38.         }  
  39.   
  40.         session.setAttribute("activeTimes", activeTimes + 1);  
  41.     }  
  42. }  

[java] view plain copy
 
  1. package com.helloweenvsfei.listener;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import javax.servlet.http.HttpSession;  
  6. import javax.servlet.http.HttpSessionAttributeListener;  
  7. import javax.servlet.http.HttpSessionBindingEvent;  
  8. import javax.servlet.http.HttpSessionEvent;  
  9. import javax.servlet.http.HttpSessionListener;  
  10.   
  11. import com.helloweenvsfei.util.ApplicationConstants;  
  12.   
  13. public class MySessionListener implements HttpSessionListener,  
  14.         HttpSessionAttributeListener {  
  15.   
  16.     public void sessionCreated(HttpSessionEvent sessionEvent) {  
  17.   
  18.         HttpSession session = sessionEvent.getSession();  
  19.   
  20.         // 将 session 放入 map  
  21.         ApplicationConstants.SESSION_MAP.put(session.getId(), session);  
  22.         // 总访问人数++  
  23.         ApplicationConstants.TOTAL_HISTORY_COUNT++;  
  24.   
  25.         // 如果当前在线人数超过历史记录,则更新最大在线人数,并记录时间  
  26.         if (ApplicationConstants.SESSION_MAP.size() > ApplicationConstants.MAX_ONLINE_COUNT) {  
  27.             ApplicationConstants.MAX_ONLINE_COUNT = ApplicationConstants.SESSION_MAP  
  28.                     .size();  
  29.             ApplicationConstants.MAX_ONLINE_COUNT_DATE = new Date();  
  30.         }  
  31.     }  
  32.   
  33.     public void sessionDestroyed(HttpSessionEvent sessionEvent) {  
  34.         HttpSession session = sessionEvent.getSession();  
  35.         // 将session从map中移除  
  36.         ApplicationConstants.SESSION_MAP.remove(session.getId());  
  37.     }  
  38.   
  39.     public void attributeAdded(HttpSessionBindingEvent event) {  
  40.   
  41.         if (event.getName().equals("personInfo")) {  
  42.   
  43.             // 当前登录用户数++  
  44.             ApplicationConstants.CURRENT_LOGIN_COUNT++;  
  45.             HttpSession session = event.getSession();  
  46.   
  47.             // 查找该帐号有没有在其他机器上登录  
  48.             for (HttpSession sess : ApplicationConstants.SESSION_MAP.values()) {  
  49.   
  50.                 // 如果该帐号已经在其他机器上登录,则以前的登录失效  
  51.                 if (event.getValue().equals(sess.getAttribute("personInfo"))  
  52.                         && session.getId() != sess.getId()) {  
  53.                     sess.invalidate();  
  54.                 }  
  55.             }  
  56.         }  
  57.     }  
  58.   
  59.     public void attributeRemoved(HttpSessionBindingEvent event) {  
  60.   
  61.         // 注销 当前登录用户数--  
  62.         if (event.getName().equals("personInfo")) {  
  63.             ApplicationConstants.CURRENT_LOGIN_COUNT--;  
  64.         }  
  65.     }  
  66.   
  67.     public void attributeReplaced(HttpSessionBindingEvent event) {  
  68.   
  69.         // 重新登录  
  70.         if (event.getName().equals("personInfo")) {  
  71.             HttpSession session = event.getSession();  
  72.             for (HttpSession sess : ApplicationConstants.SESSION_MAP.values()) {  
  73.                 // 如果新帐号在其他机器上登录过,则以前登录失效  
  74.                 if (event.getValue().equals(sess.getAttribute("personInfo"))  
  75.                         && session.getId() != sess.getId()) {  
  76.                     sess.invalidate();  
  77.                 }  
  78.             }  
  79.         }  
  80.     }  
  81.   
  82. }  
原文地址:https://www.cnblogs.com/heyanan/p/7449453.html