<listener>
<listener-class>
com.patest.listener.onlineListener
</listener-class>
</listener>
package com.patest.listener;
import java.util.ArrayList;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* @since 2016/03/06
* @author wwhhff11
*
*/
public class onlineListener implements HttpSessionListener,
HttpSessionAttributeListener {
public static ArrayList<Object> list = new ArrayList<Object>();
public void sessionCreated(HttpSessionEvent httpEvent) {
System.out.println("新建一个session");
}
public void sessionDestroyed(HttpSessionEvent httpEvent) {
System.out.println("销毁一个session");
if (!list.isEmpty()) {
list.remove(httpEvent.getSession().getAttribute("userId"));
}
}
public void attributeAdded(HttpSessionBindingEvent httpEvent) {
System.out.println("添加session中添加对象时触发此操作");
list.add(httpEvent.getSession().getAttribute("userId"));
}
public void attributeRemoved(HttpSessionBindingEvent httpEvent) {
System.out.println("修改、删除session中添加对象时触发此操作");
}
public void attributeReplaced(HttpSessionBindingEvent httpEvent) {
System.out.println("在session中替换对象时触发此操作");
}
}