就是在一个登陆用户建立SESSION后,将他的SESSIONID和用户名称放入一个static的HashMap中,就可以的,检查HASHMAP的长度。如果超过就不给新的用户登陆

解决方案 »

  1.   

    设置一个监听器,如果SESSION失效,就从HASHMAP中根据SESSIONID将这个用户名称去除,这个做,也可以限制同一个用户在不同IP下重复登陆的
      

  2.   

    弄个计数的也行啊,计算系统有几个登陆了。
    要是还需要知道都是哪几个登陆了就把登陆人的信息扔到collection里面。实现的方法太多了
      

  3.   

    collection是不是数据库的一张表啊?我现在有了初步的想法:
    建两张表uesnum、userlimit,当用户登录的时候就,就将插入一行,存储这个用户的用户名和密码,
    退出时就删除,这个人的信息;
    用户登陆的时候统计,统计表usernum的用户数,与userlimit比较,usernum<=userlimit,就可以登陆;
      请高手支招!
      

  4.   

    SessionListener里面监视session数量变化
      

  5.   

    给个简单的实现:/*
     * Created on 2005-5-9
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package xxx;import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpSessionAttributeListener;
    import javax.servlet.http.HttpSessionBindingEvent;
    import javax.servlet.http.HttpSessionBindingListener;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;import xxx.view.OnlineUserView;
    /**
     * @author Allen
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class OnLineCounterLisenter extends 
    HttpServlet implements 
    HttpSessionListener,
    HttpSessionAttributeListener,
    HttpSessionBindingListener{
        //用来得到唯一的在线用户列表
        private static HashMap map=null;
        private static void newInstance(){
            if(map == null){
                map = new HashMap();
            }
        }
        
        /**
         * @return Returns the map.
         */
        public static HashMap getMap() {
            return map;
        }
        /**
         * @param map The map to set.
         */
        public static void setMap(HashMap map) {
            OnLineCounterLisenter.map = map;
        }
        /**
         * 
         */
        public OnLineCounterLisenter() {
            super();
            // TODO Auto-generated constructor stub
        }
        /** Title:当一个session被创建时,该方法自动被调用
         * @author Allen
         * @param HttpSession 
         * @see javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http.HttpSessionEvent)
         * @serialData 2005-05-09
         */
        public void sessionCreated(HttpSessionEvent sessionEvent) {
        }    /* (non-Javadoc)
         * @see javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet.http.HttpSessionEvent)
         */
        public void sessionDestroyed(HttpSessionEvent se) {
            // TODO Auto-generated method stub
            if(map.containsKey(se.getSession().getId())){
                map.remove( se.getSession().getId() );
            }
        }    /* (non-Javadoc)
         * @see javax.servlet.GenericServlet#init()
         */
        public void init() throws ServletException {
            // TODO Auto-generated method stub
            newInstance();        
            getServletContext().setAttribute("OnlineCounter",map);
        }    /* title:调用session.setAttribute(IConstants.SESSION_CONTAINER_KEY, sessionContainer)时,添加用户信息到列表中
         * @see javax.servlet.http.HttpSessionAttributeListener#attributeAdded(javax.servlet.http.HttpSessionBindingEvent)
         */
        public void attributeAdded(HttpSessionBindingEvent se) {
            // TODO Auto-generated method stub
           
            if(se.getName().equals("addUser")){
                OnlineUserView userView=(OnlineUserView)se.getValue();
                OnlineUserView userView2=null;
                Map.Entry entry=null;
                //判断用户是否已经在线
                boolean isExist=false;
                //map.entrySet()
                Iterator userList=map.entrySet().iterator();
                while(userList.hasNext()){
                    entry=(Map.Entry)userList.next();
                    userView2=(OnlineUserView)entry.getValue();
                    if(userView.getUserId().equals(userView2.getUserId())){
                        isExist=true;
                        return;
                    }
                }
                if(isExist==false){
                    map.put(se.getSession().getId(),userView);    
                }         
            }     }    /* (non-Javadoc)
         * @see javax.servlet.http.HttpSessionAttributeListener#attributeRemoved(javax.servlet.http.HttpSessionBindingEvent)
         */
        public void attributeRemoved(HttpSessionBindingEvent se) {
            // TODO Auto-generated method stub
            if(map.containsKey(se.getSession().getId())){
                map.remove( se.getSession().getId() );
            }
        }    /* (non-Javadoc)
         * @see javax.servlet.http.HttpSessionAttributeListener#attributeReplaced(javax.servlet.http.HttpSessionBindingEvent)
         */
        public void attributeReplaced(HttpSessionBindingEvent se) {
            // TODO Auto-generated method stub
            /*if(se.getName().equals(IConstants.SESSION_CONTAINER_KEY)){
                
                SessionContainer sc = null;
                sc = (SessionContainer) se.getValue();
                if (map.containsValue(sc)) {
                    Iterator iter = map.entrySet().iterator();
                    while (iter.hasNext()) {
                        Map.Entry entry = (Map.Entry) iter.next();
                        Object key = entry.getKey();
                        Object val = entry.getValue();
                        if (((SessionContainer) val).equals(sc)) {
                            map.remove(key);
                            map.put(key, sc);
                        }
                    }
                    newInstance();
                    getServletContext().setAttribute("OnlineCounter", getMap());
                }        } */
            
            
        }    /* (non-Javadoc)
         * @see javax.servlet.http.HttpSessionBindingListener#valueBound(javax.servlet.http.HttpSessionBindingEvent)
         */
        public void valueBound(HttpSessionBindingEvent arg0) {
            // TODO Auto-generated method stub
            
        }    /* (non-Javadoc)
         * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(javax.servlet.http.HttpSessionBindingEvent)
         */
        public void valueUnbound(HttpSessionBindingEvent se) {
            // TODO Auto-generated method stub
            if(map.containsKey(se.getSession().getId())){
                map.remove( se.getSession().getId() );
            }
        }
    }
    /////*
     * Created on 2005-5-10
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package xxx.view;import org.apache.struts.action.ActionForm;/**
     * @author Allen
     *
     * TODO To change the template for this generated type comment go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    public class OnlineUserView extends ActionForm {
        private String userId="";
        private String userName="";
        private String ip="";
        
        /**
         * @return Returns the ip.
         */
        public String getIp() {
            return ip;
        }
        /**
         * @param ip The ip to set.
         */
        public void setIp(String ip) {
            this.ip = ip;
        }
        /**
         * @return Returns the userId.
         */
        public String getUserId() {
            return userId;
        }
        /**
         * @param userId The userId to set.
         */
        public void setUserId(String userId) {
            this.userId = userId;
        }
        /**
         * @return Returns the userName.
         */
        public String getUserName() {
            return userName;
        }
        /**
         * @param userName The userName to set.
         */
        public void setUserName(String userName) {
            this.userName = userName;
        }
    }在处理登陆的时候
    request.getSession().setAttribute("addUser",onlineUserView);
              
    即将用户信息存入session的时候,会自动执行监听方法
    在web.xml中配置
      <listener>
            <listener-class>web80info.framework.util.OnLineCounterLisenter</listener-class>
      </listener>  <!--Servlet启动时自动生成该实例-->
      <servlet>
    <servlet-name>OnLineCounterLisenter</servlet-name>
        <display-name>OnLineCounterLisenter</display-name>
        <servlet-class>web80info.framework.util.OnLineCounterLisenter</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      

  6.   

    就是用楼上的Listener例子来做,方便。
    不难的,照着例子写就是了。
      

  7.   

    上面所说的session是三层C/S吧,我要做的是两层,也可以用senssion吗?