javaweb监听器

来源:互联网 发布:5355端口 编辑:程序博客网 时间:2024/06/06 03:57

1、监听器概述


  监听器是javaweb中三大组件(Servlet、Listener、Filter)之一,整个监听事务中参与对象分为:

    事件源(被监听者);

    监听者;

    监听器;

    事件。

  用生活中的事务举例,警察是监听者;小偷是事件源;小偷盗窃是事件;警察监听小偷,当小偷执行(触发)盗窃这个事件的时候,立即执行监听器中的代码(抓捕)。需要注意的是,只有当触发事件时,相应的监听器中的代码才能执行。

  被监听的事件源为:ServletContext、HttpSession、ServletRequest,即三大域对象。监听器共有8种,分别为:

    生命周期监听器:监听域对象“创建”与“销毁”,3种,即三大域对象各一种;

    属性监听器:监听域对象操作域属性,3种,三大域对象各一种;

    感知监听器:监听HttpSession,2种。


2、生命周期监听器


  (1)生命周期监听器3种:


     ServletContextListener:Tomcat启动和关闭时调用下面两个方法

      public void contextInitialized(ServletContextEvent evt):ServletContext对象被创建后调用;

      public void contextDestroyed(ServletContextEvent evt):ServletContext对象被销毁前调用;


     HttpSessionListener:开始会话和结束会话时调用下面两个方法

      public void sessionCreated(HttpSessionEvent evt):HttpSession对象被创建后调用;

      public void sessionDestroyed(HttpSessionEvent evt):HttpSession对象被销毁前调用;


     ServletRequestListener:开始请求和结束请求时调用下面两个方法

      public void requestInitiallized(ServletRequestEvent evt):ServletRequest对象被创建后调用;

      public void requestDestroyed(ServletRequestEvent evt):ServletRequest对象被销毁前调用。


  (2)事件对象


     ServletContextEvent:ServletContext getServletContext();

     HttpSeessionEvent:HttpSession getSession();

     ServletRequestEvent:

      ServletRequest getServletRequest();

      ServletContext getServletContext()。


  (3)实例:


    步骤:  

      编写MyServletContextListener类,实现ServletContextListener接口;

      在web.xml文件中部署监听器;

      为了看到session销毁的效果,在web.xml文件中设置session失效时间为1分钟;

[java] view plain copy
  1. <span style="font-size:18px;">/* 
  2.  * <span style="font-family: Arial, Helvetica, sans-serif;">ServletContextListener </span><span style="font-family: Arial, Helvetica, sans-serif;">实现类</span> 
  3.  * contextDestroyed() -- 在ServletContext对象被销毁前调用 
  4.  * contextInitialized() --  -- 在ServletContext对象被创建后调用 
  5.  * ServletContextEvent -- 事件类对象 
  6.  *     该类有getServletContext(),用来获取ServletContext对象,即获取事件源对象 
  7.  */  
  8. public class MyServletContextListener implements ServletContextListener {  
  9.     public void contextDestroyed(ServletContextEvent evt) {  
  10.         System.out.println("销毁ServletContext对象");  
  11.     }  
  12.   
  13.   
  14.     public void contextInitialized(ServletContextEvent evt) {  
  15.         System.out.println("创建ServletContext对象");  
  16.     }  
  17. }</span>  

[java] view plain copy
  1. <span style="font-size:18px;">/* 
  2.  * HttpSessionListener实现类 
  3.  * sessionCreated() -- 在HttpSession对象被创建后被调用 
  4.  * sessionDestroyed() --  -- 在HttpSession对象被销毁前调用 
  5.  * HttpSessionEvent -- 事件类对象 
  6.  *     该类有getSession(),用来获取当前HttpSession对象,即获取事件源对象 
  7.  */  
  8. public class MyHttpSessionListener implements HttpSessionListener {  
  9.     public void sessionCreated(HttpSessionEvent evt) {  
  10.         System.out.println("创建session对象");  
  11.     }  
  12.   
  13.   
  14.     public void sessionDestroyed(HttpSessionEvent evt) {  
  15.         System.out.println("销毁session对象");  
  16.     }  
  17. }</span>  

