ServletContextAttributeListener
用于监听ServletContext也就是application范围内属性的变化,实现该结构主要重写三个方法
- attributeAdded()方法:当向application中添加参数的时候会自动调用
- attributeRemoved()方法:当把application中的参数移除的时候调用
- attributeReplaced()方法:当修改application中的参数时调用
1 package com.huayu.listener; 2 3 import javax.servlet.ServletContextAttributeEvent; 4 import javax.servlet.ServletContextAttributeListener; 5 import javax.servlet.annotation.WebListener; 6 @WebListener 7 public class Listener3 implements ServletContextAttributeListener{ 8 9 @Override 10 public void attributeAdded(ServletContextAttributeEvent event) { 11 String named=event.getName().toString(); 12 String value=event.getValue().toString(); 13 System.out.println("application中添加了一个"+named+"变量值为"+value); 14 15 } 16 17 @Override 18 public void attributeRemoved(ServletContextAttributeEvent event) { 19 String named=event.getName().toString(); 20 String value=event.getValue().toString(); 21 System.out.println("application中添移除一个"+named+"变量值为"+value); 22 23 24 25 } 26 27 @Override 28 public void attributeReplaced(ServletContextAttributeEvent event) { 29 String named=event.getName().toString(); 30 String value=event.getValue().toString(); 31 System.out.println("application中"+named+"变量值变为"+value); 32 } 33 34 }
ServletRequestListener
用来监听用户的请求到达,请求的结束
requestInitialized:用户请求到达,被初始化时触发该方法
requestDestroyed:用户请求结束,被销毁时触发该方法
ServletRequestAttributeListener
用来监听request范围内变量的变化
attributeAdded()
attributeReplaced()
attributeRemoved()
1 package com.huayu.listener; 2 3 import javax.servlet.ServletRequest; 4 import javax.servlet.ServletRequestAttributeEvent; 5 import javax.servlet.ServletRequestAttributeListener; 6 import javax.servlet.ServletRequestEvent; 7 import javax.servlet.ServletRequestListener; 8 import javax.servlet.annotation.WebListener; 9 import javax.servlet.http.HttpServletRequest; 10 @WebListener 11 public class Listener4 implements ServletRequestListener,ServletRequestAttributeListener{ 12 13 @Override 14 public void requestDestroyed(ServletRequestEvent arg0) { 15 16 HttpServletRequest request=(HttpServletRequest) arg0.getServletRequest(); 17 System.out.println("---发向"+request.getRequestURI()+"请求被销毁-----"); 18 19 } 20 21 @Override 22 public void requestInitialized(ServletRequestEvent arg0) { 23 HttpServletRequest request=(HttpServletRequest) arg0.getServletRequest(); 24 System.out.println("---发向"+request.getRequestURI()+"请求被初始化-----"); 25 } 26 27 @Override 28 public void attributeAdded(ServletRequestAttributeEvent arg0) { 29 ServletRequest request=arg0.getServletRequest(); 30 String name=arg0.getName(); 31 String value=arg0.getValue().toString(); 32 System.out.println(request+"范围内添加了名为"+name+"值为"+value+"的属性"); 33 34 } 35 36 @Override 37 public void attributeRemoved(ServletRequestAttributeEvent arg0) { 38 ServletRequest request=arg0.getServletRequest(); 39 String name=arg0.getName(); 40 String value=arg0.getValue().toString(); 41 System.out.println(request+"范围内添加了名为"+name+"值为"+value+"的属性被删除"); 42 43 } 44 45 @Override 46 public void attributeReplaced(ServletRequestAttributeEvent arg0) { 47 ServletRequest request=arg0.getServletRequest(); 48 String name=arg0.getName(); 49 String value=arg0.getValue().toString(); 50 System.out.println(request+"范围内添加了名为"+name+"值为"+value+"的属性被替换了"); 51 52 } 53 54 }
HttpSessionListener
用于监听用户session的创建和销毁
sessionCreated(HttpSessionEvent se):当session
被建立的时候调用
sessionDestroyed(HttpSessionEvent se)
当session没有任何客户端与之链接的时候执行
HttpSessionAttributeListener
使用方法同上