码迷,mamicode.com
首页 > 其他好文 > 详细

Listener

时间:2016-08-17 18:04:32      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

通过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();    
    }
    
}

 

Listener

标签:

原文地址:http://www.cnblogs.com/da-peng/p/5780886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!