判断一下session有没有直不就行了?

解决方案 »

  1.   

    实现HttpSessionBindingListener接口 
    在valueBound(HttpSessionBindingEvent event) 创建session
    valueUnbound(HttpSessionBindingEvent event) session销毁  方法里做你的操作如:
    public class a implements HttpSessionBindingListener{
     private int count = 0;.....valueBound(HttpSessionBindingEvent event){count++;}valueUnbound(HttpSessionBindingEvent event){count--;}
    }
       
    count 是你再线人数
      

  2.   

    谢谢teafang你的回复,你的count是可以统计出在线人数。但是我现在必须按我上面的那样做,我还要做其它模块用呢。比如在线人员可以互发短信息。我的那个不但要统计有多少人在线,还要知道在线人的ID号。实现互发短信。
      

  3.   

    把你其他的操作也放到
    valueBound(HttpSessionBindingEvent event)和valueUnbound(HttpSessionBindingEvent event)里不就行了
    valueUnbound(HttpSessionBindingEvent event){
      取得 session内信息;  
    }给你我的程序
    import java.io.*;
    import java.util.*;
    import javax.servlet.http.*;
    import 我的一个properties文件处理的类;//监听登录的整个过程
    public class SessionListener implements HttpSessionBindingListener
     {public String privateInfo=""; //生成监听器的初始化参数字符串
      private String logString=""; //日志记录字符串
      private int count=0; //登录人数计数器
      private String logFile;
      public String whoLog = "";
      
      public SessionListener(String info,String who)
        {this.privateInfo = info;
         whoLog = who;
         /*
         try 
           {whoLog = new String(who.getBytes("ISO-8859-1"),"ISO8859_1");}
         catch(Exception e)
           {}
           */
         initListener();  
        }
      
      private void initListener()
        {PropertyFile pFile = new PropertyFile();//properties文件处理类的对象
         pFile.setPropFile("c:/webinfo/web.properties");//从properties文件里读定制的log文件的存放路径
         logFile = pFile.getProp("logfilepath");
        }  public int getCount()
        {return count;}  public String getLogFilePath()
        {return logFile;}
      
      public void valueBound(HttpSessionBindingEvent event)
        {count++;
         if (privateInfo.equals("count"))
           {return;}
         try
           {Calendar calendar=new GregorianCalendar();
            System.out.println("LOGIN:"+privateInfo+" TIME:"+calendar.getTime()+" USER:"+whoLog);
            logString="\nLOGIN:"+privateInfo+" TIME:"+calendar.getTime()+" USER:"+whoLog+"\n";
            for(int i=1;i<1000;i++)
              {File file=new File(logFile+i);
               if(!(file.exists()))
               file.createNewFile(); //如果文件不存在,创建此文件
               if(file.length()>1048576) //如果文件大于1M,重新创建一个文件
                 {continue;}
               FileOutputStream foo=new FileOutputStream(logFile+i,true);//以append方式打开创建文件
               foo.write(logString.getBytes(),0,logString.length()+2); //写入日志字符串
               foo.close();
               break;//退出
              }
           }
         catch(FileNotFoundException e)
           {}
         catch(IOException e)
           {}
        }  public void valueUnbound(HttpSessionBindingEvent event)
        {count--;
         if (privateInfo.equals("count"))
           {return;}
         try
           {Calendar calendar=new GregorianCalendar();
            System.out.println("LOGOUT:"+privateInfo+" TIME:"+calendar.getTime()+" USER:"+whoLog);
            logString="\nLOGOUT:"+privateInfo+" TIME:"+calendar.getTime()+" USER:"+whoLog+"\n";
            for(int i=1;i<1000;i++)
              {File file=new File(logFile+i);
               if(!(file.exists()))
                 {file.createNewFile();} //如果文件不存在,创建此文件
               if(file.length()>1048576) //如果文件大于1M,重新创建一个文件
                 {continue;}
               FileOutputStream foo=new FileOutputStream(logFile+i,true);//以append方式打开创建文件
               foo.write(logString.getBytes(),0,logString.length()+2); //写入日志字符串
               foo.close();
               break;//退出
              }
           }
         catch(FileNotFoundException e)
           {}
         catch(IOException e)
           {}
        }  }