[java] view plain copy
  1. <span style="font-size:18px;">/* 
  2.  * ServletRequestListener实现类 
  3.  * requestDestroyed() -- 在ServletRequest对象被销毁前调用 
  4.  * requestInitialized() -- 在ServletRequest对象被创建后调用 
  5.  * ServletRequestEvent -- 事件类对象 
  6.  *     该类有getServletContext(),用来获取ServletContext对象 
  7.  *     该类有getServletRequest(),用来获取当前ServletRequest对象,即事件源对象 
  8.  */  
  9. public class MyServletRequestListener implements ServletRequestListener {  
  10.     public void requestDestroyed(ServletRequestEvent evt) {  
  11.         System.out.println("销毁request对象");  
  12.     }  
  13.   
  14.   
  15.     public void requestInitialized(ServletRequestEvent evt) {  
  16.         System.out.println("创建request对象");  
  17.     }  
  18. }</span>  
    XML配置:

[html] view plain copy
  1. <span style="font-size:18px;"><listener>  
  2. <listener-class>cn.itcast.listener.MyServletContextListener</listener-class>  
  3. </listener>  
  4. <listener>  
  5. <listener-class>cn.itcast.listener.MyHttpSessionListener</listener-class>  
  6. </listener>  
  7. <listener>  
  8. <listener-class>cn.itcast.listener.MyServletRequestListener</listener-class>  
  9. </listener>  
  10. <session-config>  
  11.  <session-timeout>1</session-timeout>  
  12. </session-config></span>  

3、属性监听器


  (1)属性监听器共3种:


     ServletContextAttributeListener:在ServletContext域进行增、删、改属性时调用下面方法:

      public void attributeAdded(ServletContextAttributeEvent evt);

      public void attributeRemoved(ServletContextAttributeEvent evt);

      public void attributeReplaced(ServletContextAttributeEvent evt)。


     HttpSessionAttributeListener:在HttpSession域进行增、删、改属性时调用下面方法:

      public void attributeAdded(HttpSessionBindingEvent evt);

      public void attributeRemoved (HttpSessionBindingEvent evt);

      public void attributeReplaced (HttpSessionBindingEvent evt) 。


     ServletRequestAttributeListener:在ServletRequest域进行增、删、改属性时调用下面方法:

      public void attributeAdded(ServletRequestAttributeEvent evt);

      public void attributeRemoved (ServletRequestAttributeEvent evt);

      public void attributeReplaced (ServletRequestAttributeEvent evt)。


  (2)事件对象介绍:


     ServletContextAttributeEvent:

      String getName():获取当前操作的属性名;

      Object getValue():获取当前操作的属性值;

      ServletContext getServletContext():获取ServletContext对象。


     HttpSessionBindingEvent:

      String getName():获取当前操作的属性名;

      Object getValue():获取当前操作的属性值;

      HttpSession getSession():获取当前操作的session对象。


     ServletRequestAttributeEvent:

      String getName():获取当前操作的属性名;

      Object getValue():获取当前操作的属性值;

      ServletContext getServletContext():获取ServletContext对象;

      ServletRequest getServletRequest():获取当前操作的ServletRequest对象。


[java] view plain copy
  1. <span style="font-size:18px;">public class MyListener implements ServletContextAttributeListener,  
  2.         ServletRequestAttributeListener, HttpSessionAttributeListener {  
  3.     public void attributeAdded(HttpSessionBindingEvent evt) {  
  4.         System.out.println("向session中添加属性:" + evt.getName() + "=" + evt.getValue());  
  5.     }  
  6.   
  7.   
  8.     public void attributeRemoved(HttpSessionBindingEvent evt) {  
  9.         System.out.println("从session中移除属性:" + evt.getName() + "=" + evt.getValue());  
  10.     }  
  11.   
  12.   
  13.     public void attributeReplaced(HttpSessionBindingEvent evt) {  
  14.         System.out.println("修改session中的属性:" + evt.getName() + "=" + evt.getValue());  
  15.     }  
  16.   
  17.   
  18.     public void attributeAdded(ServletRequestAttributeEvent evt) {  
  19.         System.out.println("向request中添加属性:" + evt.getName() + "=" + evt.getValue());  
  20.     }  
  21.   
  22.   
  23.     public void attributeRemoved(ServletRequestAttributeEvent evt) {  
  24.         System.out.println("从request中移除属性:" + evt.getName() + "=" + evt.getValue());  
  25.     }  
  26.   
  27.   
  28.     public void attributeReplaced(ServletRequestAttributeEvent evt) {  
  29.         System.out.println("修改request中的属性:" + evt.getName() + "=" + evt.getValue());  
  30.     }  
  31.   
  32.   
  33.     public void attributeAdded(ServletContextAttributeEvent evt) {  
  34.         System.out.println("向context中添加属性:" + evt.getName() + "=" + evt.getValue());  
  35.     }  
  36.   
  37.   
  38.     public void attributeRemoved(ServletContextAttributeEvent evt) {  
  39.         System.out.println("从context中移除属性:" + evt.getName() + "=" + evt.getValue());  
  40.     }  
  41.   
  42.   
  43.     public void attributeReplaced(ServletContextAttributeEvent evt) {  
  44.         System.out.println("修改context中的属性:" + evt.getName() + "=" + evt.getValue());  
  45.     }  
  46. }</span>  


