1.新建一个Demo4代码如下:
package com.ask.listener;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
//servlet属性值的改变;
public class Demo4 implements ServletContextAttributeListener {
//增加
public void attributeAdded(ServletContextAttributeEvent scab) {
System.out.println("attributeAdded");
System.out.println(scab.getName()+" "+scab.getValue());
}
//移除
public void attributeRemoved(ServletContextAttributeEvent scab) {
System.out.println("attributeRemoved");
System.out.println(scab.getName()+ " "+scab.getValue());
}
//取代;
public void attributeReplaced(ServletContextAttributeEvent scab) {
System.out.println("attributeReplaced");
System.out.println(scab.getName()+" "+scab.getValue());
}
}
2.新建一个index.jsp增加代码如下:
<body>
<%
application.setAttribute("name", "tom");
application.setAttribute("sal", "6000");
application.removeAttribute("sal");
%>
</body>
3.web.xml配置文件代码如下:
<!--demo4-->
<listener>
<listener-class>com.ask.listener.Demo4</listener-class>
</listener>
4.程序运行如下:
attributeReplaced
name tom
attributeAdded
sal 6000
attributeRemoved
sal 6000