监听器,过滤器,拦截器详解之一:监听器概要

监听器

监听器javax.servlet.Listener是servlet规范中定义的一种特殊类。
用于监听ServletContext、HttpSession和ServletRequest等域对象的创建和销毁事件。

常见监听器如下

  1. 应用上下文监听器:javax.servlet.ServletContextListener
  2. 应用上下文属性监听器:javax.servlet.ServletContextAttributeListener
  3. HttpSession监听器: HttpSessionListener
  4. HttpSession属性监听器: HttpSessionAttributeListener
  5. 请求监听器:ServletRequestListener
  6. 请求属性监听器:ServletRequestAttributeListener

应用上下文监听器

应用上下文监听器ServletContextListener,用于监听web应用全局对象ServletContext的创建和销毁。而其创建和销毁是在应用的启动与关闭时触发的。

在Spring中,ServletContexxt获取的方式有3种1

// 1、request获取servletContext
ServletContext servletContext = request.getServletContext();

// 2、使用ContextLoader
ServletContext servletContext = ContextLoader.getCurrentWebApplicationContext().getServletContext();

// 3、使用spring注入自动注入
@Autowired
private ServletContext servletContext;

监听事件有:

  1. contextInitialized,应用全局上下文初始化完毕通知事件(即应用启动时调用一次)。在servlet/filter初始化前触发此通知。
  2. contextDestroyed,应用全局上下将要销毁时通知事件(即应用关闭时调用一次),servlets/filter执行完毕destroy后才会触发此通知。

在事件源ServletContextEvent 中,通过ServletContext getServletContext()方法,可以获取到变更后的ServletContext实例。

ps:一般情况下,每个虚拟机的每个 web应用,只有一个servletContext实例。

ServletContextListener 接口源码

/**
 * Implementations of this interface receive notifications about changes to the
 * servlet context of the web application they are part of. To receive
 * notification events, the implementation class must be configured in the
 * deployment descriptor for the web application.
 *
 * @see ServletContextEvent
 * @since v 2.3
 */

public interface ServletContextListener extends EventListener {

    /**
     ** Notification that the web application initialization process is starting.
     * All ServletContextListeners are notified of context initialization before
     * any filter or servlet in the web application is initialized.
     * @param sce Information about the ServletContext that was initialized
     */
    public void contextInitialized(ServletContextEvent sce);

    /**
     ** Notification that the servlet context is about to be shut down. All
     * servlets and filters have been destroy()ed before any
     * ServletContextListeners are notified of context destruction.
     * @param sce Information about the ServletContext that was destroyed
     */
    public void contextDestroyed(ServletContextEvent sce);
}

应用上下文属性监听器

应用上下文属性监听器ServletRequestAttributeListener,在ServletContext的属性发生变化时触发相应事件通知。如ServletContext#setAttribute ,ServletContext#removeAttribute方法。所以只要能获取到ServlettContext实例,并对属性做出变更,便会触发此监听器相应事件。

监听方法如下:
1. attributeAdded:属性添加成功后调用,事件对象的值是新加对象
2. attributeRemoved:属性移除成功后调用,事件对象的值是移除对象
3. attributeReplaced:属性更新成功后调用,事件对象的值是更新前对象,即旧值

ServletRequestAttributeListener 接口源码

/**
 * Implementations of this interface receive notifications of changes to the
 * attribute list on the servlet context of a web application. To receive
 * notification events, the implementation class must be configured in the
 * deployment descriptor for the web application.
 *
 * @see ServletContextAttributeEvent
 * @since v 2.3
 */

public interface ServletContextAttributeListener extends EventListener {
    /**
     * Notification that a new attribute was added to the servlet context.
     * Called after the attribute is added.
     * @param scae Information about the new attribute
     */
    public void attributeAdded(ServletContextAttributeEvent scae);

    /**
     * Notification that an existing attribute has been removed from the servlet
     * context. Called after the attribute is removed.
     * @param scae Information about the removed attribute
     */
    public void attributeRemoved(ServletContextAttributeEvent scae);

    /**
     * Notification that an attribute on the servlet context has been replaced.
     * Called after the attribute is replaced.
     * @param scae Information about the replaced attribute
     */
    public void attributeReplaced(ServletContextAttributeEvent scae);
}

HttpSession监听器

监听HttpSession的创建与销毁。监听器如下:

  1. sessionCreated:session创建时触发此通知。同一会话,只会触发一次
  2. sessionDestroyed:session销毁时触发此通知。同一会话,只会触发一次

通过HttpSessionEventgetSession()可以获取到session对象实例

HttpSessionListener 接口源码

/**
 * Implementations of this interface are notified of changes to the list of
 * active sessions in a web application. To receive notification events, the
 * implementation class must be configured in the deployment descriptor for the
 * web application.
 *
 * @see HttpSessionEvent
 * @since v 2.3
 */
public interface HttpSessionListener extends EventListener {

    /**
     * Notification that a session was created.
     *
     * @param se
     *            the notification event
     */
    public void sessionCreated(HttpSessionEvent se);

    /**
     * Notification that a session is about to be invalidated.
     *
     * @param se
     *            the notification event
     */
    public void sessionDestroyed(HttpSessionEvent se);
}

未完待续…

HttpSessionAttributeListener 与 ServletContextAttributeListener雷同
ServletRequestAttributeListener 与 ServletContextAttributeListener雷同
ServletRequestListener 与 ServletContextListener雷同

小结

  1. ServletContextListener / HttpSessionListener / ServletRequestListener监听大都类似,都是对象实例的创建一销毁时触发通知。只是范围不一样。分别是全局应用范围/会话范围/请求范围
  2. ServletContextAttributeListener / HttpSessionAttributeListener / ServletRequestAttributeListener 则是在实例对应的属性发生变化时触发。一般是通过setAttribute触发新增与变更通知,removeAttribute触发移除通知。
后续

将对以上监听器的触发时机相关代码进行解析。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值