javaWeb基础之 servlet 监听器(实战)在线人数统计
功能描述:在线人数统计:记录在线人数,IP地址,登录时间,以及sessionId
目的:熟悉 ServletContext 、httpRequest、httpSession 三个域的生命周期及区别
环境:servlet 3.0 \ tomcat7 \ idea2017 \ jdk8 无需任何包
注:必须servlet 3.0 以上才能使用webListener 这个注解,这样不需要在web.xml 中配置
目录结构:
实体类:
package entity; /** * 建立实体类 */ public class User { private String sessionId; private String ipAddr; private String accessDate; public String getSessionId() { return sessionId; } public void setSessionId(String sessionId) { this.sessionId = sessionId; } public String getIpAddr() { return ipAddr; } public void setIpAddr(String ipAddr) { this.ipAddr = ipAddr; } public String getAccessDate() { return accessDate; } public void setAccessDate(String accessDate) { this.accessDate = accessDate; } }
myRequestListener类:
package listener; import entity.User; import util.sessionUtil; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpServletRequest; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; /** servet 3.0以上 只需要增加该注解即可作为监听器 */ @WebListener public class myRequestListener implements ServletRequestListener { private List<User> listUser =null; @Override public void requestDestroyed(ServletRequestEvent servletRequestEvent) { } @Override public void requestInitialized(ServletRequestEvent servletRequestEvent) { /** * 逻辑理解: * 只能通过HttpServletRequest 获取当前请求中的session,因为HttpServletRequest 继承自 servletRequestEvent * 所以强制类型转化后可获取session 然后判断该sessionId 是否存在于已有的list 中,不存在则新增一个 */ HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequestEvent.getServletRequest(); listUser = (List<User>)httpServletRequest.getSession().getServletContext().getAttribute("UserList"); if(listUser==null){ listUser = new ArrayList<>(); } //判断是否该sessionId是否存在 String sessionId = httpServletRequest.getSession().getId(); if(sessionUtil.getSessionUser(listUser,sessionId)==null) { User user = new User(); user.setSessionId(httpServletRequest.getSession().getId());//id user.setIpAddr(httpServletRequest.getRemoteAddr());//ip user.setAccessDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); listUser.add(user); } //以上动作完成后,则将该集合放入 ServletContext 域中提供给其他地方使用。 httpServletRequest.getSession().getServletContext().setAttribute("UserList",listUser); } }
mySessionListener类:
package listener; import entity.User; import util.sessionUtil; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; import java.util.List; /** servet 3.0以上 只需要增加该注解即可作为监听器 */ @WebListener public class mySessionListener implements HttpSessionListener { private int userNumber =0; @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { /** userNumber可计算当前在线人数 在httpSession域 进行增删一旦session过期则会自动调用sessionDestroyed 进行-1 */ userNumber++; httpSessionEvent.getSession().getServletContext().setAttribute("userNumber",userNumber); } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { userNumber--; httpSessionEvent.getSession().getServletContext().setAttribute("userNumber",userNumber); /** 获取servletContext 域中 UserList 如果存在这个sessionId 就将这个对象从集合中删除。 */ List<User> listUser =(List<User> )httpSessionEvent.getSession().getServletContext().getAttribute("UserList"); String sessionId=httpSessionEvent.getSession().getId(); User user = sessionUtil.getSessionUser(listUser,sessionId); if(user != null) { listUser.remove(user); } } }
sessionUtil 类:
package util; import entity.User; import java.util.List; public class sessionUtil { /** * 判断是否存在该session * @param listUser userList * @param sessionId 当前sessionID * @return */ public static User getSessionUser(List<User> listUser, String sessionId) { for (int i = 0; i < listUser.size(); i++) { User user = listUser.get(i); if (user.getSessionId() .equals( sessionId)) { return user; } } return null; } }
index.jsp:
<%@ page import="entity.User" %> <%@ page import="java.util.List" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> 当前在线人数<p>${userNumber}</p></br> <% List<User> listUser = (List<User>) request.getSession().getServletContext().getAttribute("UserList"); if(listUser.size()>0){ for (int i = 0; i < listUser.size(); i++) { User user = (User)listUser.get(i); %> <p> 详情: <%=user.getSessionId()%>---<%=user.getIpAddr()%>---<%=user.getAccessDate()%></p> <%}}%> </body> </html>
效果如下: