一个监听器

import java.util.Vector;

import javax.servlet.ServletContext;
$a,w#n$`4K ^0import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class ConfirmationListener implements ServletContextListener,
  HttpSessionAttributeListener {

 Vector v;
 ServletContext sc;
 public void contextDestroyed(ServletContextEvent sce) {

  v=null;
 }
 public void contextInitialized(ServletContextEvent sce) {
  v = new Vector();
  sc=sce.getServletContext();
  }

 public void attributeAdded(HttpSessionBindingEvent se) {

  HttpSession session=se.getSession();
 if (se.getName().equals("id")) {
 v.add(se.getValue());
 System.out.println("add:" + se.getValue());
  } 
 session.setAttribute("vector", v);//存放所有在线用户ID
 sc.setAttribute("population",v.size());//存放在线人数
 }

 public void attributeRemoved(HttpSessionBindingEvent se) {
 HttpSession session=se.getSession();
  if (se.getName().equals("id")) {
   v.removeElement(se.getValue());  
   System.out.println("remove:" + se.getValue());
 }
  session.setAttribute("vector", v);
 sc.setAttribute("population",v.size());
 }

 public void attributeReplaced(HttpSessionBindingEvent se) {

 }

}
这个监听器实现了用户登入后,不能在用登入的帐号登入,并且计算在线人数!

 

posted @ 2008-05-17 17:38  玉米疯收  阅读(291)  评论(1编辑  收藏  举报