ServletContextAttributeListener的用法和实例

2)ServletContextAttributeListener:

ServletContextAttributeListener能监测到有人正在往ServletContext里添加属性。你可以采取相应的措施。

例 2.2.2.1

web.xml当中再加入一段话:

  <listener>
    <listener-class>com.MyServletContextAttributeListener</listener-class>
  </listener>



package com;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
public class MyServletContextAttributeListener implements ServletContextAttributeListener  {
    public void attributeAdded(ServletContextAttributeEvent arg0) {
        System.out.println("增加了" + arg0.getName() + " " +
                arg0.getValue() );
    }
    public void attributeRemoved(ServletContextAttributeEvent arg0) {
        System.out.println("去除了" + arg0.getName() + " " +
                arg0.getValue() );
    }
    public void attributeReplaced(ServletContextAttributeEvent arg0) {
        System.out.println("取代了" + arg0.getName() + " " +
                arg0.getValue()+"现在的新值是"+arg0.getServletContext().getAttribute(arg0.getName()) );
    }
}
 

更多内容请见原文,原文转载自:https://blog.csdn.net/qq_44638460/article/details/104101837

posted @ 2022-03-01 18:01  malala  阅读(32)  评论(0编辑  收藏  举报