session.getAttributeNames()获得该会话的所有对象的名字。

解决方案 »

  1.   

    楼上几位:HttpSession好像是针对一次会话的,也就是只针对某个用户的,而我是想得到所有用户的ID,这样好像不行吧,
    麻烦指条明路!
      

  2.   

    application.setAttribute("userID",userID_arrayList_And_IP);
    ArrayList userID=(ArrayList)application.getAttribute("userID");if(new_login_userID 包含于userID){
       you can do any.
    }else{
       userID.add(new_login_userID+" split_char "+login_IP);
       application.setAttribute("userID",userID);
    }
    试试用application对象来实现可能方便一些
      

  3.   

    package bean.sample ;import java.util.*;
    import javax.servlet.http.*;
    /**
     *用户登陆成功时:
     *session.setAttribute("users",new UserSession(username));
     */public class UserSession implements HttpSessionBindingListener
    { private String userName; public UserSession(String username)
     {
       this.userName=username;
     } public void valueBound(HttpSessionBindingEvent e)
     {
       Properties sys=System.getProperties();   int online;
       try {online=((Integer)sys.get("online")).intValue();}
       catch(Exception ex) {online=0;}
       online++;
       sys.put("online",new Integer(online));   ArrayList users=(ArrayList)sys.get("users");
       if(users==null)
         users = new ArrayList();
       users.add(this.userName);
       sys.put("users",users);   System.setProperties(sys);
     } public void valueUnbound(HttpSessionBindingEvent e)
     {
       Properties sys=System.getProperties();   int online;
       try {online=((Integer)sys.get("online")).intValue();}
       catch(Exception ex) {online=0;}
       online--;
       if(online<0) online=0;
       sys.put("online",new Integer(online));   ArrayList users=(ArrayList)sys.get("users");
       users.remove(this.userName);
       sys.put("users",users);   System.setProperties(sys);
     }}
      

  4.   

    楼上,你这代码应该有可行性可是我试了一下,没有发现在什么时候会调用valueUnbound()呀
    也就是用户退出没有执行valueUnbound,为什么会这样??
      

  5.   

    解决思路可以开阔一些:
    (1)直接放在一个Static Vector中,每次都去找一下
    (2)直接放在数据库中,每次都去搜一下
    (3)在servlet老的规范里有取得全部Session的方法,可惜已经停用了,因为安全理由
      

  6.   

    谢谢两位精辟详尽的讲解目前我已经采用session的方法,原因:
    1、对数据库过多的操作将导致数据库负担过重,10w用户的点击不是一般的量,并且不是一个用户只进行一次数据库操作,包括用户的进入,退出等都需要查询、插入、更新相应字段
    2、用session本身已经的机制,可以很好的控制用户退出,也就是session-timeout,并且session本身的操作很方便
    3、对原来的代码不需要做太多的改动,只要在用户成功登录的地方session.setAttribute(),再写一个UserSession类就可以了,层次很清楚采用方法:
    HttpSessionBindingLister未解决问题:
    1、用户非正常退出,比如拔网线、电源等,没有一个好的标准检测
    2、如果用户在session-timeout时间之内进出,无法判断,目前只做到将冲突的用户IP写入日志,但实际可能并不是冲突。