实现一个监听器的接口:(有n种)
1、编写一个监听器
实现监听器的接口:
public class OnlineCountListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent se) {
ServletContext context = se.getSession().getServletContext();
Integer onlineCount = (Integer) context.getAttribute("OnlineCount");
if (onlineCount==null){
onlineCount = new Integer(1);
}else {
int count = onlineCount.intValue();
onlineCount = new Integer(count+1);
}
context.setAttribute("onlineCount",onlineCount);
}
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext context = se.getSession().getServletContext();
Integer onlineCount = (Integer) context.getAttribute("OnlineCount");
if (onlineCount==null){
onlineCount = new Integer(0);
}else {
int count = onlineCount.intValue();
onlineCount = new Integer(count+1);
}
context.setAttribute("onlineCount",onlineCount);
}
}
2、we b.xml中注册监听器
<listener>
<listener-class>com.xiao.filter.OnlineCountListener</listener-class>
</listener>
3、看情况是否使用(几乎不用)