javax.servlet.ServletContextListener

javax.servlet.ServletContextListener 接口

http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html

父接口

注意,此接口是在 java.util 包下。文档说明此接口:是所有事件监听器接口必须 extend 的标记接口。

package java.util;

/**
 * A tagging interface that all event listener interfaces must extend.
 * @since JDK1.1
 */
public interface EventListener {
}

接口原型

public interface ServletContextListener extends EventListener {
    public void contextInitialized(ServletContextEvent sce);
    public void contextDestroyed(ServletContextEvent sce);
}

接口文档

英文文档

Interface for receiving notification events about ServletContext lifecycle changes. 

In order to receive these notification events, the implementation class must be 
either declared in the deployment descriptor of the web application, 
annotated with javax.servlet.annotation.WebListener, 
or registered via one of the addListener methods defined on ServletContext. 

Implementations of this interface are invoked at their contextInitialized method in the order in which they have been declared, 
and at their contextDestroyed method in reverse order.

Since:
    Servlet 2.3
See Also:
    ServletContextEvent

中文翻译

(ServletContextListener 是)一个接收 ServletContext 生命周期通知事件的 Interface。

为了接收这些通知事件,实现类必须被 javax.servlet.annotation.WebListener 注解并声明在 Web 部署描述(文件)中,
或被 ServletContext 中定义的其中一个 addListener 方法注册。

此接口的实现类的 contextInitialized 方法会被按他们的声明顺序调用,contextDestroyed 方法则按相反的顺序。

Since:
    Servlet 2.3
See Also:
    ServletContextEvent

contextInitialized 方法文档

英文文档

public void contextInitialized(ServletContextEvent sce);

    Receives notification that the web application initialization process is starting. 

    All ServletContextListeners are notified of context initialization before any filters or servlets in the web application are initialized.

    Parameters:
        sce the ServletContextEvent containing the ServletContext that is being initialized

中文翻译

    接收 Web 应用初始化程序启动的通知。

    所有 ServletContextListeners 会在 Web 应用中所有的 filters or servlets 被初始化之前被通知。

    Parameters:
        sce:包含当前正在被初始化的 ServletContextServletContextEvent

contextDestroyed 方法文档

英文文档

public void contextDestroyed(ServletContextEvent sce);

    Receives notification that the ServletContext is about to be shut down. 

    All servlets and filters will have been destroyed before any ServletContextListeners are notified of context destruction.

    Parameters:
        sce the ServletContextEvent containing the ServletContext that is being destroyed

中文翻译

    接收 Web 应用初始化程序将被关闭的通知。

    所有 ServletContextListeners 会在所有 servlets and filters 被销毁之后,被通知上下文销毁。

    Parameters:
        sce:包含当前正在被销毁的 ServletContextServletContextEvent

总结

  1. ServletContextListener 是接收 ServletContext 生命周期通知事件的接口。
  2. 可以确保的是:contextInitialized 会在所有 filters 和 servlets 被初始化之前被调用。
  3. 可以确保的是:contextDestroyed 会在所有 filters 和 servlets 被销毁之后被调用。
  4. contextInitialized 和 contextDestroyed 都提供 ServletContextEvent 参数,通过该参数可以访问到 ServletContext 上下文。
  5. ServletContextListener 的 contextInitialized 方法会按照 ServletContextListener 声明顺序被 Web 容器调用,
    而 contextDestroyed 则相反。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值