@WebListener
public class MyHttpSessionListener implements HttpSessionListener {......}
web.xml方式
com.invicme.apps.shiro.listener.XXXListener
说明:
在线人数统计网上一般的实现方式是实现HttpSessionListener,监控session的创建(create)和销毁(destroy),但在实际的使用过程中,发现destroy并不及时并且在session.invalidate()的时候还会调用一次sessionCreated()。
package com.invicme.apps.shiro.listener;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
public class OnlineUserBindingListener implements HttpSessionBindingListener {
String username;
public OnlineUserBindingListener(String username){
this.username = username;
}
public void valueBound(HttpSessionBindingEvent event) {
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
// 把用户名放入在线列表
List onlineUserList = (List) application.getAttribute("onlineUserList");
// 第一次使用前,需要初始化
if (onlineUserList == null) {
onlineUserList = new ArrayList();
application.setAttribute("onlineUserList", onlineUserList);
}
onlineUserList.add(this.username);
}
public void valueUnbound(HttpSessionBindingEvent event) {
HttpSession session = event.getSession();
ServletContext application = session.getServletContext();
// 从在线列表中删除用户名
List onlineUserList = (List) application.getAttribute("onlineUserList");
onlineUserList.remove(this.username);
System.out.println(this.username + "退出。");
}
} 在LoginController中,每次登录成功之后可以执行一次,session.setAttribute("loginUserLS", new OnlineUserBindingListener(username))。
3、Listener示例
3.1 Servlet Context Events
package com.invicme.apps.shiro.listener.context;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import org.apache.log4j.Logger;
/**
* @author lucl
* Interface for receiving notification events about ServletContext lifecycle changes.
*/
@WebListener
public class MyServletContextListener implements ServletContextListener {
private static final Logger logger = Logger.getLogger(MyServletContextListener.class);
public MyServletContextListener() {
logger.info("MyServletContextListener() was invoke...");
}
// Web容器启动的时候执行改方法
public void contextInitialized(ServletContextEvent sce) {
logger.info("contextInitialized() was invoke.");
}
// Web容器reload应用时,首先destroy,然后再initialize
public void contextDestroyed(ServletContextEvent sce) {
logger.info("contextDestroyed() was invoke.");
}
}
package com.invicme.apps.shiro.listener.context;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.annotation.WebListener;
import org.apache.log4j.Logger;
/**
* @author lucl
* Interface for receiving notification events about ServletContext attribute changes.
*/
@WebListener
public class MyServletContextAttributeListener implements ServletContextAttributeListener {
private static final Logger logger = Logger.getLogger(MyServletContextAttributeListener.class);
/**
* Default constructor.
*/
public MyServletContextAttributeListener() {
logger.info("MyServletContextAttributeListener() was invoke...");
}
// context.setAttribute()
public void attributeAdded(ServletContextAttributeEvent event) {
logger.info("attributeAdded() was invoke...");
}
// context.setAttribute(已设置过的属性)
public void attributeReplaced(ServletContextAttributeEvent event) {
logger.info("attributeReplaced() was invoke...");
}
// context.removeAttribute()
public void attributeRemoved(ServletContextAttributeEvent event) {
logger.info("attributeRemoved() was invoke...");
}
}
3.2 Http Session Events
package com.invicme.apps.shiro.listener.session;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.log4j.Logger;
/**
* @author lucl
* Interface for receiving notification events about HttpSession lifecycle changes.
*/
@WebListener
public class MyHttpSessionListener implements HttpSessionListener {
private static final Logger logger = Logger.getLogger(MyHttpSessionListener.class);
/**
* Default constructor.
*/
public MyHttpSessionListener() {
logger.info("MyHttpSessionListener() was invoke...");
}
// 会话建立(如:request.getSession())
public void sessionCreated(HttpSessionEvent se) {
logger.info("sessionCreated() was invoke...");
}
public void sessionDestroyed(HttpSessionEvent se) {
logger.info("sessionCreated() was invoke...");
}
}
package com.invicme.apps.shiro.listener.session;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import org.apache.log4j.Logger;
/**
* @author lucl
* Interface for receiving notification events about HttpSession attribute changes.
*/
@WebListener
public class MyHttpSessionAttributeListener implements HttpSessionAttributeListener {
private static final Logger logger = Logger.getLogger(MyHttpSessionAttributeListener.class);
/**
* Default constructor.
*/
public MyHttpSessionAttributeListener() {
logger.info("MyHttpSessionAttributeListener() was invoke...");
}
// session.setAttribute()
public void attributeAdded(HttpSessionBindingEvent event) {
logger.info("attributeAdded() was invoke...");
}
// session.setAttribute(已经设置过的属性)
public void attributeReplaced(HttpSessionBindingEvent event) {
logger.info("attributeReplaced() was invoke...");
}
// session.removeAttribute()
public void attributeRemoved(HttpSessionBindingEvent event) {
logger.info("attributeRemoved() was invoke...");
}
}
package com.invicme.apps.shiro.listener.session;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import org.apache.log4j.Logger;
/**
* @author lucl
* Causes an object to be notified when it is bound to or unbound from a session.
*/
@WebListener
public class MyHttpSessionBindingListener implements HttpSessionBindingListener {
private static final Logger logger = Logger.getLogger(MyHttpSessionBindingListener.class);
/**
* Default constructor.
*/
public MyHttpSessionBindingListener() {
logger.info("MyHttpSessionBindingListener() was invoke...");
}
public void valueUnbound(HttpSessionBindingEvent event) {
logger.info("valueUnbound() was invoke...");
}
public void valueBound(HttpSessionBindingEvent event) {
logger.info("valueBound() was invoke...");
}
}
3.3 Servlet Request Events
package com.invicme.apps.shiro.listener.request;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;
import org.apache.log4j.Logger;
/**
* @author lucl
* Interface for receiving notification events about requests coming into and going out of scope of a web application.
*/
@WebListener
public class MyServletRequestListener implements ServletRequestListener {
private static final Logger logger = Logger.getLogger(MyServletRequestListener.class);
/**
* Default constructor.
*/
public MyServletRequestListener() {
logger.info("MyServletRequestListener() was invoke...");
}
// 每次Http请求都是一个request,方法调用一次
public void requestInitialized(ServletRequestEvent sre) {
logger.info("requestInitialized() was invoke...");
}
// 一次request请求执行完毕
public void requestDestroyed(ServletRequestEvent sre) {
logger.info("requestDestroyed() was invoke...");
}
}
package com.invicme.apps.shiro.listener.request;
import javax.servlet.ServletRequestAttributeEvent;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.annotation.WebListener;
import org.apache.log4j.Logger;
/**
* @author lucl
* Interface for receiving notification events about ServletRequest attribute changes.
*/
@WebListener
public class MyServletRequestAttributeListener implements ServletRequestAttributeListener {
private static final Logger logger = Logger.getLogger(MyServletRequestAttributeListener.class);
/**
* Default constructor.
*/
public MyServletRequestAttributeListener() {
logger.info("MyServletRequestAttributeListener() was invoke...");
}
public void attributeAdded(ServletRequestAttributeEvent srae) {
logger.info("attributeAdded() was invoke...");
}
public void attributeReplaced(ServletRequestAttributeEvent srae) {
logger.info("attributeReplaced() was invoke...");
}
public void attributeRemoved(ServletRequestAttributeEvent srae) {
logger.info("attributeRemoved() was invoke...");
}
}