[java] view plain copy
  1. <span style="font-size:18px;">public class ListenerServlet extends BaseServlet {  
  2.     public String contextOperation(HttpServletRequest request, HttpServletResponse response)  
  3.             throws ServletException, IOException {  
  4.         ServletContext context = this.getServletContext();  
  5.         context.setAttribute("a""a");  
  6.         context.setAttribute("a""A");  
  7.         context.removeAttribute("a");  
  8.         return "/index.jsp";  
  9.     }  
  10.       
  11.     ///////////////////////////////  
  12.       
  13.     public String sessionOperation(HttpServletRequest request, HttpServletResponse response)  
  14.             throws ServletException, IOException {  
  15.         HttpSession session = request.getSession();  
  16.         session.setAttribute("a""a");  
  17.         session.setAttribute("a""A");  
  18.         session.removeAttribute("a");  
  19.         return "/index.jsp";  
  20.     }  
  21.   
  22.     ///////////////////////////////  
  23.       
  24.     public String requestOperation(HttpServletRequest request, HttpServletResponse response)  
  25.             throws ServletException, IOException {  
  26.         request.setAttribute("a""a");  
  27.         request.setAttribute("a""A");  
  28.         request.removeAttribute("a");  
  29.         return "/index.jsp";  
  30.     }  
  31. }</span>  

[html] view plain copy
  1. <span style="font-size:18px;">  <body>  
  2.     <a href="<c:url value='/ListenerServlet?method=contextOperation'/>">SevletContext操作属性</a>  
  3.     <br/>  
  4.     <a href="<c:url value='/ListenerServlet?method=sessionOperation'/>">HttpSession操作属性</a>  
  5.     <br/>  
  6.     <a href="<c:url value='/ListenerServlet?method=requestOperation'/>">ServletRequest操作属性</a> |   
  7.   </body></span>  


4、感知监听器


  感知监听器有HttpSessionBindingListener和,这是两个与HttpSession相关的特殊的监听器,特点如下:


    ● 不用在web.xml文件中部署;

    ● 这两个监听器不是给session添加,而是给Bean添加。即让Bean类实现监听器接口,然后再把Bean对象添加到session域中。


  (1)HttpSessionBindingListener(绑定解绑监听器):


    当某个类实现了该接口后,可以感知本类对象添加到session中,以及感知从session中移除。例如让Person类实现HttpSessionBindingListener接口,那么当把Person对象添加到session中,或者把Person对象从session中移除时会调用下面两个方法:


       public void valueBound(HttpSessionBindingEvent event):当把监听器对象添加到session中会调用监听器对象的本方法;

       public void valueUnbound(HttpSessionBindingEvent event):当把监听器对象从session中移除时会调用监听器对象的本方法;


    这里要注意,HttpSessionBindingListener监听器的使用与前面介绍的都不相同,当该监听器对象添加到session中,或把该监听器对象从session移除时会调用监听器中的方法。并且无需在web.xml文件中部署这个监听器。

    实例:

      编写Person类,让其实现HttpSessionBindingListener监听器接口;

      编写Servlet类,一个方法向session中添加Person对象,另一个从session中移除Person对象;

      在index.jsp中给出两个超链接,分别访问Servlet中的两个方法。

