java监听器详解_Java中ServletContextAttributeListener监听器详解

Java中ServletContextAttributeListener监听器详解

ServletContextAttributeListener是对Servlet上下文(ServletContext)增删改的属性进行监听。

方法

//增加属性

public void attributeAdded(ServletContextAttributeEvent scab);

//属性删除

public void attributeRemoved(ServletContextAttributeEvent scab);

//属性替换(第二次设置同一属性)

public void attributeRepalced(ServletContextAttributeEvent scab);

//ServletContextAttributeEvent事件:能取得设置属性的名称与内容

//得到属性名称

public String getName();

//取得属性的值

public Object getValue();

例子

//ServletContext域对象中属性的变更的事件监听器

package webtest.listener;

import java.text.MessageFormat;

import javax.servlet.ServletContextAttributeEvent;

import javax.servlet.ServletContextAttributeListener;

public class MyServletContextAttributeListener implements

ServletContextAttributeListener {

@Override

public void attributeAdded(ServletContextAttributeEvent scab) {

String str =MessageFormat.format(

"ServletContext域对象中添加了属性:{0},属性值是:{1}"

,scab.getName()

,scab.getValue());

System.out.println(str);

}

@Override

public void attributeRemoved(ServletContextAttributeEvent scab) {

String str =MessageFormat.format(

"ServletContext域对象中删除属性:{0},属性值是:{1}"

,scab.getName()

,scab.getValue());

System.out.println(str);

}

@Override

public void attributeReplaced(ServletContextAttributeEvent scab) {

String str =MessageFormat.format(

"ServletContext域对象中替换了属性:{0}的值"

,scab.getName());

System.out.println(str);

}

}

//web.xml配置监听器

webtest.listener.MyServletContextAttributeListener

//Test.jsp测试页面

//往application域对象中添加属性

application.setAttribute("name", "51gjie");

//替换application域对象中name属性的值

application.setAttribute("name", "51gjie.com");

//移除application域对象中name属性

application.removeAttribute("name");

%>

这样ServletContextListener监听器成功监听到了ServletContext域对象(application)中的属性值的变化情况。

版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值