Listener

通过Listner获得当前的用户个数

package listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class OnlineUsers implements ServletContextListener,HttpSessionListener{
    private int count;
    private ServletContext cxt;
    @Override
    public void sessionCreated(HttpSessionEvent arg0) {
        // TODO Auto-generated method stub
        count++;
        cxt.setAttribute("count", new Integer(count));
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent arg0) {
        // TODO Auto-generated method stub
        count--;
        cxt.setAttribute("count", new Integer(count));
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        cxt = null;
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        cxt = arg0.getServletContext();    
    }
    
}

 

posted @ 2016-08-17 17:25  式微胡不归  阅读(109)  评论(0编辑  收藏  举报