监听器
统计在线人数
@WebListener
public class CountOnlineListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
Integer countOnline = (Integer) se.getSession().getServletContext().getAttribute("CountOnline");
if (countOnline == null) {
se.getSession().getServletContext().setAttribute("CountOnline", 1);
} else {
se.getSession().getServletContext().setAttribute("CountOnline", countOnline + 1);
}
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
Integer countOnline = (Integer) se.getSession().getServletContext().getAttribute("CountOnline");
System.out.println(countOnline);
se.getSession().getServletContext().setAttribute("CountOnline", countOnline - 1);
countOnline = (Integer) se.getSession().getServletContext().getAttribute("CountOnline");
System.out.println(countOnline);
}
}
本文来自博客园,作者:神乐g,转载请注明原文链接:https://www.cnblogs.com/shenleg/p/14263825.html