Section3 - サーブレットコンテナモデル

ここではリスナーインターフェイスについて説明します。

リスナーの種類

リスナーのインターフェイスは以下のものがあります。
javax.servlet.
ServletContextListener
アプリケーションの作成/終了時のリスナー

void contextDestroyed(ServletContextEvent sce)
void contextInitialized(ServletContextEvent sce)
javax.servlet.
ServletContextAttributeListener
アプリケーションスコープのsetAttribute/getAttribute時 呼ばれた時のリスナー

void attributeAdded(ServletContextAttributeEvent scab)
void attributeRemoved(ServletContextAttributeEvent scab)
void attributeReplaced(ServletContextAttributeEvent scab)
javax.servlet.http.
HttpSessionListener
セッションの作成/終了時のリスナー

void sessionCreated(HttpSessionEvent se)
void sessionDestroyed(HttpSessionEvent se)
javax.servlet.http.
HttpSessionAttributeListener
セッションスコープのsetAttribute/getAttribute時のリスナー

void attributeAdded(HttpSessionBindingEvent se)
void attributeRemoved(HttpSessionBindingEvent se)
void attributeReplaced(HttpSessionBindingEvent se)
javax.servlet.http.
HttpSessionActivationListener
セッションの活性化/非活性化のリスナー

void sessionDidActivate(HttpSessionEvent se)
void sessionWillPassivate(HttpSessionEvent se)
javax.servlet.http.
HttpSessionBindingListener
オブジェクトをセッションに登録、削除した時のリスナー

void valueBound(HttpSessionBindingEvent event)
void valueUnbound(HttpSessionBindingEvent event)
APIリファレンスを見て、各リスナーのインターフェイスを確認しましょう。

web.xmlへの記述

すべてのリスナーで共通です。 リスナーを実装したクラスを登録します。 複数のリスナーを実装したクラスを登録することも可能です。

<listener>
  <listener-class>test.TestServletContextListener</listener-class>
<listener>

listener要素のDTD

<!ELEMENT listener (listener-class)>

アプリケーションスコープのパラメータ指定は 以下のように行います。 ここではアプリケーション共通で使う情報を指定します。 設定ファイルのパスの指定などにも利用されます。 サーブレット、リスナーなどから利用可能です。

<context-param>
  <param-name>config.path</param-name>
  <param-value>WEB-INF/hoge.config</param-value>
</context-param>

context-param要素のDTD

<!ELEMENT context-param (param-name, param-value, description?)>