자바 감청 세 션 을 통 해 온라인 사용자 통계 실현

1661 단어
package com.boventech.learning.listener;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import com.boventech.learning.constants.Constants;
import com.boventech.learning.entity.User;


public class SessionListener implements ServletContextAttributeListener, HttpSessionAttributeListener {

	private static Set<User> onLineUsers = new HashSet<User>();//        

	public static Set<User> getOnLineUsers() {
		return onLineUsers;
	}

        //session        ,              
	public void attributeAdded(HttpSessionBindingEvent arg0) {
		if(arg0.getName().equals(Constants.USER_KEY)){
			onLineUsers.add((User)arg0.getValue());
		}
	}
        //  session   
	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		if(arg0.getName().equals(Constants.USER_KEY)){
			onLineUsers.remove((User)arg0.getValue());
		}
	}
        //session      
	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		if(arg0.getName().equals(Constants.USER_KEY)){
			onLineUsers.add((User)arg0.getValue());
		}
		
	}

	public void attributeAdded(ServletContextAttributeEvent arg0) {	

	}
	public void attributeRemoved(ServletContextAttributeEvent arg0) {

	}
	public void attributeReplaced(ServletContextAttributeEvent arg0) {

	}
}
<listener>
	  <listener-class>com.boventech.learning.listener.SessionListener</listener-class>
	</listener>

좋은 웹페이지 즐겨찾기