java web会话监听并使用spring注入
Java  /  管理员 发布于 3年前   174
在java web应用中,当你想在建立会话或移除会话时,让系统做某些事情,比如说,统计在线用户,每当有用户登录时,或退出时,那么可以用下面这个监听器来监听。
import java.util.ArrayList;import java.util.List;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.http.HttpSessionAttributeListener;import javax.servlet.http.HttpSessionBindingEvent;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;public class OnlineCountListener implements HttpSessionAttributeListener ,HttpSessionListener,ServletContextListener{/** * 定义监听的session属性名. */public final static String LISTENER_NAME = "user_name";/** * 定义存储客户登录session的集合. */private static List sessions = new ArrayList();private static int userSum=0;/** * session加入某值时此方法会被调用. * * @param HttpSessionBindingEvent * session事件 */public void attributeAdded(HttpSessionBindingEvent sbe) {if (LISTENER_NAME.equals(sbe.getName())) {sessions.add(sbe.getValue());}}/** * session失效时的监听方法. * * @param HttpSessionBindingEvent * session事件 */public void attributeRemoved(HttpSessionBindingEvent sbe) {if (LISTENER_NAME.equals(sbe.getName())) {sessions.remove(sbe.getValue());}}/** * session覆盖时的监听方法. * * @param HttpSessionBindingEvent * session事件 */public void attributeReplaced(HttpSessionBindingEvent sbe) {if (LISTENER_NAME.equals(sbe.getName())) {sessions.remove(sbe.getValue());sessions.add(sbe.getValue());}}public void sessionCreated(HttpSessionEvent arg) {userSum++;}public void sessionDestroyed(HttpSessionEvent arg) {userSum--;}/** * 返回客户登录session的集合. * * @return */public static List getSessions() {return sessions;}public static int getMemberSum() {return sessions.size();}public static int getSum() {return userSum;}public static Boolean isExistInSessions(String username) {String user_name;for (Integer i = 0; i < sessions.size(); i++) {user_name = (String) sessions.get(i);if (user_name.equals(username)) {return true;} else {continue;}}return false;}//此方法在初始化监听器的时候会调用,用于spring注入public void contextInitialized(ServletContextEvent sce) { WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()); this.xxxService=(XxxService)applicationContext.getBean("xxxService"); } public void contextDestroyed(ServletContextEvent sce) { } private XxxService xxxService;//这里是你希望注入的服务类 public XxxService getXxxService(){ return this.xxxService; } public void setxxxService(XxxService xxxService){ this.xxxService=xxxService; }}
在web.xml中加入以下配置代码,注意必须在spring容器配置之后。
<listener> <listener-class> com.xxx.xxx.xxx.OnlineCountListener </listener-class> </listener>
然后在spring配置
<bean id="xxxService">xxx.xxx.xxx.xxxService</bean>
我的网店,有劳各位参观参观 http://mrs-x.taobao.com/
博主 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..xiaoB 在
2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..BUG4 在
你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..博主 在
go语言+beego框架中获取get,post请求的所有参数中评论 @ t1 直接在router.go文件中配就ok..Jade 在
如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号