Hello everyone, I am a pig arched by cabbage.
Without further ado, go directly to the code.
Implement the HttpSessionListener interface to count the number of online people on the website
/**
* @ClassName: OnlineListener
* @Description:统计网站在线人数
* @author Lily
* @date 2020年4月10日
*/
public class OnlineListener implements HttpSessionListener{
private static Integer online=0;
/*
* 监听session创建
*/
@Override
public void sessionCreated(HttpSessionEvent arg0) {
synchronized (online) {
online++;
}
}
/*
* 监听session销毁
*/
@Override
public void sessionDestroyed(HttpSessionEvent arg0) {
synchronized(online){
if(online>0){
online--;
}
}
}
public static int getOnline(){
return online;
}
}
The front page uses the getOnline method to directly obtain the online value, it is so simple, so easy !!!