使用监听器统计当前在线人数
@WebListener
public class MyListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent
) {
ServletContext sc
= httpSessionEvent
.getSession().getServletContext();
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
) {
ServletContext sc
= httpSessionEvent
.getSession().getServletContext();
int count
= (int) sc
.getAttribute("count");
sc
.setAttribute("count",count
-1);
}
}