一个监听器

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) {

 }

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值