Lisenter

来源:互联网 发布:mysql date转字符串 编辑:程序博客网 时间:2024/06/05 06:12

Listener是Servlet的监听器,可以监听客户端的请求,服务端的操作等


定义监听器类的步骤

1)要想让一个类成为监听器类,就必须去实现监听接口,及实现接口的方法,

常见的监听接口如下:

  • ServletContextListener
  • ServletContextAtrributeListener
  • HttpSessionListener
  • HttpSessionAttributeListener

2)在web.xml中配置相应的信息


例如

<一>定义一个监听器类

[java] view plain copy
  1. package com.servlet.listener;  
  2.   
  3. import javax.servlet.ServletContextEvent;  
  4. import javax.servlet.ServletContextListener;  
  5.   
  6. //ServletContextListener 监听器  
  7. //监听器的初始化比 servlet和filter 都优先,而销毁比servlet和filter 都慢  
  8. public class MyServletContextListener implements ServletContextListener {  
  9.       
  10.     //初始化   当创建ServletContext时该方法得到调用         
  11.     public void contextInitialized(ServletContextEvent sce )  
  12.     {  
  13.         System.out.println("Listener contextInitialized : "+ sce.getServletContext());  
  14.     }  
  15.     //当销毁ServletContext时该方法得到调用  
  16.     public void contextDestroyed(ServletContextEvent sce)  
  17.     {  
  18.         System.out.println("Listener contextDestroyed : "+ sce.getServletContext());  
  19.     }  
  20.   
  21. }  

定义完一个类后,下一步就要在web.xml中配置信息,该配置信息必须在filter和Servlet配置信息之前

<二>配置信息如下

[html] view plain copy
  1. <listener>  
  2.     
  3.   <listener-class>com.servlet.listener.MyServletContextListener</listener-class>  
  4.   
  5. </listener>  


<一>在定义一个监听器类

[java] view plain copy
  1. package com.servlet.listener;  
  2.   
  3. import javax.servlet.ServletContextAttributeEvent;  
  4. import javax.servlet.ServletContextAttributeListener;  
  5.   
  6. public class MyServletContextAttributeListener implements  
  7.         ServletContextAttributeListener {  
  8.       
  9.     //当增加一个属性值时,该方法得到调用  
  10.     public void attributeAdded(ServletContextAttributeEvent scab) {  
  11.         // TODO Auto-generated method stub  
  12.           
  13.         System.out.println("attributeAdded:");  
  14.           
  15.         System.out.println("name: " + scab.getName() + " value :" + scab.getValue());  
  16.   
  17.     }  
  18.       
  19.     //当移除一个属性值时,该方法得到调用  
  20.     public void attributeRemoved(ServletContextAttributeEvent scab) {  
  21.         // TODO Auto-generated method stub  
  22.         System.out.println("attributeRemoved");  
  23.           
  24.         System.out.println("name: " + scab.getName() + " value :" + scab.getValue());  
  25.   
  26.     }  
  27.       
  28.     //当替换一个属性值时,该方法得到调用  
  29.     public void attributeReplaced(ServletContextAttributeEvent scab) {  
  30.         // TODO Auto-generated method stub  
  31.           
  32.         System.out.println("attributeReplaced");  
  33.           
  34.         //scab.getValue()返回被替换的值也就是 旧值  
  35.         System.out.println("name: " + scab.getName() + " value :" + scab.getValue());  
  36.   
  37.     }  
  38.   
  39. }  

<二>web.xml的配置信息

[html] view plain copy
  1. <listener>  
  2.     
  3.   <listener-class>com.servlet.listener.MyServletContextAttributeListener</listener-class>  
  4.   
  5. </listener>  


其它的定义监听器类和上面两个差不多


监听器的初始化(ServletContextListener的初始化方法)比 servlet和filter 都优先,而销毁比servlet和filter 都慢


原创粉丝点击