1.网站全局统计变量类,只定义全局变量
package com.lt.listener;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
/**
* 网站全局变量类
* @author LIUTIE
*
*/
public abstract class ApplicationConstants {
/**
* 用户登录session名称
*/
public static final String LOGIN_SESSION_NAME = "userInfo";
/**
* 索引所有的session
* 用于单一登录
*/
public static Map<String,HttpSession> SESSION_MAP = new HashMap<>();
/**
* 当前在线用户数
*/
public static int CURRENT_LOGIN_COUNT = 0;
/**
* 历史访客总数
*/
public static int TOTAL_HISTORY_COUNT = 0;
/**
* 最高同时在线人数
*/
public static int MAX_ONLINE_COUNT = 0;
/**
* 服务器启动时间
*/
public static Date SERVER_START_DATE = new Date();
/**
* 最高在线人数时间
*/
public static Date MAX_ONLINE_COUNT_DATE = new Date();
}
2.实现servletContext监听,用于记录服务器信息
package com.lt.listener;
import java.util.Date;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* servletContext监听
* 记录服务器信息 启动关闭时间等
* @author LIUTIE
*
*/
public class MyContextListener implements ServletContextListener {
/**
* 服务器启动时被调用
*/
@Override
public void contextDestroyed(ServletContextEvent arg0) {
//记录启动时间
ApplicationConstants.SERVER_START_DATE = new Date();
}
/**
* 服务器关闭时被调用
*/
@Override
public void contextInitialized(ServletContextEvent arg0) {
//保存数据到硬盘
// TODO Auto-generated method stub
}
}
3.实现 HttpSessionListener, HttpSessionAttributeListener监听,用于记录登录信息、访问总人数、在线人数,实现单一登录等
package com.lt.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;
/**
* session监听
* 记录登录信息 访问总人数 在线人数等
* 实现单一登录
* @author LIUTIE
*
*/
public class MySessionListener implements HttpSessionListener, HttpSessionAttributeListener {
/**
* session创建时被调用
*/
@Override
public void sessionCreated(HttpSessionEvent sessionEvent) {
// 获取创建的session
HttpSession session = sessionEvent.getSession();
// 添加到map
ApplicationConstants.SESSION_MAP.put(session.getId(), session);
// 访问总人数++
ApplicationConstants.TOTAL_HISTORY_COUNT++;
// 如果map总数大于最高同时在线人数则更新最高在线人数及时间
if (ApplicationConstants.MAX_ONLINE_COUNT < ApplicationConstants.SESSION_MAP.size()) {
ApplicationConstants.MAX_ONLINE_COUNT = ApplicationConstants.SESSION_MAP.size();
ApplicationConstants.MAX_ONLINE_COUNT_DATE = new Date();
}
}
/**
* session销毁时被调用
*/
@Override
public void sessionDestroyed(HttpSessionEvent sessionEvent) {
// 获取即将被销毁的session
HttpSession session = sessionEvent.getSession();
// 在map中根据key移除
ApplicationConstants.SESSION_MAP.remove(session.getId());
}
/**
* 添加session属性时被调用
*/
@Override
public void attributeAdded(HttpSessionBindingEvent event) {
// 判断是否添加的用户登录信息session
if (event.getName().equals(ApplicationConstants.LOGIN_SESSION_NAME)) {
// 当前登录用户数++
ApplicationConstants.CURRENT_LOGIN_COUNT++;
// 是否在其他机器登录处理
isLoginInOtherPlace(event);
}
}
/**
* 移除session属性时被调用
*/
@Override
public void attributeRemoved(HttpSessionBindingEvent event) {
// 判断是否移除的用户登录信息session
if (event.getName().equals(ApplicationConstants.LOGIN_SESSION_NAME)) {
// 当前登录用户数--
ApplicationConstants.CURRENT_LOGIN_COUNT--;
// 是否在其他机器登录处理
isLoginInOtherPlace(event);
}
}
/**
* 修改session属性时被调用
*/
@Override
public void attributeReplaced(HttpSessionBindingEvent event) {
// 判断是否修改的用户登录信息session
if (event.getName().equals(ApplicationConstants.LOGIN_SESSION_NAME)) {
// 是否在其他机器登录处理
isLoginInOtherPlace(event);
}
}
/**
* 是否在其他机器登录处理
*
* @param event
*/
private void isLoginInOtherPlace(HttpSessionBindingEvent event) {
// 获取添加的session
HttpSession session = event.getSession();
// 遍历查找此用户是否登录
for (HttpSession s : ApplicationConstants.SESSION_MAP.values()) {
// 如果已经在其他机器登录则使其失效
if (event.getValue().equals(s.getAttribute(ApplicationConstants.LOGIN_SESSION_NAME))
&& session.getId() != s.getId()) {
// 使session失效
session.invalidate();
break;
}
}
}
}
4.实现 request监听,用于记录客户信息 ip、url等
package com.lt.listener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.http.HttpServletRequest;
/**
* request监听 用于记录客户信息 ip、url等
*
* @author LIUTIE
*
*/
public class MyRequestListener implements ServletRequestListener {
/**
* request销毁时调用
*/
@Override
public void requestDestroyed(ServletRequestEvent event) {
// TODO Auto-generated method stub
}
/**
* request创建时调用
*/
@Override
public void requestInitialized(ServletRequestEvent event) {
HttpServletRequest request = (HttpServletRequest) event;
// 客户端ip
String ip = request.getRemoteAddr();
// 访问的URL地址
String url = request.getRequestURI();
// 只做简单后台打印
System.out.println("The client ip is " + ip);
System.out.println("The address url is " + url);
}
}
5.在web.xml中配置队一行的listener
<listener>
<listener-class>
com.lt.listener.MyContextListener
</listener-class>
</listener>
<listener>
<listener-class>
com.lt.listener.MySessionListener
</listener-class>
</listener>
<listener>
<listener-class>
com.lt.listener.MyRequestListener
</listener-class>
</listener>
Listener种类:
1.监听对象的创建与销毁的Listener:
HttpSessionListener: sessionCreated(HttpSessionEvent sessionEvent)、sessionDestroyed(HttpSessionEvent sessionEvent)
ServletRequestListener: requestInitialized(ServletRequestEvent event)、requestDestroyed(ServletRequestEvent event)
ServletContextListener: contextInitialized(ServletContextEvent event)、contextDestroyed(ServletContextEvent event)
2.监听对象的属性变化的Listener:
HttpSessionAttributeListener:(添加、更新、移除session时触发)
attributeAdded(HttpSessionBindingEvent event)、attributeReplaced(HttpSessionBindingEvent event)、attributeRemoved(HttpSessionBindingEvent event)
ServletContextAttributeListener:(添加、更新、移除context时触发)
attributeAdded(ServletContextAttributeEvent event)、attributeReplaced(ServletContextAttributeEvent event)、attributeRemoved(ServletContextAttributeEvent event)
ServletRequestAttributeListener:(添加、更新、移除request时触发)
attributeAdded(ServletRequestAttributeEvent event)、attributeReplaced(ServletRequestAttributeEvent event)、attributeRemoved(ServletRequestAttributeEvent event)
3.监听Session内的对象
HttpSessionBindingListener:(对象放入session、对象从session移除时触发)
valueBound(HttpSessionBindingEvent event)、valueUnbound(HttpSessionBindingEvent event)
HttpSessionActivationListener:(session中的对象被钝化、对象被重新加载时触发ps:将session中的内容保存到硬盘的过程叫做钝化,钝化需实现Serializable序列化接口)
sessionWillPassivate(HttpSessionEvent event)、sessionDidActivate(HttpSessionEvent event)