JavaWeb相关(三):监听器(Listener)

来源:互联网 发布:飞友网络 待遇 编辑:程序博客网 时间:2024/04/30 00:55
在web应用中,有时候需要在web程序关闭,启动时执行一些任务(如建立数据库链接的释放和建立)或者你想要监控Session的创建和销毁,或者你希望在ServletContext,HttpSession以及ServletRequest对象中的属性发生改变时得到通知,那么可以通过Servlet监听器实现目的。

1.监听器(Listener):

  • 它是一个接口,内容由我们来实现;
  • 它需要注册,例如注册在按钮上!
  • 监听器中的方法,会在特殊事件发生时被调用!

1.1监听器接口:

Servlet API定义了8个监听器接口,可用于监听三大域对象的声明周期事件,以及对这些对象的属性改变事件。
在JavaWeb中,事件源主要有三大域对象:(1)ServletContext;(2)HttpSession;(3)ServletRequest

    javaWeb中完成编写监听器:    (1)写一个监听器类:要求必须去实现某个监听器接口;    (2)注册,是在web.xml中配置来完成注册!

事件源:三大域!

1.1.1 ServletContext

•   生命周期监听:ServletContextListener,继承接口 javax.servlet.ServletContext.Listener,它有两个方法,一个在出生时调用,一个在死亡时调用;   (1)void contextInitialized(ServletContextEvent sce):创建Servletcontext时;   (2)void contextDestroyed(ServletContextEvent sce):销毁Servletcontext时;•   属性监听:ServletContextAttributeListener,继承接口javax.servlet.ServletContext.Attribute.Listener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。   (1)void attributeAdded(ServletContextAttributeEvent event):添加属性时;   (2)void attributeReplaced(ServletContextAttributeEvent event):替换属性时;   (3)void attributeRemoved(ServletContextAttributeEvent event):移除属性时;

代码示例:

 * ServletContextListener实现类 * contextDestroyed() -- 在ServletContext对象被销毁前调用 * contextInitialized() --  -- 在ServletContext对象被创建后调用 * ServletContextEvent -- 事件类对象 *     该类有getServletContext(),用来获取ServletContext对象,即获取事件源对象 */public class MyServletContextListener implements ServletContextListener {    public void contextDestroyed(ServletContextEvent evt) {        System.out.println("销毁ServletContext对象");    }    public void contextInitialized(ServletContextEvent evt) {        System.out.println("创建ServletContext对象");    }}在web.xml中配置:<listener><listener-class>cn.itcast.listener.MyServletContextListener</listener-class></listener>

1.1.2HttpSession

•   生命周期监听:HttpSessionListener,继承接口javax.servlet.http.HttpsessionListener,它有两个方法,一个在出生时调用,一个在死亡时调用;    (1)void sessionCreated(HttpSessionEvent se):创建session时    (2)void sessionDestroyed(HttpSessionEvent se):销毁session时•   属性监听:HttpSessioniAttributeListener,继承接口javax.servlet.http.HttpsessionAttributeListener它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。    (1)void attributeAdded(HttpSessionBindingEvent event):添加属性时;    (2)void attributeReplaced(HttpSessionBindingEvent event):替换属性时    (3)void attributeRemoved(HttpSessionBindingEvent event):移除属性时•   感知监听:HttpSessionBindingListener,实现接口:javax.servlet.http.HttpSessionBindingListener,两个方法:若想让一个对象绑定到session或者从session中被删除时得到通知,实现此接口。    (1)valueBound():对象绑定到session上时    (2)valueUnbound():对象从session上移除时•   感知监听:HttpSessionActionListener,实现接口:javax.servlet.http.HttpSessionActionListener,实现这个接口的对象,如果绑定到Session中,当Session钝化或者激活时,servlet容器将通知该对象。    (1)sessionDidActive:session钝化上时    (2)sessionWillPassivate:session激活上时

代码示例:

1.1.3 ServletRequest

•   生命周期监听:ServletRequestListener,它有两个方法,一个在出生时调用,一个在死亡时调用;   (1)void requestInitialized(ServletRequestEvent sre):创建request时   (2)void requestDestroyed(ServletRequestEvent sre):销毁request时•   属性监听:ServletRequestAttributeListener,它有三个方法,一个在添加属性时调用,一个在替换属性时调用,最后一个是在移除属性时调用。   (1)void attributeAdded(ServletRequestAttributeEvent srae):添加属性时   (2)void attributeReplaced(ServletRequestAttributeEvent srae):替换属性时   (3)void attributeRemoved(ServletRequestAttributeEvent srae):移除属性时
原创粉丝点击