登录的时候检查session应该就可以了啊,好像都是这么干的

解决方案 »

  1.   

    application对象Attibutes: ArrayList users;
    登陆 
    if(users.Contains(username))
    {
    该用户己登陆!
    }
    else
    {
     user refUser=new user(username))
    }
      

  2.   

    使用HttpSessionBindingListener。
    下面是一个在线人数的统计例子。
    你可以很容易把在线人数改为在线用户。注册事件:
    session.setAttribute("sessionObj",new sessionbean());
    //sessionbean.java
    package beans; 
    import java.util.Properties;
    import javax.servlet.http.*;
    public class sessionbean implements HttpSessionBindingListener 
    {
     public sessionbean() {}
     
     public void valueBound(HttpSessionBindingEvent e)
     {Properties sys=System.getProperties();
      String temp=(String)sys.get("online");  
      int online=1;
      try {online=Integer.parseInt(temp);}
      catch(Exception e1) {return;}
      if(online<0) online=0;
      sys.put("online",(online+1)+"");    
      System.setProperties(sys);
     }
     public void valueUnbound(HttpSessionBindingEvent e)
     {Properties sys=System.getProperties();
      String temp=(String)sys.get("online");
      int online=0;
      try {online=Integer.parseInt(temp);}
      catch(Exception e1) {return;}
      online--;
      if(online<0) online=0;
      sys.put("online",online+"");    
      System.setProperties(sys);
     }
     
    }                 
      

  3.   

    使用xuzhenhua21(舍得)的方法ArrayList users只能添加不能更新,这样用户登陆一次就永远都不能再登陆。所以不使用HttpSessionBindingListener是实现不了的。接分!!!
      

  4.   

    一般来说都是用session检查,good luck
      

  5.   

    登陆信息是保存在session里边的,登陆的时候检查session就可以了
      

  6.   

    javahui(阶级斗争要年年讲,月月讲,天天讲。) 
    为什么不能,可以注销呀,
    用户注销
    user.Remove(String username);
    希望说话负得起责任!!
      

  7.   

    当登陆时候把用户名保存在session里面,在有人登陆时候检查session,如果有这样的session存在就说明是重复登陆,因为用户名是唯一的。
      

  8.   

    to xuzhenhua21(舍得):
    我开始也想用application的attribute存放userList,关闭时remove掉。但是这种情况如何remove掉:用户登陆后离开了,过了一段时间在另一台计算上登录,而他先前的那个session已经过期了。如何让session过期触发userList的一个remove动作?to javahui(阶级斗争要年年讲,月月讲,天天讲。) 
    你的方法我先试试,好使的话,分少不了你的。^^to haode(好的), acefr():
    是想用session中的user来比较。但我怎么从application中获得所有当前激活的session来一个个比较呀?
      

  9.   

    vearbear(老熊) :如何让session过期触发userList的一个remove动作?这个问题问的很好,同时我也回答xuzhenhua21(舍得):
    HttpSessionBindingListener 就是负责触发功能的。
      

  10.   

    TO:xuzhenhua21(舍得)
    也不是说你的方法是错误的。你的方法是对的。但是缺少SESSION失效检查的代码。user.Remove(String username);方法是可以,但是你什么时候调用这个方法?不可能要求用户退出时一定要点exit.jsp吧!
      

  11.   

    真的不错。
       我查看了关于HttpSessionBindingListener相关的一些文档。的确,当实现了这个接口,在session过期或失效时,会发送消息给valueUnbound()方法。
       javahui和xuzhenhua21也不用争论,二者结合起来就是一个完整的方案。
       用javahui的例子,把方法valueBound()和valueUnbound()内容改写成xuzhenhua21的userList的方式(呵呵,Properties功能有限)。bound则在userList增加一个,unbound则在userList中删掉一个。应用程序中提供userList的是否包含新登录用户的验证方法。
       是否就是这样子?
       做为另外一种不需要建立userList的方案,我们能够直接获得当前所有有效的session,从而一个个比较呢?
       准备结账!
      

  12.   

    package com.jingjian.tms.Util;import java.util.*;
    import javax.servlet.http.*;
    /**
     *
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2003</p>
     * <p>Company: </p>
     * @author not attributable
     * @version 1.0
     用户登陆成功时注册事件:
     session.setAttribute("sessionObj",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");
       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);
     }}