我目前有一个spring mvc的项目,用security 登陆,在filter里获取session,取在线人数。
我发现系统在运行过程中,有的时候只有我一个人登陆,会忽然创建好多session,然后在线人数就到了5k以上
请问这个是什么原因造成的。

解决方案 »

  1.   

    要先判断session是否为空,如果为空,才需要设置!
      

  2.   


    HttpSession session = event.getSession();      
                ServletContext context = session.getServletContext();      
               
             
               HashSet sessions = (HashSet) context.getAttribute("sessions");      
               if (sessions == null) {      
                   sessions = new HashSet();   
                   sessions.add(session);
                 context.setAttribute("sessions", sessions);      
              }      
           
                     
                sessions.add(session);    
                
                
                super.sessionCreated(event);
                super.sessionDestroyed(event);       
                
                context.setAttribute("sessions", sessions); 这是filter的代码
      

  3.   

    有高手给解答一下吗?用的是 SessionCounter extends  HttpSessionEventPublisher  
      

  4.   


              HttpServletRequest request = ServletActionContext.getRequest();
      HttpSession session = request.getSession();
      String sID=(String)request.getSession().getAttribute("SESSIONID");
      if(!session.getId().equals(sID)){
    session.setAttribute("SESSIONID", session.getId());
                    ...
      }
      

  5.   

    可是这里面没有session。setattribute,都是context。setattribute,是spring管理的啊
      

  6.   

    创建session监听类:SessionListener.javapackage com.jdlx.common;import java.util.Hashtable;
    import java.util.Iterator;import javax.servlet.http.HttpSession;
    import javax.servlet.http.HttpSessionEvent;
    import javax.servlet.http.HttpSessionListener;
    public class SessionListener implements HttpSessionListener{
     
     //创建集合保存session对象
     static Hashtable sessionList=new Hashtable(); //session创建触发的方法
     public void sessionCreated(HttpSessionEvent event) {
      sessionList.put(event.getSession().getId(), event.getSession());
     } //session过期或者销毁触发的方法
     public void sessionDestroyed(HttpSessionEvent event) {
      sessionList.remove(event.getSession().getId());
     }
     
     //返回全部session对象集合
     static public Iterator getSet(){
      return sessionList.values().iterator();
     } //根据session对象的id返回session对象
     static public HttpSession getSession(String sessionId){
      return (HttpSession)sessionList.get(sessionId);
     }
    }
    action类中调用   int count=0;
       List userList=new ArrayList();
       Iterator iterator = SessionListener.getSet();
       while(iterator.hasNext()){
        HttpSession session=(HttpSession)iterator.next();
        User user=(User)session.getAttribute(MachineConstant.LOGIN_KEY); //得到单个session值   
        if(userList==null){
         
        }else{
         userList.add(user.getLoginname()); 
         count++;
        }
       }
       setAttribute("userList",userList);
       setAttribute("count",count);在web.xml中配置监听 <listener>
        <listener-class>com.jdlx.common.SessionListener</listener-class>
       </listener>
      

  7.   

    谢谢楼上,我这个是用spring的security登录的,所以用的是security的一些方法。目前又出现了一个问题,当单点登录cas注销的时候,仍然要创建一个session,郁闷了。
      

  8.   

    最好还是用监听器吧,创建和销毁都可以知道,用fiter实在不是个好主意