分类: Java
2015-12-24 14:17:58
在web.xml配置相应的监听器可以监听系统的一些事件。具体有如下集中监听器接口
ServletContextListener:用于监听Web应用的开启和关闭。
contextInitialized(ServletContextEvent sce):启动程序时,系统调用listener的该方法
contextDestoryed(SevletContextEvent sce):关闭程序时,系统调用该listener的方法
ServletContextAttributeListener:用于监听application范围内的属性的值的改变
attributeAdd(ServletContextAttributeEvent evnet):当程序把一个属性放入application范围
attributeRemoved(ServletContextAttributeEvent):当程序把一个属性从application范围移除
attributeReplaced(ServletContextAttributeEvent):当程序把一个属性从application范围内替
ServletRequestListener:用于监听request请求
requestInitialized(SevletRequestEvent):用户请求到底,被初始化是触发
requestDestroyed(SevletRequestEvent):用户请求结束,被销毁时出发
ServletRequestAttributeListener:用于监听request范围内的属性的值的变化
attributeAdd(ServletContextAttributeEvent evnet):当程序把一个属性放入request范围
attributeRemoved(ServletContextAttributeEvent):当程序把一个属性从request范围移除
attributeReplaced(ServletContextAttributeEvent):当程序把一个属性从request范围内替
HttpSessionListener:用于监听session的开始和结束
sessionCreated(HttpSessionEvent se):用户与服务器的会话开始
sessionDestotyed(HttpSessionEvent se):用户与服务器会话断开时
HttpSessionAttributeListener:用于监听session属性范围内值的变化。
attributeAdd(ServletContextAttributeEvent evnet):当程序把一个属性放入session范围
attributeRemoved(ServletContextAttributeEvent):当程序把一个属性从session范围移除
attributeReplaced(ServletContextAttributeEvent):当程序把一个属性从session范围内替
通过检查httpRequestListener可以精确地监控在线用户的状态,当用户请求到达时,系统将1:用户的seesionId,用户名,用户ip,正在访问的资源,访问时间记录下来。:
2:启动一条后台流程,这条线程每隔一段时间检查用户的在线状态,如果用户的最后访问时间与当前时间相差超过了指定的时间,将这条在线记录删除,这条线程可以实现ServletContextListener