Servlet Listener之ServletContextListener用法

本文旨在解釋JavaEE中的ServletContextListener接口及用法。

1.何時(shí)需要使用ServletContextListener?

通常我們可能有這樣的需求:即在web 應(yīng)用啟動(dòng)之前運(yùn)行一些代碼。例如:我們可能需要?jiǎng)?chuàng)建一個(gè)數(shù)據(jù)庫連接以便web應(yīng)用在任何時(shí)候都能使用它執(zhí)行一些操作,并且當(dāng)web應(yīng)用關(guān)閉的時(shí)候能夠關(guān)閉數(shù)據(jù)庫連接。

2.如何實(shí)現(xiàn)這個(gè)需求?

Java EE規(guī)范提供了一個(gè)叫ServletContextListener的接口,這個(gè)接口可以實(shí)現(xiàn)我們的需求。ServletContextListener監(jiān)聽servlet context的生命周期事件。當(dāng)這個(gè)listener關(guān)聯(lián)的web應(yīng)用啟動(dòng)和關(guān)閉的時(shí)候,這個(gè)接口會(huì)收到通知。下面是javadoc對(duì)這個(gè)接口的說明:

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.

如果想要監(jiān)聽web應(yīng)用的啟動(dòng),可以使用contextInitialized(ServletContextEvent event)方法。

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.

如果要監(jiān)聽web應(yīng)用的停止(關(guān)閉),用contextDestroyed(ServletCOntextEvent event)方法。

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.

如下創(chuàng)建一個(gè)監(jiān)聽器類:

package com.cruise;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyServletContextListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        System.out.println("context initialized");
    }

    public void contextDestroyed(ServletContextEvent event) {
        System.out.println("context destroyed");
    }

}

接下來在web.xml文件中配置listener

</web-app ...>
  <listener>
    <listener-class>com.thejavageek.MyServletContextListener</listener-class>
  </listener>
</web-app>

配置完成后,部署應(yīng)用到tomcat服務(wù)器并啟動(dòng)tomcat,將會(huì)看到如下的日志。

INFO: Starting service Catalina
Oct 24, 2015 10:52:04 AM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.35
context initialized
Oct 24, 2015 10:52:04 AM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Oct 24, 2015 10:52:04 AM org.apache.jk.common.ChannelSocket init
  1. 繼承thread
public class ThreadListener extends Thread implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        super.start();
    }

    public void contextDestroyed(ServletContextEvent event) {
        super.stop();
    }

    @override
    public void run(){
        
    }
    

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文包括:1、Listener簡介2、Servlet監(jiān)聽器3、監(jiān)聽三個(gè)域?qū)ο髣?chuàng)建和銷毀的事件監(jiān)聽器4、監(jiān)聽三個(gè)域?qū)?..
    廖少少閱讀 6,679評(píng)論 6 28
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,285評(píng)論 6 342
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,688評(píng)論 19 139
  • 從三月份找實(shí)習(xí)到現(xiàn)在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂視家的研發(fā)崗...
    時(shí)芥藍(lán)閱讀 42,872評(píng)論 11 349
  • 喜歡一個(gè)人多苦啊 1 看孟京輝的《空中花園謀殺案》。 男主說: 你知道流浪貓和流浪狗的區(qū)別嗎? 流浪狗是被人拋棄的...
    沐森讀書閱讀 1,129評(píng)論 0 0

友情鏈接更多精彩內(nèi)容