WEB容器监听器详解 ServletContextListener



WEB容器监听器ServletContextListener主要用来监听容器启动和 销毁的时候需要做一些操作,就可以使用这个监听器来做。

ServletContextListener在spring启动前启动。


我们实现一个简单的监听器,需要继承接口ServletContextListener:

  1.  * 一个测试的监听器例子  
  2.  * @author zhuli  
  3.  * @date 2014-7-26  
  4.  */  
  5. public class TestContextLister implements ServletContextListener {  
  6.   
  7.     @Override  
  8.     public void contextInitialized(ServletContextEvent sce) {  
  9.         System.out.println("==============================容器装载");  
  10.   
  11.     }  
  12.   
  13.     @Override  
  14.     public void contextDestroyed(ServletContextEvent sce) {  
  15.         System.out.println("==============================容器销毁");  
  16.     }  
  17.   
  18. }  
 * 一个测试的监听器例子
 * @author zhuli
 * @date 2014-7-26
 */
public class TestContextLister implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("==============================容器装载");

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("==============================容器销毁");
    }

}

ServletContextListener 实现两个接口,一个是容器启动的时候,一个是容器销毁的时候:

  1. public interface ServletContextListener extends EventListener {  
  2.     /** 
  3.      ** Notification that the web application initialization 
  4.      ** process is starting. 
  5.      ** All ServletContextListeners are notified of context 
  6.      ** initialization before any filter or servlet in the web 
  7.      ** application is initialized. 
  8.      */  
  9.   
  10.     public void contextInitialized ( ServletContextEvent sce );  
  11.   
  12.     /** 
  13.      ** Notification that the servlet context is about to be shut down. 
  14.      ** All servlets and filters have been destroy()ed before any 
  15.      ** ServletContextListeners are notified of context 
  16.      ** destruction. 
  17.      */  
  18.     public void contextDestroyed ( ServletContextEvent sce );  
  19. }  
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.
	 */

    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.
	 */
    public void contextDestroyed ( ServletContextEvent sce );
}

在web.xml中的配置:

  1. <listener>  
  2.     <listener-class>com.xxx.controller.web.TestContextLister</listener-class>  
  3. </listener>  
	<listener>
		<listener-class>com.xxx.controller.web.TestContextLister</listener-class>
	</listener>

容器启动后,会在容器启动的日志中看到:

  1. ==============================容器装载  
  2. 2014-07-26 08:54:01.302:INFO:/:Initializing Spring FrameworkServlet 'apiServlet'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值