[java] view plain copy
  1. <span style="font-size:18px;">public class Person implements HttpSessionBindingListener {  
  2.     private String name;  
  3.     private int age;  
  4.     private String sex;  
  5.       
  6.     public Person(String name, int age, String sex) {  
  7.         super();  
  8.         this.name = name;  
  9.         this.age = age;  
  10.         this.sex = sex;  
  11.     }  
  12.   
  13.     public Person() {  
  14.         super();  
  15.     }  
  16.   
  17.     public String toString() {  
  18.         return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";  
  19.     }  
  20.   
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.   
  25.     public void setName(String name) {  
  26.         this.name = name;  
  27.     }  
  28.   
  29.     public int getAge() {  
  30.         return age;  
  31.     }  
  32.   
  33.     public void setAge(int age) {  
  34.         this.age = age;  
  35.     }  
  36.   
  37.     public String getSex() {  
  38.         return sex;  
  39.     }  
  40.   
  41.     public void setSex(String sex) {  
  42.         this.sex = sex;  
  43.     }  
  44.   
  45.     public void valueBound(HttpSessionBindingEvent evt) {  
  46.         System.out.println("把Person对象存放到session中:" + evt.getValue());  
  47.     }  
  48.   
  49.     public void valueUnbound(HttpSessionBindingEvent evt) {  
  50.         System.out.println("从session中移除Pseron对象:" + evt.getValue());  
  51.     }  
  52. }  
  53. </span>  

    ListenerServlet.Java:

