
package sanglp.servlet; import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.annotation.WebListener; /**
* Created by Administrator on 2016/10/5.
* 监听ServletContext范围内属性改变的Listener
*/
@WebListener
public class MyServletContextAttributeListener implements ServletContextAttributeListener { @Override
public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {
ServletContext application=servletContextAttributeEvent.getServletContext();
//获取添加的属性名和属性值
String name=servletContextAttributeEvent.getName();
Object value=servletContextAttributeEvent.getValue();
System.out.println(application+"范围内添加了"+name+"值为"+value+"的属性"); } @Override
public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {
ServletContext application=servletContextAttributeEvent.getServletContext();
//获取添加的属性名和属性值
String name=servletContextAttributeEvent.getName();
Object value=servletContextAttributeEvent.getValue();
System.out.println(application+"范围内删除了"+name+"值为"+value+"的属性");
} @Override
public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {
ServletContext application=servletContextAttributeEvent.getServletContext();
//获取添加的属性名和属性值
String name=servletContextAttributeEvent.getName();
Object value=servletContextAttributeEvent.getValue();
System.out.println(application+"范围内替换了"+name+"值为"+value+"的属性"); }
}