怎么能够知道当前有多少用户连接到了TOMCAT呢?谢谢!

解决方案 »

  1.   

    用HttpSessionListener监听session的创建,数量保存在application中
      

  2.   

    package li;/**
     * <p>Title: </p>
     *
     * <p>Description: </p>
     *
     * <p>Copyright: Copyright (c) 2006</p>
     *
     * <p>Company: </p>
     *
     * @author not attributable
     * @version 1.0
     */
    import javax.servlet.http.*;
    import javax.servlet.*;
    import java.util.*;public class onLineUser implements HttpSessionBindingListener {
         public onLineUser(){
       }   private Vector users=new Vector();
       public int getCount(){
           users.trimToSize();
           return users.capacity();
       }
       public boolean existUser(String userName){
           users.trimToSize();
           boolean existUser=false;
           for (int i=0;i<users.capacity();i++ )
           {
               if (userName.equals((String)users.get(i)))
               {
                   existUser=true;
                   break;
               }
           }
           return existUser;
       }   public boolean deleteUser(String userName) {
           users.trimToSize();
           if(existUser(userName)){
               int currUserIndex=-1;
               for(int i=0;i<users.capacity();i++){
                   if(userName.equals((String)users.get(i))){
                       currUserIndex=i;
                       break;
                   }
               }
               if (currUserIndex!=-1){
                   users.remove(currUserIndex);
                   users.trimToSize();
                   return true;
               }
           }
           return false;
       }   public Vector getOnLineUser()
       {
           return users;
       }
         public void valueBound(HttpSessionBindingEvent e) {
           users.trimToSize();
           if(!existUser(e.getName())){
               users.add(e.getName());
               System.out.print(e.getName()+"\t   登入到系统\t"+(new Date()));
               System.out.println("      在线用户数为:"+getCount());
           }else
               System.out.println(e.getName()+"已经存在");
         }     public void valueUnbound(HttpSessionBindingEvent e) {
           users.trimToSize();
           String userName=e.getName();
           deleteUser(userName);
           System.out.print(userName+"\t   退出系统\t"+(new Date()));
           System.out.println("      在线用户数为:"+getCount());
         }
    }
      

  3.   

    既然基于数据库,最简单的方式就是在数据库中作一个标记。实际上,随着功能的铺开,可能会产生很多个类似统计,比如在线访客数量、在线VIP数量、在线网管数量、历史最高在线访客数量、历史最高在线VIP数量……用数据库无疑更容易扩展。
      

  4.   

    用数据库中用户状态比读取session更容易控制
    最好是方法是,在读取数据库中用户状态时,加上读取session,这样可以排除非法关机(或退出系统)数据库不能及时更新