Servlet 监听器Listener详解
转自:http://blog.csdn.net/u012228718/article/details/41730799
一、简介
(一)概述
1、Listener 用于监听 Javaweb程序中的事件,例如创建、修改、删除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>便签的前面。
- package servlet.listener;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- /**
- *
- * MyListener.java
- *
- * @title Context监听器
- * @description
- * @author SAM-SHO
- * @Date 2014-9-25
- */
- public class MyListener implements ServletContextListener {
- public void contextDestroyed(ServletContextEvent sce) {
- }
- public void contextInitialized(ServletContextEvent sce) {
- }
- }
- <!--监听器 -->
- <listener>
- <listener-class>servlet.listener.MyListener</listener-class>
- </listener>
二、八种类型监听器
(一)监听 Session、request、context 的创建于销毁。
HttpSessionLister、ServletContextListener、ServletRequestListener
1、三种监听器的触发时机及使用:
2、实例:实现监听对象的创建与销毁
- package servlet.listener;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import javax.servlet.ServletRequestEvent;
- import javax.servlet.ServletRequestListener;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- /**
- *
- * ListenerTest.java
- *
- * @title 监听对象的创建与销毁
- * @description
- * @author SAM-SHO
- * @Date 2014-12-10
- */
- public class ListenerTest implements HttpSessionListener, ServletContextListener, ServletRequestListener {
- Log log = LogFactory.getLog(getClass());
- // 创建 session
- public void sessionCreated(HttpSessionEvent se) {
- HttpSession session = se.getSession();
- log.info("新创建一个session, ID为: " + session.getId());
- }
- // 销毁 session
- public void sessionDestroyed(HttpSessionEvent se) {
- HttpSession session = se.getSession();
- log.info("销毁一个session, ID为: " + session.getId());
- }
- // 加载 context
- public void contextInitialized(ServletContextEvent sce) {
- ServletContext servletContext = sce.getServletContext();
- log.info("即将启动" + servletContext.getContextPath());
- }
- // 卸载 context
- public void contextDestroyed(ServletContextEvent sce) {
- ServletContext servletContext = sce.getServletContext();
- log.info("即将关闭" + servletContext.getContextPath());
- }
- // 创建 request
- public void requestInitialized(ServletRequestEvent sre) {
- HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
- String uri = request.getRequestURI();
- uri = request.getQueryString() == null ? uri : (uri + "?" + request.getQueryString());
- request.setAttribute("dateCreated", System.currentTimeMillis());
- log.info("IP " + request.getRemoteAddr() + " 请求 " + uri);
- }
- // 销毁 request
- public void requestDestroyed(ServletRequestEvent sre) {
- HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
- long time = System.currentTimeMillis() - (Long) request.getAttribute("dateCreated");
- log.info(request.getRemoteAddr() + "请求处理结束, 用时" + time + "毫秒. ");
- }
- }
(二)监听对象属性变化,分别为HttpSessionAttributeLister、ServletContextAttributeListener、ServletRequestAttributeListener
1、三种监听器的触发时机及使用:
2、实例:实现对象属性的监听
- package servlet.listener;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpSessionAttributeListener;
- import javax.servlet.http.HttpSessionBindingEvent;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- /**
- *
- * SessionAttributeListenerTest.java
- *
- * @title 监听Session对象的属性
- * @description
- * @author SAM-SHO
- * @Date 2014-12-10
- */
- public class SessionAttributeListenerTest implements HttpSessionAttributeListener {
- Log log = LogFactory.getLog(getClass());
- // 添加属性
- public void attributeAdded(HttpSessionBindingEvent se) {
- HttpSession session = se.getSession();
- String name = se.getName();
- log.info("新建session属性:" + name + ", 值为:" + se.getValue());
- }
- // 删除属性
- public void attributeRemoved(HttpSessionBindingEvent se) {
- HttpSession session = se.getSession();
- String name = se.getName();
- log.info("删除session属性:" + name + ", 值为:" + se.getValue());
- }
- // 修改属性
- public void attributeReplaced(HttpSessionBindingEvent se) {
- HttpSession session = se.getSession();
- String name = se.getName();
- Object oldValue = se.getValue();
- log.info("修改session属性:" + name + ", 原值:" + oldValue + ", 新值:" + session.getAttribute(name));
- }
- }
(三)监听Session 内的对象,分别为HttpSessionBindingListener 和 HttpSessionActivationListener
1、触发时机及使用:对象必须实现Listener接口,不需要在web.xml中配置
2、实例:实现对象属性的监听
- package servlet.listener;
- import java.io.Serializable;
- import java.util.Date;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpSessionActivationListener;
- import javax.servlet.http.HttpSessionBindingEvent;
- import javax.servlet.http.HttpSessionBindingListener;
- import javax.servlet.http.HttpSessionEvent;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- /**
- *
- * PersonInfo.java
- *
- * @title 同时实现多个接口
- * 被串行化,需要实现Serializable接口
- * @description
- * @author SAM-SHO
- * @Date 2014-12-10
- */
- public class PersonInfo implements HttpSessionActivationListener, HttpSessionBindingListener, Serializable {
- private static final long serialVersionUID = -4780592776386225973L;
- Log log = LogFactory.getLog(getClass());
- private String name;
- private Date dateCreated;
- // 从硬盘加载后
- public void sessionDidActivate(HttpSessionEvent se) {
- HttpSession session = se.getSession();
- log.info(this + "已经成功从硬盘中加载。sessionId: " + session.getId());
- }
- // 即将被钝化到硬盘时
- public void sessionWillPassivate(HttpSessionEvent se) {
- HttpSession session = se.getSession();
- log.info(this + "即将保存到硬盘。sessionId: " + session.getId());
- }
- // 被放进session前
- public void valueBound(HttpSessionBindingEvent event) {
- HttpSession session = event.getSession();
- String name = event.getName();
- log.info(this + "被绑定到session \"" + session.getId() + "\"的" + name + "属性上");
- // 记录放到session中的时间
- this.setDateCreated(new Date());
- }
- // 从session中移除后
- public void valueUnbound(HttpSessionBindingEvent event) {
- HttpSession session = event.getSession();
- String name = event.getName();
- log.info(this + "被从session \"" + session.getId() + "\"的" + name + "属性上移除");
- }
- @Override
- public String toString() {
- return "PersonInfo(" + name + ")";
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Date getDateCreated() {
- return dateCreated;
- }
- public void setDateCreated(Date dateCreated) {
- this.dateCreated = dateCreated;
- }
- }
三、Listener 实例
(一)单态登录:一个账号只能在一台机器上登录。
1、Listener 的代码:
- package servlet.listener.singleton;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpSessionAttributeListener;
- import javax.servlet.http.HttpSessionBindingEvent;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- /**
- *
- * LoginSessionListener.java
- *
- * @title 实现单态登录的监听器
- * @description
- * @author SAM-SHO
- * @Date 2014-12-10
- */
- public class LoginSessionListener implements HttpSessionAttributeListener {
- Log log = LogFactory.getLog(this.getClass());
- Map<String, HttpSession> map = new HashMap<String, HttpSession>();
- public void attributeAdded(HttpSessionBindingEvent event) {
- String name = event.getName();
- // 登录
- if (name.equals("personInfo")) {
- PersonInfo personInfo = (PersonInfo) event.getValue();
- if (map.get(personInfo.getAccount()) != null) {
- // map 中有记录,表明该帐号在其他机器上登录过,将以前的登录失效
- HttpSession session = map.get(personInfo.getAccount());
- PersonInfo oldPersonInfo = (PersonInfo) session.getAttribute("personInfo");//map已经存在的旧的信息
- log.info("帐号" + oldPersonInfo.getAccount() + "在" + oldPersonInfo.getIp() + "已经登录,该登录将被迫下线。");
- session.removeAttribute("personInfo");
- session.setAttribute("msg", "您的帐号已经在其他机器上登录,您被迫下线。");
- }
- // 将session以用户名为索引,放入map中
- map.put(personInfo.getAccount(), event.getSession());
- log.info("帐号" + personInfo.getAccount() + "在" + personInfo.getIp() + "登录。");
- }
- }
- public void attributeRemoved(HttpSessionBindingEvent event) {
- String name = event.getName();
- // 注销
- if (name.equals("personInfo")) {
- // 将该session从map中移除
- PersonInfo personInfo = (PersonInfo) event.getValue();
- map.remove(personInfo.getAccount());
- log.info("帐号" + personInfo.getAccount() + "注销。");
- }
- }
- public void attributeReplaced(HttpSessionBindingEvent event) {
- String name = event.getName();
- // 没有注销的情况下,用另一个帐号登录
- if (name.equals("personInfo")) {
- // 移除旧的的登录信息
- PersonInfo oldPersonInfo = (PersonInfo) event.getValue();
- map.remove(oldPersonInfo.getAccount());
- // 新的登录信息
- PersonInfo personInfo = (PersonInfo) event.getSession().getAttribute("personInfo");
- // 也要检查新登录的帐号是否在别的机器上登录过
- if (map.get(personInfo.getAccount()) != null) {
- // map 中有记录,表明该帐号在其他机器上登录过,将以前的登录失效
- HttpSession session = map.get(personInfo.getAccount());
- session.removeAttribute("personInfo");
- session.setAttribute("msg", "您的帐号已经在其他机器上登录,您被迫下线。");
- }
- map.put("personInfo", event.getSession());
- }
- }
- }
- package servlet.listener.singleton;
- import java.io.Serializable;
- import java.util.Date;
- /**
- *
- * PersonInfo.java
- *
- * @title
- * @description
- * @author SAM-SHO
- * @Date 2014-12-10
- */
- public class PersonInfo implements Serializable {
- private static final long serialVersionUID = 4063725584941336123L;
- // 帐号
- private String account;
- // 登录IP地址
- private String ip;
- // 登录时间
- private Date loginDate;
- public String getAccount() {
- return account;
- }
- public void setAccount(String account) {
- this.account = account;
- }
- public String getIp() {
- return ip;
- }
- public void setIp(String ip) {
- this.ip = ip;
- }
- public Date getLoginDate() {
- return loginDate;
- }
- public void setLoginDate(Date loginDate) {
- this.loginDate = loginDate;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((account == null) ? 0 : account.hashCode());
- result = prime * result + ((ip == null) ? 0 : ip.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- PersonInfo other = (PersonInfo) obj;
- if (account == null) {
- if (other.account != null)
- return false;
- } else if (!account.equals(other.account))
- return false;
- if (ip == null) {
- if (other.ip != null)
- return false;
- } else if (!ip.equals(other.ip))
- return false;
- return true;
- }
- }
- <!-- 单态登录监听器 -->
- <listener>
- <listener-class>servlet.listener.singleton.LoginSessionListener</listener-class>
- </listener>
(二)显示在线人数:会需要3个监听器。
1、ContextListener:获取服务启动的时间等。
2、RequestListener:获取客户端的IP、访问地址,访问次数等。
3、SessionListener:需要监听 Session 的创建与属性变化。
4、代码如下:
- package com.helloweenvsfei.listener;
- import java.util.Date;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import com.helloweenvsfei.util.ApplicationConstants;
- public class MyContextListener implements ServletContextListener {
- public void contextInitialized(ServletContextEvent event) {
- // 启动时,记录服务器启动时间
- ApplicationConstants.START_DATE = new Date();
- }
- public void contextDestroyed(ServletContextEvent event) {
- // 关闭时,将结果清除。也可以将结果保存到硬盘上。
- ApplicationConstants.START_DATE = null;
- ApplicationConstants.MAX_ONLINE_COUNT_DATE = null;
- }
- }
- package com.helloweenvsfei.listener;
- import javax.servlet.ServletRequestEvent;
- import javax.servlet.ServletRequestListener;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- public class MyRequestListener implements ServletRequestListener {
- public void requestDestroyed(ServletRequestEvent event) {
- }
- public void requestInitialized(ServletRequestEvent event) {
- HttpServletRequest request = (HttpServletRequest) event
- .getServletRequest();
- HttpSession session = request.getSession(true);
- // 记录IP地址
- session.setAttribute("ip", request.getRemoteAddr());
- // 记录访问次数,只记录访问 .html, .do, .jsp, .action 的累计次数
- String uri = request.getRequestURI();
- String[] suffix = { ".html", ".do", ".jsp", ".action" };
- for (int i=0; i<suffix.length; i++) {
- if (uri.endsWith(suffix[i])) {
- break;
- }
- if(i == suffix.length-1)
- return;
- }
- Integer activeTimes = (Integer) session.getAttribute("activeTimes");
- if (activeTimes == null) {
- activeTimes = 0;
- }
- session.setAttribute("activeTimes", activeTimes + 1);
- }
- }
- package com.helloweenvsfei.listener;
- import java.util.Date;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpSessionAttributeListener;
- import javax.servlet.http.HttpSessionBindingEvent;
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
- import com.helloweenvsfei.util.ApplicationConstants;
- public class MySessionListener implements HttpSessionListener,
- HttpSessionAttributeListener {
- public void sessionCreated(HttpSessionEvent sessionEvent) {
- HttpSession session = sessionEvent.getSession();
- // 将 session 放入 map
- ApplicationConstants.SESSION_MAP.put(session.getId(), session);
- // 总访问人数++
- ApplicationConstants.TOTAL_HISTORY_COUNT++;
- // 如果当前在线人数超过历史记录,则更新最大在线人数,并记录时间
- if (ApplicationConstants.SESSION_MAP.size() > ApplicationConstants.MAX_ONLINE_COUNT) {
- ApplicationConstants.MAX_ONLINE_COUNT = ApplicationConstants.SESSION_MAP
- .size();
- ApplicationConstants.MAX_ONLINE_COUNT_DATE = new Date();
- }
- }
- public void sessionDestroyed(HttpSessionEvent sessionEvent) {
- HttpSession session = sessionEvent.getSession();
- // 将session从map中移除
- ApplicationConstants.SESSION_MAP.remove(session.getId());
- }
- public void attributeAdded(HttpSessionBindingEvent event) {
- if (event.getName().equals("personInfo")) {
- // 当前登录用户数++
- ApplicationConstants.CURRENT_LOGIN_COUNT++;
- HttpSession session = event.getSession();
- // 查找该帐号有没有在其他机器上登录
- for (HttpSession sess : ApplicationConstants.SESSION_MAP.values()) {
- // 如果该帐号已经在其他机器上登录,则以前的登录失效
- if (event.getValue().equals(sess.getAttribute("personInfo"))
- && session.getId() != sess.getId()) {
- sess.invalidate();
- }
- }
- }
- }
- public void attributeRemoved(HttpSessionBindingEvent event) {
- // 注销 当前登录用户数--
- if (event.getName().equals("personInfo")) {
- ApplicationConstants.CURRENT_LOGIN_COUNT--;
- }
- }
- public void attributeReplaced(HttpSessionBindingEvent event) {
- // 重新登录
- if (event.getName().equals("personInfo")) {
- HttpSession session = event.getSession();
- for (HttpSession sess : ApplicationConstants.SESSION_MAP.values()) {
- // 如果新帐号在其他机器上登录过,则以前登录失效
- if (event.getValue().equals(sess.getAttribute("personInfo"))
- && session.getId() != sess.getId()) {
- sess.invalidate();
- }
- }
- }
- }
- }
一、简介
(一)概述
1、Listener 用于监听 Javaweb程序中的事件,例如创建、修改、删除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>便签的前面。
- package servlet.listener;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- /**
- *
- * MyListener.java
- *
- * @title Context监听器
- * @description
- * @author SAM-SHO
- * @Date 2014-9-25
- */
- public class MyListener implements ServletContextListener {
- public void contextDestroyed(ServletContextEvent sce) {
- }
- public void contextInitialized(ServletContextEvent sce) {
- }
- }
- <!--监听器 -->
- <listener>
- <listener-class>servlet.listener.MyListener</listener-class>
- </listener>
二、八种类型监听器
(一)监听 Session、request、context 的创建于销毁。
HttpSessionLister、ServletContextListener、ServletRequestListener
1、三种监听器的触发时机及使用:
2、实例:实现监听对象的创建与销毁
- package servlet.listener;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import javax.servlet.ServletRequestEvent;
- import javax.servlet.ServletRequestListener;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- /**
- *
- * ListenerTest.java
- *
- * @title 监听对象的创建与销毁
- * @description
- * @author SAM-SHO
- * @Date 2014-12-10
- */
- public class ListenerTest implements HttpSessionListener, ServletContextListener, ServletRequestListener {
- Log log = LogFactory.getLog(getClass());
- // 创建 session
- public void sessionCreated(HttpSessionEvent se) {
- HttpSession session = se.getSession();
- log.info("新创建一个session, ID为: " + session.getId());
- }
- // 销毁 session
- public void sessionDestroyed(HttpSessionEvent se) {
- HttpSession session = se.getSession();
- log.info("销毁一个session, ID为: " + session.getId());
- }
- // 加载 context
- public void contextInitialized(ServletContextEvent sce) {
- ServletContext servletContext = sce.getServletContext();
- log.info("即将启动" + servletContext.getContextPath());
- }
- // 卸载 context
- public void contextDestroyed(ServletContextEvent sce) {
- ServletContext servletContext = sce.getServletContext();
- log.info("即将关闭" + servletContext.getContextPath());
- }
- // 创建 request
- public void requestInitialized(ServletRequestEvent sre) {
- HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
- String uri = request.getRequestURI();
- uri = request.getQueryString() == null ? uri : (uri + "?" + request.getQueryString());
- request.setAttribute("dateCreated", System.currentTimeMillis());
- log.info("IP " + request.getRemoteAddr() + " 请求 " + uri);
- }
- // 销毁 request
- public void requestDestroyed(ServletRequestEvent sre) {
- HttpServletRequest request = (HttpServletRequest) sre.getServletRequest();
- long time = System.currentTimeMillis() - (Long) request.getAttribute("dateCreated");
- log.info(request.getRemoteAddr() + "请求处理结束, 用时" + time + "毫秒. ");
- }
- }
(二)监听对象属性变化,分别为HttpSessionAttributeLister、ServletContextAttributeListener、ServletRequestAttributeListener
1、三种监听器的触发时机及使用:
2、实例:实现对象属性的监听
- package servlet.listener;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpSessionAttributeListener;
- import javax.servlet.http.HttpSessionBindingEvent;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- /**
- *
- * SessionAttributeListenerTest.java
- *
- * @title 监听Session对象的属性
- * @description
- * @author SAM-SHO
- * @Date 2014-12-10
- */
- public class SessionAttributeListenerTest implements HttpSessionAttributeListener {
- Log log = LogFactory.getLog(getClass());
- // 添加属性
- public void attributeAdded(HttpSessionBindingEvent se) {
- HttpSession session = se.getSession();
- String name = se.getName();
- log.info("新建session属性:" + name + ", 值为:" + se.getValue());
- }
- // 删除属性
- public void attributeRemoved(HttpSessionBindingEvent se) {
- HttpSession session = se.getSession();
- String name = se.getName();
- log.info("删除session属性:" + name + ", 值为:" + se.getValue());
- }
- // 修改属性
- public void attributeReplaced(HttpSessionBindingEvent se) {
- HttpSession session = se.getSession();
- String name = se.getName();
- Object oldValue = se.getValue();
- log.info("修改session属性:" + name + ", 原值:" + oldValue + ", 新值:" + session.getAttribute(name));
- }
- }
(三)监听Session 内的对象,分别为HttpSessionBindingListener 和 HttpSessionActivationListener
1、触发时机及使用:对象必须实现Listener接口,不需要在web.xml中配置
2、实例:实现对象属性的监听
- package servlet.listener;
- import java.io.Serializable;
- import java.util.Date;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpSessionActivationListener;
- import javax.servlet.http.HttpSessionBindingEvent;
- import javax.servlet.http.HttpSessionBindingListener;
- import javax.servlet.http.HttpSessionEvent;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- /**
- *
- * PersonInfo.java
- *
- * @title 同时实现多个接口
- * 被串行化,需要实现Serializable接口
- * @description
- * @author SAM-SHO
- * @Date 2014-12-10
- */
- public class PersonInfo implements HttpSessionActivationListener, HttpSessionBindingListener, Serializable {
- private static final long serialVersionUID = -4780592776386225973L;
- Log log = LogFactory.getLog(getClass());
- private String name;
- private Date dateCreated;
- // 从硬盘加载后
- public void sessionDidActivate(HttpSessionEvent se) {
- HttpSession session = se.getSession();
- log.info(this + "已经成功从硬盘中加载。sessionId: " + session.getId());
- }
- // 即将被钝化到硬盘时
- public void sessionWillPassivate(HttpSessionEvent se) {
- HttpSession session = se.getSession();
- log.info(this + "即将保存到硬盘。sessionId: " + session.getId());
- }
- // 被放进session前
- public void valueBound(HttpSessionBindingEvent event) {
- HttpSession session = event.getSession();
- String name = event.getName();
- log.info(this + "被绑定到session \"" + session.getId() + "\"的" + name + "属性上");
- // 记录放到session中的时间
- this.setDateCreated(new Date());
- }
- // 从session中移除后
- public void valueUnbound(HttpSessionBindingEvent event) {
- HttpSession session = event.getSession();
- String name = event.getName();
- log.info(this + "被从session \"" + session.getId() + "\"的" + name + "属性上移除");
- }
- @Override
- public String toString() {
- return "PersonInfo(" + name + ")";
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Date getDateCreated() {
- return dateCreated;
- }
- public void setDateCreated(Date dateCreated) {
- this.dateCreated = dateCreated;
- }
- }
三、Listener 实例
(一)单态登录:一个账号只能在一台机器上登录。
1、Listener 的代码:
- package servlet.listener.singleton;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpSessionAttributeListener;
- import javax.servlet.http.HttpSessionBindingEvent;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- /**
- *
- * LoginSessionListener.java
- *
- * @title 实现单态登录的监听器
- * @description
- * @author SAM-SHO
- * @Date 2014-12-10
- */
- public class LoginSessionListener implements HttpSessionAttributeListener {
- Log log = LogFactory.getLog(this.getClass());
- Map<String, HttpSession> map = new HashMap<String, HttpSession>();
- public void attributeAdded(HttpSessionBindingEvent event) {
- String name = event.getName();
- // 登录
- if (name.equals("personInfo")) {
- PersonInfo personInfo = (PersonInfo) event.getValue();
- if (map.get(personInfo.getAccount()) != null) {
- // map 中有记录,表明该帐号在其他机器上登录过,将以前的登录失效
- HttpSession session = map.get(personInfo.getAccount());
- PersonInfo oldPersonInfo = (PersonInfo) session.getAttribute("personInfo");//map已经存在的旧的信息
- log.info("帐号" + oldPersonInfo.getAccount() + "在" + oldPersonInfo.getIp() + "已经登录,该登录将被迫下线。");
- session.removeAttribute("personInfo");
- session.setAttribute("msg", "您的帐号已经在其他机器上登录,您被迫下线。");
- }
- // 将session以用户名为索引,放入map中
- map.put(personInfo.getAccount(), event.getSession());
- log.info("帐号" + personInfo.getAccount() + "在" + personInfo.getIp() + "登录。");
- }
- }
- public void attributeRemoved(HttpSessionBindingEvent event) {
- String name = event.getName();
- // 注销
- if (name.equals("personInfo")) {
- // 将该session从map中移除
- PersonInfo personInfo = (PersonInfo) event.getValue();
- map.remove(personInfo.getAccount());
- log.info("帐号" + personInfo.getAccount() + "注销。");
- }
- }
- public void attributeReplaced(HttpSessionBindingEvent event) {
- String name = event.getName();
- // 没有注销的情况下,用另一个帐号登录
- if (name.equals("personInfo")) {
- // 移除旧的的登录信息
- PersonInfo oldPersonInfo = (PersonInfo) event.getValue();
- map.remove(oldPersonInfo.getAccount());
- // 新的登录信息
- PersonInfo personInfo = (PersonInfo) event.getSession().getAttribute("personInfo");
- // 也要检查新登录的帐号是否在别的机器上登录过
- if (map.get(personInfo.getAccount()) != null) {
- // map 中有记录,表明该帐号在其他机器上登录过,将以前的登录失效
- HttpSession session = map.get(personInfo.getAccount());
- session.removeAttribute("personInfo");
- session.setAttribute("msg", "您的帐号已经在其他机器上登录,您被迫下线。");
- }
- map.put("personInfo", event.getSession());
- }
- }
- }
- package servlet.listener.singleton;
- import java.io.Serializable;
- import java.util.Date;
- /**
- *
- * PersonInfo.java
- *
- * @title
- * @description
- * @author SAM-SHO
- * @Date 2014-12-10
- */
- public class PersonInfo implements Serializable {
- private static final long serialVersionUID = 4063725584941336123L;
- // 帐号
- private String account;
- // 登录IP地址
- private String ip;
- // 登录时间
- private Date loginDate;
- public String getAccount() {
- return account;
- }
- public void setAccount(String account) {
- this.account = account;
- }
- public String getIp() {
- return ip;
- }
- public void setIp(String ip) {
- this.ip = ip;
- }
- public Date getLoginDate() {
- return loginDate;
- }
- public void setLoginDate(Date loginDate) {
- this.loginDate = loginDate;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((account == null) ? 0 : account.hashCode());
- result = prime * result + ((ip == null) ? 0 : ip.hashCode());
- return result;
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- PersonInfo other = (PersonInfo) obj;
- if (account == null) {
- if (other.account != null)
- return false;
- } else if (!account.equals(other.account))
- return false;
- if (ip == null) {
- if (other.ip != null)
- return false;
- } else if (!ip.equals(other.ip))
- return false;
- return true;
- }
- }
- <!-- 单态登录监听器 -->
- <listener>
- <listener-class>servlet.listener.singleton.LoginSessionListener</listener-class>
- </listener>
(二)显示在线人数:会需要3个监听器。
1、ContextListener:获取服务启动的时间等。
2、RequestListener:获取客户端的IP、访问地址,访问次数等。
3、SessionListener:需要监听 Session 的创建与属性变化。
4、代码如下:
- package com.helloweenvsfei.listener;
- import java.util.Date;
- import javax.servlet.ServletContextEvent;
- import javax.servlet.ServletContextListener;
- import com.helloweenvsfei.util.ApplicationConstants;
- public class MyContextListener implements ServletContextListener {
- public void contextInitialized(ServletContextEvent event) {
- // 启动时,记录服务器启动时间
- ApplicationConstants.START_DATE = new Date();
- }
- public void contextDestroyed(ServletContextEvent event) {
- // 关闭时,将结果清除。也可以将结果保存到硬盘上。
- ApplicationConstants.START_DATE = null;
- ApplicationConstants.MAX_ONLINE_COUNT_DATE = null;
- }
- }
- package com.helloweenvsfei.listener;
- import javax.servlet.ServletRequestEvent;
- import javax.servlet.ServletRequestListener;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- public class MyRequestListener implements ServletRequestListener {
- public void requestDestroyed(ServletRequestEvent event) {
- }
- public void requestInitialized(ServletRequestEvent event) {
- HttpServletRequest request = (HttpServletRequest) event
- .getServletRequest();
- HttpSession session = request.getSession(true);
- // 记录IP地址
- session.setAttribute("ip", request.getRemoteAddr());
- // 记录访问次数,只记录访问 .html, .do, .jsp, .action 的累计次数
- String uri = request.getRequestURI();
- String[] suffix = { ".html", ".do", ".jsp", ".action" };
- for (int i=0; i<suffix.length; i++) {
- if (uri.endsWith(suffix[i])) {
- break;
- }
- if(i == suffix.length-1)
- return;
- }
- Integer activeTimes = (Integer) session.getAttribute("activeTimes");
- if (activeTimes == null) {
- activeTimes = 0;
- }
- session.setAttribute("activeTimes", activeTimes + 1);
- }
- }
- package com.helloweenvsfei.listener;
- import java.util.Date;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpSessionAttributeListener;
- import javax.servlet.http.HttpSessionBindingEvent;
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
- import com.helloweenvsfei.util.ApplicationConstants;
- public class MySessionListener implements HttpSessionListener,
- HttpSessionAttributeListener {
- public void sessionCreated(HttpSessionEvent sessionEvent) {
- HttpSession session = sessionEvent.getSession();
- // 将 session 放入 map
- ApplicationConstants.SESSION_MAP.put(session.getId(), session);
- // 总访问人数++
- ApplicationConstants.TOTAL_HISTORY_COUNT++;
- // 如果当前在线人数超过历史记录,则更新最大在线人数,并记录时间
- if (ApplicationConstants.SESSION_MAP.size() > ApplicationConstants.MAX_ONLINE_COUNT) {
- ApplicationConstants.MAX_ONLINE_COUNT = ApplicationConstants.SESSION_MAP
- .size();
- ApplicationConstants.MAX_ONLINE_COUNT_DATE = new Date();
- }
- }
- public void sessionDestroyed(HttpSessionEvent sessionEvent) {
- HttpSession session = sessionEvent.getSession();
- // 将session从map中移除
- ApplicationConstants.SESSION_MAP.remove(session.getId());
- }
- public void attributeAdded(HttpSessionBindingEvent event) {
- if (event.getName().equals("personInfo")) {
- // 当前登录用户数++
- ApplicationConstants.CURRENT_LOGIN_COUNT++;
- HttpSession session = event.getSession();
- // 查找该帐号有没有在其他机器上登录
- for (HttpSession sess : ApplicationConstants.SESSION_MAP.values()) {
- // 如果该帐号已经在其他机器上登录,则以前的登录失效
- if (event.getValue().equals(sess.getAttribute("personInfo"))
- && session.getId() != sess.getId()) {
- sess.invalidate();
- }
- }
- }
- }
- public void attributeRemoved(HttpSessionBindingEvent event) {
- // 注销 当前登录用户数--
- if (event.getName().equals("personInfo")) {
- ApplicationConstants.CURRENT_LOGIN_COUNT--;
- }
- }
- public void attributeReplaced(HttpSessionBindingEvent event) {
- // 重新登录
- if (event.getName().equals("personInfo")) {
- HttpSession session = event.getSession();
- for (HttpSession sess : ApplicationConstants.SESSION_MAP.values()) {
- // 如果新帐号在其他机器上登录过,则以前登录失效
- if (event.getValue().equals(sess.getAttribute("personInfo"))
- && session.getId() != sess.getId()) {
- sess.invalidate();
- }
- }
- }
- }
- }
Servlet 监听器Listener详解的更多相关文章
- java Web 监听器Listener详解
简介 JavaWeb中的监听器是Servlet规范中定义的一种特殊类,它用于监听web应用程序中的ServletContext.HttpSession和 ServletRequest这三大域对象的创建 ...
- 监听器(Listener)详解及举例
概念: 监听器就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即被执行. 监听器组成要素: 事件源:事 ...
- servlet监听器Listener(理论+例子)
Listener采用了观察者模式(24种模式之一),Listener是servlet的监听器,他可以监听客户端的请求.服务器端的操作等, 通过监听器,可以自动激发一些操作.比如:监听在线用户数量 当增 ...
- java之Servlet监听器Listener
常用应用场景:单点登录.统计在线人数 一.简介 (一)概述 1.Listener 用于监听 java web程序中的事件,例如创建.修改.删除Session.request.context等,并触发响 ...
- 走进JavaWeb技术世界4:Servlet 工作原理详解
从本篇开始,正式进入Java核心技术内容的学习,首先介绍的就是Java web应用的核心规范servlet 转自:https://www.ibm.com/developerworks/cn/java/ ...
- java web学习总结(二十) -------------------监听器属性详解
一.监听域对象中属性的变更的监听器 域对象中属性的变更的事件监听器就是用来监听 ServletContext, HttpSession, HttpServletRequest 这三个对象中的属性变更信 ...
- 浅谈JavaWEB入门必备知识之Servlet入门案例详解
工欲善其事.必先利其器,想要成为JavaWEB高手那么你不知道servlet是一个什么玩意的话,那就肯定没法玩下去,那么servlet究竟是个什么玩意?下面,仅此个人观点并通过一个小小的案例来为大家详 ...
- Servlet之Filter详解
参考文献:http://www.cnblogs.com/zlbx/p/4888312.html Filter,过滤器,顾名思义,即是对数据等的过滤,预处理过程.为什么要引入过滤器呢?在平常访问网站的时 ...
- Servlet运行过程详解
比如,在浏览器地址栏输入http://ip:port/web01/hello step1,浏览器依据ip,port建立与servlet容器(容器同时也是一个简单的web服务器)之间的连接. step2 ...
随机推荐
- std::set 中内部元素有序条件删除的理解
std::set 中内部元素有序条件删除的理解 1. std::set中的元素是有序排列的 注意:Set集合中的元素通过iterator的引用,但是不能修改. 元素排序: (1)元素中实现比较oper ...
- js-数组面试题
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> & ...
- Strom开发配置手册
一:Storm集群搭建 1.本次开发使用的是storm0.9.3 2.Storm0.9.3集群搭建: 1)storm集群角色包含集群主节点Nimbus:集群从节点Supervisor 2)集群安装:先 ...
- centos下添加git
CentOS中yum里没有Git,需要手动安装. 首先需要安装git的依赖包 yum install curl yum install curl-devel yum install zlib-deve ...
- UWP 2018 新版 NavigationView 尝鲜
本文参考了官方文档以及提供的示例代码(官方代码貌似有点误导,所以写了这一篇,并且文末有GayHub代码地址) 官方文档发布于20180806,说明NavigationView刚发布了没几天,还在开发中 ...
- delphi datetimepicker 修改时间无效问题
今天用delphi写个程序,使用datetimepicker获得想要的时间.蛋疼的问题是无论怎么调整明明看着控件里面的日期变了,但是show出来的datetimepicker.datetime日期都不 ...
- Python运维脚本整理
Python 实现的自动化服务器管理 import sys import os import paramiko ssh = paramiko.SSHClient() ssh.set_missing_h ...
- mac终端常用命令
1.du #查看文件目录大小 示例:查看DataCenter目录下所有文件/文件夹的大小 everSeeker:DataCenter pingping$ -h .9G ./Books 1.2M ./C ...
- 跟着刚哥学习Spring框架--JDBC(六)
Spring的JDBC框架 Spring JDBC提供了一套JDBC抽象框架,用于简化JDBC开发. Spring主要提供JDBC模板方式.关系数据库对象化方式.SimpleJdbc方式.事务管理来简 ...
- ES6之Array.from()方法
Array.from()方法就是将一个类数组对象或者可遍历对象转换成一个真正的数组. 那么什么是类数组对象呢?所谓类数组对象,最基本的要求就是具有length属性的对象. 1.将类数组对象转换为真正数 ...