java属性监听_监听三个对象属性的变化_曾秋雷的博客-CSDN博客

## 监听三个对象的属性的变化

添加,替换,删除.

### ServletContextAttributeListener

web.xml


com.like.attr.MyServletContextAttrListener

public class MyServletContextAttrListener implements ServletContextAttributeListener

{

@Override

public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent)

{

System.out.println("ServletContext属性添加了 :" + servletContextAttributeEvent.getName() + "," + servletContextAttributeEvent.getValue());

}

@Override

public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent)

{

System.out.println("ServletContext属性移除了 :" + servletContextAttributeEvent.getName() + "," + servletContextAttributeEvent.getValue());

}

@Override

public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent)

{

System.out.println("ServletContext属性替换了 :" + servletContextAttributeEvent.getName() + "," + servletContextAttributeEvent.getValue());

}

}

创建add.jsp


application.setAttribute("name", "jack");

%>

创建update.jsp


application.setAttribute("name", "milan");

%>

创建remove.jsp


application.removeAttribute("name");

%>

访问三个页面触发不同监听器方法.

注意:第一次添加的时候会执行三次.


ServletContext属性添加了 :org.apache.jasper.runtime.JspApplicationContextImpl,org.apache.jasper.runtime.JspApplicationContextImpl@521dec7e

ServletContext属性添加了 :org.apache.jasper.compiler.ELInterpreter,org.apache.jasper.compiler.ELInterpreterFactory$DefaultELInterpreter@66ce0f61

ServletContext属性添加了 :name,jack //显示的是添加的值

ServletContext属性替换了 :name,jack //显示的是被替换的值

ServletContext属性移除了 :name,milan //显示的是被移除的值

### ServletRequestAttributeListener

同上

### ServletSessionAttributeListener

同上


原网址: 访问
创建于: 2022-11-17 22:11:26
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论