前言 监听器(Listener)就是监听对象的创建、销毁等状态的变化以及定义一些事件发生后接下来要进行的动作。主要监听的三个域对象为:ServletRequest域、HttpSession域和ServletContext域。本文通过几个简单的例子介绍一下监听器的用法。ServletContextListener监听Servlet上下文 监听Servlet上下文对象可以在系统启动的时候初始化一些数据,方便在使用的时候直接调用。监听器实现代码如下:ComponentpublicclassMyServletContextListenerimplementsServletContextListener{Override在ServletContext对象创建之后马上调用,做初始化publicvoidcontextInitialized(ServletContextEventevent){event。getServletContext()。setAttribute(name,九天银河聊编程);System。out。println(ServletContext对象被创建了);}Override在ServletContext对象销毁之后调用publicvoidcontextDestroyed(ServletContextEventevent){System。out。println(ServletContext对象被销毁了);}}GetMapping(getServletContext)publicStringgetServletContext(HttpServletRequestrequest){ServletContextservletContextrequest。getServletContext();ObjectnameservletContext。getAttribute(name);returnString。valueOf(name);} 执行效果如下: HttpSessionListener获取在线用户数量ComponentpublicclassMyHttpSessionListenerimplementsHttpSessionListener{publicstaticIntegercount0;OverridepublicvoidsessionCreated(HttpSessionEventevent){count;ServletContextapplicationevent。getSession()。getServletContext();application。setAttribute(UserCount,count);System。out。println(有人上线了,现在在线人数为:count人);}OverridepublicvoidsessionDestroyed(HttpSessionEventevent){count;ServletContextapplicationevent。getSession()。getServletContext();application。setAttribute(UserCount,count);System。out。println(有人下线了,现在在线人数为:count人);}}GetMapping(online)publicStringgetOnlinePersoncount(HttpServletRequestrequest){IntegeruserCount(Integer)request。getServletContext()。getAttribute(UserCount);return(userCountnull?0:userCount);}GetMapping(login)publicStringlogined(HttpSessionsession){相同的session,如果sessionid一致,只会被监听一次。session。setAttribute(username,九天银河聊编程);returnsuccess;}GetMapping(logout)publicStringlogout(HttpSessionsession){session。invalidate();将session设置为失效returnsuccess;} 执行127。0。0。1:8090login,控制台显示: 执行127。0。0。1:8090online,返回 执行127。0。0。1:8090logout,控制台显示 ServletRequestListener统计网站访问次数ComponentpublicclassMyServletRequestListenerimplementsServletRequestListener{OverridepublicvoidrequestInitialized(ServletRequestEventservletRequestEvent){ObjectcountObjectservletRequestEvent。getServletContext()。getAttribute(count);System。out。println(历史访问次数:countObject);Integercount0;if(countObject!null)countInteger。valueOf(countObject。toString());count;servletRequestEvent。getServletContext()。setAttribute(count,count);}OverridepublicvoidrequestDestroyed(ServletRequestEventservletRequestEvent){System。out。println(当前访问次数:servletRequestEvent。getServletContext()。getAttribute(count));}} 随便执行一个接口请求,控制台打印如下: 再次执行: 自定义监听方式 定义监听事件ublicclassListenerEventextendsApplicationEvent{Stringnamenull;publicStringgetName(){returnname;}publicvoidsetName(Stringname){this。namename;}publicListenerEvent(Objectsource,Stringvalue){super(source);namevalue;}} 定义监听器ComponentpublicclassMySpringBootListenerimplementsApplicationListenerListenerEvent{OverridepublicvoidonApplicationEvent(ListenerEventlistenerEvent){StringeventInfolistenerEvent。getName();System。out。println(eventInfo);}} 发布事件RestControllerpublicclassListenerController{ResourceprivateApplicationContextapplicationContext;GetMapping(listener)publicStringlistener(){ListenerEventeventnewListenerEvent(this,九天银河聊编程);applicationContext。publishEvent(event);return;}} 执行127。0。0。1:8090listener,控制台显示 监听器说明 在目前的ServletAPI中提供的web事件监听器接口有以下几个:ServletContextListener监听servletContext对象的创建以及销毁contextInitialized(ServletContextEventevent)创建时执行contextDestroyed(ServletContextEventevent)销毁时执行HttpSessionListener监听session对象的创建以及销毁sessionCreated(HttpSessionEventevent)创建时执行sessionDestroyed(HttpSessionEventevent)销毁时执行ServletRequestListener监听request对象的创建以及销毁requestInitialized(ServletRequestEventevent)创建时执行requestDestroyed(ServletRequestEventevent)销毁时执行ServletContextAttributeListener监听servletContext对象中属性的改变attributeAdded(ServletContextAttributeEventevent)添加属性时执行attributeReplaced(ServletContextAttributeEventevent)修改属性时执行attributeRemoved(ServletContextAttributeEventevent)删除属性时执行HttpSessionAttributeListener监听session对象中属性的改变attributeAdded(HttpSessionBindingEventevent)添加属性时执行attributeReplaced(HttpSessionBindingEventevent)修改属性时执行attributeRemoved(HttpSessionBindingEventevent)删除属性时执行ServletRequestAttributeListener监听request对象中属性的改变attributeAdded(ServletRequestAttributeEventevent)添加属性时执行attributeReplaced(ServletRequestAttributeEventevent)修改属性时执行attributeRemoved(ServletRequestAttributeEventevent)删除属性时执行生命周期 request 指一个URL请求,当发送一个请求时被创建,当一个响应返回时,即被销毁。 session 当一个客户端访问一个WEB应用时创建,标记一个用户与服务器之间的多次请求。session失效有以下几个情况:session过期,即用户长时间不访问服务器造成过期用户退出系统,即执行session的invalidate方法,清理session当前web应用被卸载(session未被持久化) application 贯穿于当前的WEB应用的生命周期,当前WEB应用被加载时创建application对象,当前WEB应用被卸载时销毁application对象。 每天一个小知识,每天进步一点点!!!〔加油〕〔加油〕〔加油〕