java listener详解_Java监听器Listener使用详解

监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动。监听器其实就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法立即被执行。

上述概念设计到3个名词概念:

1.事件源:即谁产生的事件

2.事件对象:即产生了什么事件

3.监听器:监听事件源的动作

由于事件源可以产生多个动作(即产生多个事件),而监听器中的每一个方法监听一个动作,故每个监听器中都有很多方法。

1.JavaWeb中的监听器

1.1概念

JavaWeb中的监听器是Servlet规范中定义的一种特殊类,它用于监听web应用程序中的ServletContext、HttpSession和 ServletRequest这三大域对象的创建、销毁事件以及监听这些域对象中的属性发生修改的事件。

1.2JavaWeb中监听器的分类

在Servlet规范中定义了多种类型的监听器(一共8个监听器),它们用于监听的事件源分别为ServletContext,HttpSession和ServletRequest这三个域对象。Servlet规范针对这三个对象上的操作,又把多种类型的监听器划分为三种类型:

1.域对象的生命周期监听:监听域对象自身的创建和销毁。这个监听器需要实现相应的监听器接口:ServletContextListener、HttpSessionListener、ServletRequestListener。

2.域对象的属性监听:监听域对象中属性的增加和删除。这个监听器需要实现的监听器接口为:ServletContextAttributeListener、HttpSessionAttributeListener、ServletRequestAttributeListener

3.感知监听(都与HttpSession域对象有关):监听绑定到HttpSession域中的某个JavaBean对象的状态的监听器。这个监听器需要实现的监听器接口:HttpSessionBindingListener、HttpSessionActiveationListener.

1.3第一类:域对象的生命周期监听

事件源为:三大域事件对象为:创建与销毁监听器为:实现了ServletContextListener、HttpSessionListener、ServletRequestListener这三个接口的监听器

1.3.1ServletContext的生命周期监听

public class AListener implements ServletContextListener{ //在项目启动时调用 public void contextInitialized(ServletContextEvent sce) { } //在项目关闭时调用 public void contextDestroyed(ServletContextEvent sce) { } }

在web.xml文件中对该监听器进行配置:

listener.AListener

1.3.2HttpSession的生命周期监听

代码同上述基本一致:

public class AListener implements HttpSessionListener{ //在会话产生时调用 public void sessionCreated(HttpSessionEvent sce) { } //在会话关闭时调用 public void sessionDestroyed(HttpSessionEvent sce) { } }

同样需要在web.xml文件中进行配置:

listener.AListener

1.3.3对各个监听器接口的方法中出现的类介绍

ServletContextEvent类:类中有一个方法getServletContext(),该方法返回ServletContext对象。

HttpSessionEvent类:类中有一个方法getSession(),该方法返回一个HttpSession对象。

ServletRequestEvent类:类中有两个方法,getServletContext()用于返回一个ServletContext对象,getServletRequest()用于返回一个ServletRequest对象。

1.4第二类:域对象的属性监听

事件源:三大域事件对象:属性的增加与删除监听器:实现了ServletContextAttributeListener、HttpSessionAttributeListener、ServletRequestAttributeListener接口的监听器

1.4.1ServletContext的属性监听

public class AListener implements ServletContextAttributeListener{ //给ServletContext对象添加属性时调用 public void attributeAdded(ServletcontextAttribute scab){ } //给ServletContext对象删除属性时调用 public void attributeRemoved(ServletContextAttributeEvent scab){ } //给ServletContext对象替换属性值时调用 public void attributeReplaced(ServletContextAttributeEvent scab){ } }

同样需要在web.xml文件中对AListener进行配置。

1.4.2HttpSession的属性监听

public class AListener implements HttpSessionAttributeListener{ //给HttpSession对象添加属性时调用 public void attributeAdded(HttpSessionAttribute scab){ } //给HttpSession对象删除属性时调用 public void attributeRemoved(HttpSessionAttributeEvent scab){ } //给HttpSession对象替换属性值时调用 public void attributeReplaced(HttpSessionAttributeEvent scab){ } }

同样需要在web.xml中对AListener进行配置。

1.4.3ServletRequest的属性监听

public class AListener implements ServletRequestAttributeListener{ //给ServletRequest对象添加属性时调用 public void attributeAdded(ServletRequestAttribute scab){ } //给ServletRequest对象删除属性时调用 public void attributeRemoved(ServletRequestAttributeEvent scab){ } //给ServletRequest对象替换属性值时调用 public void attributeReplaced(ServletRequestAttributeEvent scab){ } }

同样需要在web.xml中对AListener进行配置。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值