您的位置:首页 > 编程语言 > Java开发

javax.servlet.ServletContextListener

2016-07-16 12:55 411 查看

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:包含当前正在被初始化的 ServletContext 的 ServletContextEvent。


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:包含当前正在被销毁的 ServletContext 的 ServletContextEvent。


总结

ServletContextListener 是接收 ServletContext 生命周期通知事件的接口。

可以确保的是:contextInitialized 会在所有 filters 和 servlets 被初始化之前被调用。

可以确保的是:contextDestroyed 会在所有 filters 和 servlets 被销毁之后被调用。

contextInitialized 和 contextDestroyed 都提供 ServletContextEvent 参数,通过该参数可以访问到 ServletContext 上下文。

ServletContextListener 的 contextInitialized 方法会按照 ServletContextListener 声明顺序被 Web 容器调用,

而 contextDestroyed 则相反。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: