使用监听器统计当前在线人数

tech2022-07-11  156

使用监听器统计当前在线人数

@WebListener public class MyListener implements HttpSessionListener { @Override public void sessionCreated(HttpSessionEvent httpSessionEvent) { //获取Application对象 ServletContext sc = httpSessionEvent.getSession().getServletContext(); //获取application对象中当前的在线人数 Object count = sc.getAttribute("count"); //判断 if(count!=null){ int c=(int)count; sc.setAttribute("count",c+1); }else{ sc.setAttribute("count",1); } } @Override public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { //获取Application对象 ServletContext sc = httpSessionEvent.getSession().getServletContext(); //获取application对象中当前的在线人数 int count = (int) sc.getAttribute("count"); sc.setAttribute("count",count-1); } }
最新回复(0)