监听器

1.ServletContextListener:

    用于监听应用程序的启动与销毁的事件,监听类需要实现javax.servlet.ServletContextListener接口.


         ServletContextListenerServletContext的监听者,如果ServletContext发生变化,如服务器启动时ServletContext被创建,服务器关闭时 

         ServletContext被销毁.

         ServletContextListener 接口的方法:

                  >void contextInitialized(ServletContextEvent sce):通知正在接受的对象,应用程序已经被加载及初始化.

                  >void contextDestroyed(ServletContextEvent sce):通知正在接受的对象,应用程序已经被销毁.

ServletContextEvent中的方法:

                  >ServletContext getServletContext():取得ServletContext对象

     配置文件:

    <listener>

        <listener-class>com.lovo.MyServletContextListener</listener-class>

    </listener>

 

 

2.ServletContextAttributeListener:

    用于监听web应用属性的改变的事件,包括:增加属性,修改属性,删除属性.监听需要实现javax.servlet.ServletContextsAttributeListener:

 

    ServletContextsAttributeListener接口方法:

        >void attributeAdded(ServletContextAttributeEvent scae):若有属性对象加入Application的范围,通知正在收听的对象.

        >void AttributeRemoved(ServletContextAttributeEvent scae):若有属性对象从Application的范围移除,通知正在收听的对象.

        >void AttributeReplaced(ServletContextAttributeEvent scae):若在Application的范围中,有对象替代了另一个对象时,通知正在收听的对象.

    ServletContextEvent中的方法:

        >java.lang.String.getName():回传属性的名称

        >java.lang.Object.getValue():回传属性的值

 

    监听会话范围内的事件:

3.HttpSessionBindingListener接口:对象绑定

 

    注意:HttpSessionBindingListener接口是唯一不需要在web.xml中设定的Listener


    4.HttpSessionAttributeListener接口:不用注册到web.xml

    5.HttpSessionListener接口:在请求作用域范围内的生命周期事件用于管理整个request生命周期的状态和资源.

 

    方法:

        >void requestDestroyed(ServletRequestEvent e)’:当请求被销毁时被处理

        >void requestInitialized(ServletRequestEvent e)’:当请求被创建时被处理

 

6. ServletRequestAttributeListener接口方法:

    >void attributeAdded(ServletRequestAttributeEvent scae):若请求作用域添加一个属性的时候调用该方法;

    >void AttributeRemoved(ServletRequestAttributeEvent scae):当请求作用域删除一个属性时调用该方法

    >void AttributeReplaced(ServletRequestAttributeEvent scae):当请求作用域替换一个属性时候呀被调用

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值