我定义了一个监听Servlet的类package com.binarystar.control;import java.util.HashMap;import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;import com.binarystar.model.dataobject.BSObject;public class BSControlListener implements ServletContextListener,
ServletContextAttributeListener, HttpSessionListener,
HttpSessionAttributeListener {
private ServletContext application = null;
private HttpSession session = null;
private BSObject m_bs = null; public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub } public void contextInitialized(ServletContextEvent sce) {
// 初始化一个application对象
this.application = sce.getServletContext();
// 设置一个列表属性,用于保存在线用户名
this.application.setAttribute("BS_ONLINE", new HashMap());
} public void sessionCreated(HttpSessionEvent arg0) {
// TODO Auto-generated method stub } public void sessionDestroyed(HttpSessionEvent se) {
// 取得用户名列表
HashMap online = (HashMap) this.application.getAttribute("BS_ONLINE");
// 取得当前用户名
String username = (String) se.getSession().getAttribute("BS_ONLINE");
System.out.println("sessionDestroyed: " + username);
// 将此用户名从列表中删除
online.remove(username);
// 将删除后的列表重新设置到application属性中
this.application.setAttribute("BS_ONLINE", online); } public void attributeAdded(HttpSessionBindingEvent se) {
// 取得用户名列表
HashMap online = (HashMap) this.application.getAttribute("BS_ONLINE");
// 将当前用户名添加到列表中
String userName = (String) se.getSession().getAttribute("BS_ONLINE");
// 将此用户名从列表中删除
if (userName != null && !userName.equals("")) {
System.out.println("attributeAdded(HttpSessionBindingEvent se): "
+ userName);
online.put(userName, userName);
}
// 将添加后的列表重新设置到application属性中
this.application.setAttribute("BS_ONLINE", online);
} public void attributeRemoved(HttpSessionBindingEvent se) {
System.out.println("attributeReplaced(HttpSessionBindingEvent se): "
+ se.getName()+"--->"+se.getValue());
} public void attributeReplaced(HttpSessionBindingEvent se) {
// 取得用户名列表
HashMap online = (HashMap) this.application.getAttribute("BS_ONLINE");
// 取得当前用户名
String userName = (String) se.getSession().getAttribute("BS_OUTLINE");
// 将此用户名从列表中删除
if (userName != null && !userName.equals("")) {
System.out.println("attributeReplaced(HttpSessionBindingEvent se): "
+ userName);
online.remove(userName);
// 将删除后的列表重新设置到application属性中
this.application.setAttribute("BS_ONLINE", online);
se.getSession().setAttribute("BS_OUTLINE", "");
}
} public void attributeAdded(ServletContextAttributeEvent arg0) {
// TODO Auto-generated method stub } public void attributeRemoved(ServletContextAttributeEvent se) {
System.out.println("attributeReplaced(HttpSessionBindingEvent se): "
+ se.getName()+"--->"+se.getValue());
} public void attributeReplaced(ServletContextAttributeEvent arg0) {
// TODO Auto-generated method stub }}
然后在Servlet里写了一个Session.removeAttribute("BS_ONLINE")。但是没有触发attributeRemoved函数。