zoukankan      html  css  js  c++  java
  • 监听器学习笔记

     

    k 2013年3月21日 18:24:15

    使用方法:

    1. 根据需要创建对应的接口实现类.

    2. 注册监听:注册监听方法:在web.xml中配置

    <listener>

    <listener-class>类的全路径</listener-class>

    </listener>

    这样监听器就可以工作了.WEB服务器在适当的时候触发调用接口中的方法

    解释:

    监听器就是一个实现特定接口的普通java程序,这个程序专门用于监听另一个java对象的方法调用或属性改变,当被监听对象发生上述事件后,监听器某个方法将立即被执行。

    wps_clip_image-4365

    Servlet监听器

    在Servlet规范中定义了多种类型的监听器,它们用于监听的事件源分别为 ServletContext, HttpSession 和 ServletRequest 这三个域对象。

    Servlet规范针对这三个对象上的操作,又把这多种类型的监听器划分为如下类型:

    l 监听三个域对象创建或初始化和销毁的事件监听器

    l 监听域对象中属性的增加或修改和删除的事件监听器

    监听ServletContext域对象创建和销毁

    ServletContextListener 接口用于监听 ServletContext 对象的创建和销毁事件。

    当 ServletContext 对象被创建时,激发contextInitialized (ServletContextEvent sce)方法

    当 ServletContext 对象被销毁时,激发contextDestroyed(ServletContextEvent sce)方法。

    提问,ServletContext域对象何时创建和销毁:

    创建:服务器启动针对每一个web应用创建ServletContext

    销毁:服务器关闭前先销毁代表每一个web应用的ServletContext

    编写 Servlet 监听器

    和编写其它事件监听器一样,编写servlet监听器也需要实现一个特定的接口,并针对相应动作覆盖接口中的相应方法。

    和其它事件监听器略有不同的是,servlet监听器的注册不是直接注册在事件源上,而是由web容器负责注册,开发人员只需在web.xml文件中使用<listener>标签配置好监听器,web容器就会自动把监听器注册到事件源中(即:通过反射机制)。

    一个 web.xml 文件中可以配置多个 Servlet 事件监听器,web 服务器按照它们在 web.xml 文件中的注册顺序来加载和注册这些 Serlvet 事件监听器。

    注册监听方法:在web.xml中配置

    <listener>

    <listener-class>类的全路径</listener-class>

    </listener>

    监听ServletRequest域对象创建和销毁

    ServletRequestListener 接口用于监听ServletRequest 对象的创建和销毁。

    Request 对象被创建时,监听器的requestInitialized方法将会被调用。

    Request对象被销毁时,监听器的requestDestroyed方法将会被调用。

    servletRequest域对象创建和销毁的时机:

    创建:用户每一次访问,都会创建一个

                  新reqeust内容

    销毁:当前访问结束,原request内容

                  就会销毁

    监听三个域对象属性变化

    Servlet规范定义了监听 ServletContext, HttpSession, ServletRequest 这三个对象中的属性变更信息事件的监听器。

    这三个监听器接口分别是ServletContextAttributeListener, HttpSessionAttributeListener ServletRequestAttributeListener

    这三个接口中都定义了三个方法来处理被监听对象中的属性的增加,删除和替换的事件,同一个事件在这三个接口中对应的方法名称完全相同,只是接受的参数类型不同。 

    attributeAdded 方法

    当向被监听器对象中增加一个属性时,web容器就调用事件监听器的 attributeAdded 方法进行操作,这个方法接受一个事件类型的参数,监听器可以通过这个参数来获得正在增加属性的域对象和被保存到域中的属性对象

    各个域属性监听器中的完整语法定义为:

    public void attributeAdded(ServletContextAttributeEvent scae) 

    public void attributeReplaced(HttpSessionBindingEvent  hsbe) 

    public void attributeRmoved(ServletRequestAttributeEvent srae)

    attributeRemoved 方法

    当删除被监听对象中的一个属性时,web 容器调用事件监听器的这个方法进行相应

    各个域属性监听器中的完整语法定义为:

    public void attributeRemoved(ServletContextAttributeEvent scae) 

    public void attributeRemoved (HttpSessionBindingEvent  hsbe) 

    public void attributeRemoved (ServletRequestAttributeEvent srae)

    attributeReplaced 方法

    当监听器的域对象中的某个属性被替换时,web容器调用事件监听器的这个方法进行操作

    各个域属性监听器中的完整语法定义为:

    public void attributeReplaced(ServletContextAttributeEvent scae) 

    public void attributeReplaced (HttpSessionBindingEvent  hsbe) 

    public void attributeReplaced (ServletRequestAttributeEvent srae)

    监听HttpSession域对象创建和销毁

    HttpSessionListener接口用于监听HttpSession的创建和销毁

    创建一个Session时,sessionCreated(HttpSessionEvent se) 方法将会被调用。

    销毁一个Session时,sessionDestroyed (HttpSessionEvent se) 方法将会被调用。销毁HttpSession时间不精确。

    Session域对象创建和销毁的时机创建:用户每一次访问时,服务器创建session

    销毁:

    如果用户的session 30分钟没有使用,服务器就会销毁session,我们在web.xml里面也可以配置session失效时间

    调用HttpSession.invalidate()方法

    attributeRemoved 方法

    当删除被监听对象中的一个属性时,web 容器调用事件监听器的这个方法进行相应

    各个域属性监听器中的完整语法定义为:

    public void attributeRemoved(ServletContextAttributeEvent scae) 

    public void attributeRemoved (HttpSessionBindingEvent  hsbe) 

    public void attributeRemoved (ServletRequestAttributeEvent srae)

    attributeReplaced 方法

    当监听器的域对象中的某个属性被替换时,web容器调用事件监听器的这个方法进行操作

    各个域属性监听器中的完整语法定义为:

    public void attributeReplaced(ServletContextAttributeEvent scae) 

    public void attributeReplaced (HttpSessionBindingEvent  hsbe) 

    public void attributeReplaced (ServletRequestAttributeEvent srae)

     

    wps_clip_image-5402

  • 相关阅读:
    Android Xmpp协议讲解
    IOS 教程以及基础知识
    android的快速开发框架集合
    Android项目快速开发框架探索(Mysql + OrmLite + Hessian + Sqlite)
    afinal logoAndroid的快速开发框架 afinal
    Android 快速开发框架:ThinkAndroid
    2020.12.19,函数式接口,函数式编程,常用函数式接口,Stream流
    2020.12.18 网络编程基础,网络编程三要素,TCP通信,Socket类,ServerSocket
    2020.12.16,Properties,Buffer,InputStreamReader
    2020.12.15 IO流,字节流,字符流,流异常处理
  • 原文地址:https://www.cnblogs.com/mxcy/p/3993864.html
Copyright © 2011-2022 走看看