最近突然想到一个问题,就是一个web系统,不需要进行用户登录注册的,想得到一个浏览者的在线时间,有可能实现么?要是有可能的话,该如何实现阿?

解决方案 »

  1.   

    临时用户,可以,用SessionListener
      

  2.   

    使用sessionlistener监听session的创建和销毁
    session创建的时候根据sessionid记录一个时间,当session销毁的时候根据sessionid再记录一个时间,同一个sessionid的两个时间差就是在线时间
      

  3.   

    public class SessionListener implements HttpSessionListener {  public synchronized void sessionCreated(HttpSessionEvent se) {
           //session创建记录sessionid和时间到Hashtable
      }
      public synchronized void sessionDestroyed(HttpSessionEvent se) {
           //session销毁从Hashtable中根据sessionid取出一个时间和现在时间之差便是此游客在线时间
        }
      }
    }注册sessionlistener到web.xml
      <listener>
        <listener-class>com.link2friend.friend.common.common.SessionListener</listener-class>
      </listener>
      

  4.   

    我在网上找到的,发现有好多函数,不知道是什么意思,也不知道该如何使用阿?package   com.listener;import   javax.servlet.ServletContext;
    import   javax.servlet.ServletContextEvent;
    import   javax.servlet.ServletContextListener;
    import   javax.servlet.http.HttpSessionAttributeListener;
    import   javax.servlet.http.HttpSessionBindingEvent;
    import   javax.servlet.http.HttpSessionEvent;
    import   javax.servlet.http.HttpSessionListener;
    import   javax.servlet.http.HttpSessionActivationListener;
    import   javax.servlet.http.HttpSessionBindingListener;
    import   java.io.PrintWriter;
    import   java.io.FileOutputStream;public   final   class   MySessionListener
        implements   HttpSessionActivationListener   ,HttpSessionBindingListener   ,
            HttpSessionAttributeListener,   HttpSessionListener,ServletContextListener   {
      
        ServletContext   context;
        int   users=0;     
        
        //HttpSessionActivationListener
        public   void   sessionDidActivate(HttpSessionEvent   se)   
        {           
            logout("sessionDidActivate("+se.getSession().getId()+")");
        }
        
      
        public   void   sessionWillPassivate(HttpSessionEvent   se)   
        {
        logout("sessionWillPassivate("+se.getSession().getId()+")");
        }//HttpSessionActivationListener
        
        
        //HttpSessionBindingListener
        public   void   valueBound(HttpSessionBindingEvent   event)   
        {
            logout("valueBound("+event.getSession().getId()+event.getValue()+")");
        }
        public   void   valueUnbound(HttpSessionBindingEvent   event)   
        {
        logout("valueUnbound("+event.getSession().getId()+event.getValue()+")");
        }
        
        //HttpSessionAttributeListener
        public   void   attributeAdded(HttpSessionBindingEvent   event)   {logout("attributeAdded('"   +   event.getSession().getId()   +   "',   '"   +
        event.getName()   +   "',   '"   +   event.getValue()   +   "')");    }    public   void   attributeRemoved(HttpSessionBindingEvent   event)   {logout("attributeRemoved('"   +   event.getSession().getId()   +   "',   '"   +
        event.getName()   +   "',   '"   +   event.getValue()   +   "')");    }
        
        public   void   attributeReplaced(HttpSessionBindingEvent   se)   
        {
        logout("attributeReplaced('"+se.getSession().getId()+",'"+se.getName()+"','"+se.getValue()+"')");
        }//HttpSessionAttributeListener
          //HttpSessionListener
        public   void   sessionCreated(HttpSessionEvent   event)   {users++;
    logout("sessionCreated('"   +   event.getSession().getId()   +   "'),目前有"+users+"个用户");
    context.setAttribute("users",new   Integer(users));
        }    
        public   void   sessionDestroyed(HttpSessionEvent   event)   {users--;
    logout("sessionDestroyed('"   +   event.getSession().getId()   +   "'),目前有"+users+"个用户");
    context.setAttribute("users",new   Integer(users));    }//HttpSessionListener
        
        //ServletContextListener
        public   void   contextDestroyed(ServletContextEvent   sce)   {logout("contextDestroyed()-->ServletContext被销毁");
        this.context   =   null;    }    public   void   contextInitialized(ServletContextEvent   sce)   {this.context   =   sce.getServletContext();
    logout("contextInitialized()-->ServletContext初始化了");    }//ServletContextListener
            private   void   logout(String   message)   { 
        
        PrintWriter   out=null;
        try
        {
        out=new   PrintWriter(new   FileOutputStream("c:\\session.txt",true));
        out.println(new   java.util.Date().toLocaleString()+"::Form   MySessionListener:   "   +   message);
        out.close();
        }
        catch(Exception   e)
        {
        out.close();
        e.printStackTrace();
        }
        }   
    }
      

  5.   

    这个类倒和你所说的差不多阿!import javax.servlet.*;
    import javax.servlet.http.*;public final class CounterListener implements HttpSessionListener {
        private int count = 10;
        private ServletContext context = null;    public synchronized void sessionCreated(HttpSessionEvent se) {
            count++;
            log("sessionCreated('" + se.getSession().getId() + "'LuoTing's Log)"+"  count="+count);
            se.getSession().setAttribute("count",new Integer(count));
        }    public synchronized void sessionDestroyed(HttpSessionEvent se) {
            count--;
            se.getSession().setAttribute("count",new Integer(count));
        }    public int getCount() {
            return this.count;
        }    public void addCount(){
          count++;
        }    private void log(String message) {    if (context != null)
            context.log("SessionListener: " + message);
        else
            System.out.println("SessionListener: " + message);    }
    }
      

  6.   

    public class SessionListener implements HttpSessionListener {  public synchronized void sessionCreated(HttpSessionEvent se) {
        se.getSession().setAttribute("time",System.currentTimeMillis()+"");
      }
      public synchronized void sessionDestroyed(HttpSessionEvent se) {
        long startTime=Long.parseLong(se.getSession().getAttribute("time"));
        long endTime=System.currentTimeMillis();
        long time=(endTime-startTime)/1000;
        System.out.println("此用户在线时间为:"+time+"秒");
      }
    }
      

  7.   

    sessionCreated和sessionDestroyed是自己自动被调用的么?
      

  8.   

    我在jsp页面里该如何调用呢?
      

  9.   

    注册sessionlistener到web.xml
      <listener>
        <listener-class>SessionListener</listener-class>
      </listener>这里注册进去就可以了,自动调用的,是监听器,只要有session创建和销毁就会分别调用sessionCreated和sessionDestroyed方法
      

  10.   

    那我怎么在jsp页面里得到上面的时间数据呢?
      

  11.   

    你是想得到一个人的在线时间?
    上面统计的是每个人单个的在线时间,要在JSP页面上访问到,就把他存到application里就可以得到了
      public synchronized void sessionDestroyed(HttpSessionEvent se) {
        long startTime=Long.parseLong(se.getSession().getAttribute("time"));
        long endTime=System.currentTimeMillis();
        long time=(endTime-startTime)/1000;
        System.out.println("此用户在线时间为:"+time+"秒");
        se.getSession().getServletContext().setAttribute("time",time+"");
      }在JSP页面上可以通过application.getAttribute("time");得到刚刚离开的用户在线时间
      

  12.   

    public class TimeBean{
      private long startTime;
      private long endTime;
      public void setStartTime(long startTime){
        this.startTime=startTime;
      }
      public long getStartTime(){
        return this.startTime;
      }
      public void setEndTime(long endTime){
        this.endTime=endTime;
      }
      public long getEndTime(){
        return this.endTime();
      }
    }public class SessionListener implements HttpSessionListener {
      private static Hashtable time=new Hashtable();  public synchronized void sessionCreated(HttpSessionEvent se) {
        //这里创建一个你自己的BEAN对象
        TimeBean timeBean=new TimeBean();
        timeBean.setStartTime(System.currentTimeMillis());
        se.getSession().setAttribute("time",timeBean);
      }
      public synchronized void sessionDestroyed(HttpSessionEvent se) {
        TimeBean timeBean=new TimeBean();
        timeBean=(TimeBean)se.getSession().getAttribute("time");
        timeBean.setEndTime(System.currentTimeMillis());
        //存到Hashtable里
        time.put(se.getSession().getId(),timeBean);
      }
    }