1.定义
◆ Servlet规范定义的一种特殊类。
◆ 用于监听ServletContext,HttpSession,ServletRequest等域对象的创建、销毁及其属性修改发生变化的事件。
◆ 监听器可以在事件发生前后进行一些必要的处理操作。
2.常见应用场景
◆ 统计在线人数和在线用户
◆ 应用启动时完成信息初始化工作
◆ 网站访问量的统计
◆ 与Spring结合
◆ Web系统防止用户重复登陆
3.监听器的实现步骤
1.编写java类实现监听器接口,并实现其接口方法。
2.在web.xml文件中对实现的监听器类进行注册。
4.Java 监听器启动顺序
Ps:取决于web.xml注册监听器的顺序,并且关闭服务器的时候,销毁顺序是——先进后出。
5.Java 监听器分类
1、按监听对象
◆ ServletContext对象监听器
◆ HttpSession对象监听器
◆ ServletRequest对象监听器
2、按监听事件
◆ 域对象自身的创建和销毁事件监听器
◆ 域对象中属性的创建、替换和消除事件监听器
◆ 绑定到session中的某个对象的状态事件监听器
6.ServletContextListener
7.
<context-param><param-name>app_name</param-name><param-value>ListenerWeb</param-value></context-param><context-param><param-name>version</param-name><param-value>1.0</param-value></context-param><listener><listener-class>com.imooc.listener.MyServletContextListener</listener-class></listener>
packagecom.imooc.listener; importjavax.servlet.ServletContextEvent; importjavax.servlet.ServletContextListener; publicclassMyServletContextListenerimplementsServletContextListener { @OverridepublicvoidcontextDestroyed(ServletContextEventsce) { StringappName= (String)sce.getServletContext().getAttribute("app_name"); Stringversion= (String)sce.getServletContext().getAttribute("version"); System.out.println("MyServletContextListener destroyed,appName:"+appName+",version:"+version); } @OverridepublicvoidcontextInitialized(ServletContextEventsce) { StringappName=sce.getServletContext().getInitParameter("app_name"); Stringversion=sce.getServletContext().getInitParameter("version"); sce.getServletContext().setAttribute("app_name", appName); sce.getServletContext().setAttribute("version", version); System.out.println("MyServletContextListener init,appName:"+appName+",version:"+version); } }
Ps:Web服务器启动时,初始化Init()方法;Web服务器关闭时,销毁Destroyed()方法。
8.HttpSessionListener(session 全局)
<listener><listener-class>com.imooc.listener.MyHttpSessionListener</listener-class></listener><session-config><session-timeout>1</session-timeout><!--必须是int类型,表示分钟数--></session-config>
packagecom.imooc.listener; importjava.util.Date; importjavax.servlet.http.HttpSessionEvent; importjavax.servlet.http.HttpSessionListener; publicclassMyHttpSessionListenerimplementsHttpSessionListener { @OverridepublicvoidsessionCreated(HttpSessionEventhse) { StringsessionId=hse.getSession().getId(); DatecreateTime=newDate(hse.getSession().getCreationTime()); System.out.println("session id:"+sessionId+",createTime:"+createTime); } @OverridepublicvoidsessionDestroyed(HttpSessionEventhse) { StringsessionId=hse.getSession().getId(); System.out.println("session destroyed,session id:"+sessionId); } }
9.ServletRequestListener
packagecom.imooc.listener; importjavax.servlet.ServletRequestEvent; importjavax.servlet.ServletRequestListener; importjavax.servlet.http.HttpServletRequest; publicclassMyServletRequestListenerimplementsServletRequestListener { @OverridepublicvoidrequestDestroyed(ServletRequestEventsre) { System.out.println("request listener,request destroyed... ..."); } @OverridepublicvoidrequestInitialized(ServletRequestEventsre) { HttpServletRequestrequest= (HttpServletRequest) sre.getServletRequest(); Stringpath=request.getRequestURI(); Stringpar=request.getParameter("par"); System.out.println("request listener,path:"+path+",par is :"+par); } }
<listener><listener-class>com.imooc.listener.MyServletRequestListener</listener-class></listener>
<label><span>ServletRequestListener测试</span><ahref="servlet_request.jsp?par=request_par"target="_blank">初始化</a></label>
Ps:请求发送马上调用Init,同时马上结束调用Destroyed。
10.ServletContextAttributeListener(session、request 类似)
packagecom.imooc.listener; importjavax.servlet.ServletContextAttributeEvent; importjavax.servlet.ServletContextAttributeListener; publicclassMyServletContextAttributeListenerimplementsServletContextAttributeListener { @OverridepublicvoidattributeAdded(ServletContextAttributeEventscae) { System.out.println("ServletContext#attAdded#name:"+scae.getName() +"#value:"+scae.getValue()); } @OverridepublicvoidattributeRemoved(ServletContextAttributeEventscae) { System.out.println("ServletContext#attRemoved#name:"+scae.getName() +"#value:"+scae.getValue()); } @OverridepublicvoidattributeReplaced(ServletContextAttributeEventscae) { System.out.println("ServletContext#attReplaced#name:"+scae.getName() +"#value:"+scae.getValue()); } }
<listener><listener-class>com.imooc.listener.MyServletContextAttributeListener</listener-class></listener>
<label><span>AttributeListener测试</span><ahref="attribute_init.jsp"target="_blank">属性初始化</a><ahref="attribute_replace.jsp"target="_blank">属性修改</a><ahref="attribute_remove.jsp"target="_blank">属性移除</a></label>
// attribute_init.jsp<%application.setAttribute("servletcontextPar", "servletcontext_par"); %>// attribute_remove.jsp<%application.removeAttribute("servletcontextPar"); %>// attribute_replace.jsp<%application.setAttribute("servletcontextPar", "servletcontext_par1"); %>
Ps:可能会输出其他的信息(意料之外),可能是服务器自带的其他信息被监听到了。
11.HttpSessionBindingListener(不常用)(session 一对一)
packagecom.imooc.model; importjavax.servlet.http.HttpSessionBindingEvent; importjavax.servlet.http.HttpSessionBindingListener; publicclassUserimplementsHttpSessionBindingListener { privateStringusername; privateStringpassword; @OverridepublicvoidvalueBound(HttpSessionBindingEventhsbe) { Stringname=hsbe.getName(); System.out.println("value bound,name:"+name); } @OverridepublicvoidvalueUnbound(HttpSessionBindingEventhsbe) { Stringname=hsbe.getName(); System.out.println("value unbound,name:"+name); } // 省略 Getters and Setters..}
<label><span>BindingListener测试</span><ahref="bound.jsp"target="_blank">绑定测试</a><ahref="unbound.jsp"target="_blank">解绑测试</a></label>
// bound.jsp<%session.setAttribute("user", newcom.imooc.model.User()); %>// unbound.jsp<%session.removeAttribute("user"); %>
Ps1:getName():返回发生绑定和取消绑定的对象的名字。
Ps2:不需要在web.xml中注册。
12.待更新...