ServletContextAttributeListener, HttpSessionAttributeListener 和ServletRequestAttributeListener,这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同。
attributeAdded 方法:
attributeRemoved 方法
attributeReplaced 方法
<listener>
<listener-class>listener.MyServletContextAttributeListener</listener-class>
</listener>
jsp测试;
<%
//往application域对象中添加属性
application.setAttribute("name", "nick");
//替换application域对象中name属性的值
application.setAttribute("name", "gacl");
//移除application域对象中name属性
application.removeAttribute("name");
%>
ServletContextListener监听ServletContext域对象(application)中的属性值的变化情况。
同理对其他两个监听进行测试。
attributeAdded 方法:
1 public void attributeAdded(ServletContextAttributeEvent scae)
2 public void attributeReplaced(HttpSessionBindingEvent hsbe)
3 public void attributeRmoved(ServletRequestAttributeEvent srae)
attributeRemoved 方法
1 public void attributeRemoved(ServletContextAttributeEvent scae)
2 public void attributeRemoved (HttpSessionBindingEvent hsbe)
3 public void attributeRemoved (ServletRequestAttributeEvent srae)
attributeReplaced 方法
1 public void attributeReplaced(ServletContextAttributeEvent scae)
2 public void attributeReplaced (HttpSessionBindingEvent hsbe)
3 public void attributeReplaced (ServletRequestAttributeEvent srae)
<listener>
<listener-class>listener.MyServletContextAttributeListener</listener-class>
</listener>
public class MyServletContextAttributeListener implements ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent scab) {
String str =MessageFormat.format(
"ServletContext域对象中添加了属性:{},属性值是:{}"
,scab.getName()
,scab.getValue());
System.out.println(str);
}
@Override
public void attributeRemoved(ServletContextAttributeEvent scab) {
String str =MessageFormat.format(
"ServletContext域对象中删除属性:{},属性值是:{}"
,scab.getName()
,scab.getValue());
System.out.println(str);
}
@Override
public void attributeReplaced(ServletContextAttributeEvent scab) {
String str =MessageFormat.format(
"ServletContext域对象中替换了属性:{}的值"
,scab.getName());
System.out.println(str);
}
}
jsp测试;
<%
//往application域对象中添加属性
application.setAttribute("name", "nick");
//替换application域对象中name属性的值
application.setAttribute("name", "gacl");
//移除application域对象中name属性
application.removeAttribute("name");
%>
ServletContextListener监听ServletContext域对象(application)中的属性值的变化情况。
同理对其他两个监听进行测试。