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 ...
随机推荐
- 18:description方法
本小节知识点: [掌握]description基本概念 [掌握]description重写的方法 [了解]description陷阱 1.description基本概念 NSLog(@"%@ ...
- 再论hibernatetemplate
自动生成hibernate配置文件的时候,会在dao层用到getSession()方法来操作数据库记录,但是他还有个方法getHibernateTemplate(),这两个方法究竟有什么区别呢? 1. ...
- AngularJS 单元测试 Karma jasmine
当AngularJS项目越来越大时候,需要进行单元测试,可以先开发功能再进行测试,也可以先进行测试. 一.karma 是一个基于Node.js(先要安装)的JavaScript测试执行过程管理工具( ...
- sed,grep,进阶+source+export+环境变量
三剑客之sed 概括流程:从文件或管道中,可迭代读取. 命令格式: sed(软件) 选项 sed命令 输入文件 增 两个sed命令: a: 追加文本到指定行后 i: 插入到指定行前 sed -i '1 ...
- J - Oil Skimming 二分图的最大匹配
Description Thanks to a certain "green" resources company, there is a new profitable indus ...
- DevExpress的TcxDBLookupComboBox使用方法及问题
使用TcxDBLookupComboBox需要设置以下属性: 1.DataBinding.DataSource:数据感知控件的数据源. 2.DataBinding.DataField:数据感知控件的连 ...
- 用光的微粒说和广义相对论来解释衍射现象 Explanation of Diffraction Phenomenon by Particle Theory of Light and General Relativity
历史上,物理学是用波动说来解释光的衍射现象.但用微粒说结合广义相对论同样可以做到. 把光看作粒子,当经过障碍物边缘时,由于广义相对论效应,边缘附近的空间被弯曲,使得不同路径的平行光发生偏转,落在障碍物 ...
- C#基础之访问修饰符
C#访问修饰符,无时无刻都在使用,这里记录一下,如果写错的地方,欢迎指正. public :公有的,任何代码均可以访问,应用于所有类或成员: internal:内部的,只能在当前程序集中使用,应用于所 ...
- .Net中验证码图片生成
开发网站或平台系统,登录页面是必不可少的功能,但是现在很多人可以使用工具暴力破解网站密码,为了防止这类非法操作,需要在登录页面添加验证,验证码就是最常用的一种验证方式. 我结合了自己的经验和网上的验证 ...
- unity 人工智能AI,装备解锁临时笔记
A*算法的一种改进设想:1.如何让角色到达目标点的过程中更加平滑:获取一串到达目标点的网格串之后,就实时用带形状的物理射线检测能否直接到达下一个目标点的再下一个目标点,如果能到达,那么直接朝该方向运动 ...