监听器
实现一个监听器的接口:
1、编写一个监听器
public class OnlineCountListener implements HttpSessionListener {
//创建session的监听
@Override
public void sessionCreated(HttpSessionEvent se) {
ServletContext context = se.getSession().getServletContext();
Integer OnlineCount = (Integer) context.getAttribute("OnlineCount");
if( OnlineCount == null){
OnlineCount = 1;
}else {
int count = OnlineCount.intValue();
OnlineCount = new Integer(count+1);
}
context.setAttribute("OnlineCount",OnlineCount);
}
//销毁session监听
@Override
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext context = se.getSession().getServletContext();
Integer OnlineCount = (Integer) context.getAttribute("OnlineCount");
if( OnlineCount == null){
OnlineCount = 0;
}else {
int count = OnlineCount.intValue();
OnlineCount = new Integer(count-1);
}
context.setAttribute("OnlineCount",OnlineCount);
}
}
//复习销毁session
//getsession().invalidate();
//web.xml配置自动线销毁
2、配置监听器(注册)
<!-- 这注册监听器-->
<listener>
<listener-class>com.shiy.listener.OnlineCountListener</listener-class>
</listener>
标签:count,监听器,Listener,context,Integer,servlet,OnlineCount,se
From: https://www.cnblogs.com/teststack/p/16878777.html