HttpSession Listener 와 HttpSession Binding Listener 로 온라인 인원수 통 계 를 실현 합 니 다.

참고:http://www.cnblogs.com/shencheng/archive/2011/01/07/1930227.html


import java.io.PrintStream;
import java.util.Date;
import java.util.Vector;
import javax.servlet.http.*;

import com.maizhi.app.page.impl.logLoginBean;

import java.sql.SQLException;

public class onLineUser implements HttpSessionBindingListener
{
    public onLineUser()
    {
        users = new Vector();
    //    ipaddrs = new Vector();
    }

    public boolean deleteUser(String username)
    {
        users.trimToSize();
        if(existUser(username))
        {
            int i = -1;
            for(int j = 0; j < users.capacity(); j++)
            {
                if(!username.equals((String)users.get(j))) continue;
                i = j;
                break;
            }

            if(i != -1)
            {
                users.remove(i);
                users.trimToSize();

                return true;
            }
        }
        return false;
    }

    public boolean existUser(String username)
    {
        users.trimToSize();
        boolean flag = false;
        for(int i = 0; i < users.capacity(); i++)
        {
            if(!username.equals((String)users.get(i)))
                continue;
            flag = true;
            break;
        }

        return flag;
    }

    public static int getCount()
    {
        users.trimToSize();
        return users.capacity();
    }

    public Vector getOnLineUser()
    {
        return users;
    }

    public void valueBound(HttpSessionBindingEvent httpsessionbindingevent)
    {
        users.trimToSize();
       // ipaddrs.trimToSize();

        HttpSession session = httpsessionbindingevent.getSession();
   //     String session_id = (String)session.getId();
   //     String ip_addr = (String)session.getAttribute("session_ip_addr");

        if(!existUser(httpsessionbindingevent.getName()))
        {
            users.add(httpsessionbindingevent.getName());
      //      ipaddrs.add(ip_addr);
            System.out.println(httpsessionbindingevent.getName() + " \u767B\u5165 " + new Date());
            System.out.println("  \u5728\u7EBF   " + getCount());
        } else
        {
        	//logLoginBean.logOUt(session);
            System.out.println(httpsessionbindingevent.getName() + "\u5DF2\u7ECF\u5B58\u5728 ");
        }
        
       // if (ip_addr != null) System.out.println( " "+ ip_addr);
    }

    public void valueUnbound(HttpSessionBindingEvent httpsessionbindingevent)
    {
        users.trimToSize();

        String s = httpsessionbindingevent.getName();
        HttpSession session = httpsessionbindingevent.getSession();
    //    String session_id = (String)session.getId();
      //  logLoginBean.logOUt(session); 
        deleteUser(s);
        
       System.out.println(s + " \u9000\u51FA  " + new Date());
       System.out.println("  \u5728\u7EBF  " + getCount());
      
    }

    private static Vector users;
    private Vector ipaddrs;
}


좋은 웹페이지 즐겨찾기