[java] view plain copy
  1. <span style="font-size:18px;">public class ListenerServlet extends BaseServlet {  
  2.     public String addPerson(HttpServletRequest request, HttpServletResponse response)  
  3.             throws ServletException, IOException {  
  4.         Person p = new Person("zhangSan"23"male");  
  5.         request.getSession().setAttribute("person", p);  
  6.         return "/index.jsp";  
  7.     }  
  8.       
  9.     public String removePerson(HttpServletRequest request, HttpServletResponse response)  
  10.             throws ServletException, IOException {  
  11.         request.getSession().removeAttribute("person");  
  12.         return "/index.jsp";  
  13.     }</span>  

    index.jsp:

[html] view plain copy
  1. <span style="font-size:18px;">  <body>  
  2.     <a href="<c:url value='/ListenerServlet?method=addPerson'/>">addPerson</a>  
  3.     <br/>  
  4.     <a href="<c:url value='/ListenerServlet?method=removePerson'/>">removePerson</a>  
  5.     <br/>  
  6.   </body></span>  


  (2)HttpSessionActivationListener(钝化活化监听器):


    Tomcat会在session从时间不被使用时钝化session对象,所谓钝化session,就是把session通过序列化的方式保存到硬盘文件中。当用户再使用session时,Tomcat还会把钝化的对象再活化session,所谓活化就是把硬盘文件中的session在反序列化回内存。

    当session被Tomcat钝化时,session中存储的对象也被纯化,当session被活化时,也会把session中存储的对象活化。如果某个类实现了HttpSessionActiveationListener接口后,当对象随着session被钝化和活化时,下面两个方法就会被调用:


       public void sessionWillPassivate(HttpSessionEvent se):当对象感知被活化时调用本方法;

       public void sessionDidActivate(HttpSessionEvent se):当对象感知被钝化时调用本方法;

    HttpSessionActivationListener监听器与HttpSessionBindingListener监听器相似,都是感知型的监听器,例如让Person类实现了HttpSessionActivationListener监听器接口,并把Person对象添加到了session中。

    当Tomcat钝化session时,同时也会钝化session中的Person对象,这时Person对象就会感知到自己被钝化了,其实就是调用Person对象的sessionWillPassivate()方法。

    当用户再次使用session时,Tomcat会活化session,这时Person会感知到自己被活化,其实就是调用Person对象的sessionDidActivate()方法。

    注意:因为钝化和活化session,其实就是使用序列化和反序列化技术把session从内存保存到硬盘,和把session从硬盘加载到内存。这说明如果Person类没有实现Serializable接口,那么当session钝化时就不会钝化Person,而是把Person从session中移除再钝化!这也说明session活化后,session中就不在有Person对象了。

    实例:

      先不管HttpSessionActivationListener监听器接口,先来配置Tomcat钝化session的参数,把下面配置文件放到tomcat\conf\catalina\localhost目录下!文件名称为项目名称。

[html] view plain copy
  1. <span style="font-size:18px;"><Context>  
  2.     <Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">  
  3.         <Store className="org.apache.catalina.session.FileStore" directory="mysession"/>  
  4.     </Manager>  
  5. </Context></span>  


      访问项目的index.jsp页面,这会使Tomcat创建Session对象,然后等待一分钟后,查看Tomcat\work\Catalina\localhost\listener\mysession目录下是否会产生文件,如果产生了,说明钝化session的配置成功了,可以开始下一步了。

      创建Person类,让Person类实现HttpSessionActivationListener和Serializable接口:

[java] view plain copy
  1. <span style="font-size:18px;">public class Person implements HttpSessionActivationListener, Serializable {  
  2.     private String name;  
  3.     private int age;  
  4.     private String sex;  
  5.       
  6.     public Person(String name, int age, String sex) {  
  7.         super();  
  8.         this.name = name;  
  9.         this.age = age;  
  10.         this.sex = sex;  
  11.     }  
  12.   
  13.     public Person() {  
  14.         super();  
  15.     }  
  16.   
  17.     public String toString() {  
  18.         return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";  
  19.     }  
  20.   
  21.     public String getName() {  
  22.         return name;  
  23.     }  
  24.   
  25.     public void setName(String name) {  
  26.         this.name = name;  
  27.     }  
  28.   
  29.     public int getAge() {  
  30.         return age;  
  31.     }  
  32.   
  33.     public void setAge(int age) {  
  34.         this.age = age;  
  35.     }  
  36.   
  37.     public String getSex() {  
  38.         return sex;  
  39.     }  
  40.   
  41.     public void setSex(String sex) {  
  42.         this.sex = sex;  
  43.     }  
  44.   
  45.     public void sessionDidActivate(HttpSessionEvent evt) {  
  46.         System.out.println("session已经活化");  
  47.     }  
  48.   
  49.     public void sessionWillPassivate(HttpSessionEvent evt) {  
  50.         System.out.println("session被钝化了!");  
  51.     }  
  52. }</span>  


      与上例一样,编写Servlet,提供两个方法:一个向session中添加Person对象,另一个从session中移除Person对象:

[java] view plain copy
  1. <span style="font-size:18px;">public class ListenerServlet extends BaseServlet {  
  2.     public String addPerson(HttpServletRequest request, HttpServletResponse response)  
  3.             throws ServletException, IOException {  
  4.         Person p = new Person("zhangSan"23"male");  
  5.         request.getSession().setAttribute("person", p);  
  6.         return "/index.jsp";  
  7.     }  
  8.       
  9.     public String removePerson(HttpServletRequest request, HttpServletResponse response)  
  10.             throws ServletException, IOException {  
  11.         request.getSession().removeAttribute("person");  
  12.         return "/index.jsp";  
  13.     }  
  14. }</span>  


      在index.jsp页面中给出访问addPerson()和removePerson()的方法:

[html] view plain copy
  1. <span style="font-size:18px;">  <body>  
  2.     <a href="<c:url value='/ListenerServlet?method=addPerson'/>">addPerson</a>  
  3.     <br/>  
  4.     <a href="<c:url value='/ListenerServlet?method=removePerson'/>">removePerson</a>  
  5.     <br/>  
  6.   </body></span>  

    

    打开index.jsp页面,这时Tomcat会创建session,必须在1分钟之前点击addPerson链接,这能保证在session被钝化之前把Person对象添加到session中;

    等待一分钟,这时session会被钝化,也就会调用Person的sessionWillPassivate();

    刷新一下index.jsp页面,这会使session活化,会调用Person的sessionDidActivate()方法。


  小结:监听器体现的就是一种监听机制,当事件源触发特定事件时,就会触发监听器中的代码从而执行。监听器共三种八类,分别监听ServletContext、HttpSession、ServletRequest三大域对象。

原创粉丝点击