IT・ブラザーズJavaWebチュートリアルリスナー3のバンド

モニターのドメインオブジェクトのプロパティには、リスナーを変更します

ドメインオブジェクト変更イベントリスナ属性は、イベントについてのServletContext、HttpSessionの、HttpServletRequestの変更リスナーの情報でこれらの3つのオブジェクトのプロパティを監視するために使用されます。

3つのリスナーインターフェースは、3つのインターフェイスで同じイベントServletContextAttributeListener、HttpSessionAttributeListenerに及びServletRequestAttributeListenerあり、3つのインターフェースが増加性の監視対象オブジェクトを扱う3つの方法で定義されている、イベントを取り外して交換しますまったく同じ名前の対応する方法が、受け付けたパラメータの異なるタイプ。

 

●  attributeAdded方法

監視対象オブジェクトにプロパティを追加する場合、Webコンテナは、イベントリスナーのattributeAdded方法は、この方法は、イベント型パラメータを受信し、リスナーがドメインオブジェクトは、このパラメータによって特性を増加させ、記憶されて得られる応答コール対象ドメインのプロパティ。

次のように各ドメインの完全な構文定義は、リスナーの属性:

公共のボイドattributeAdded(ServletContextAttributeEventイベント)

公共のボイドattributeAdded(HttpSessionBindingEventイベント)

公共ボイドattributeRemove(ServletRequestAttributeEventイベント)

●  attributeRemove方法

あなたはプロパティは、オブジェクトを聴取している削除すると、attributeRemoved方法Webコンテナが応答するイベントリスナーを呼び出します。

次のように各ドメインの完全な構文定義は、リスナーの属性:

公共ボイドはattributeRemoved(ServletContextAttributeEventイベント)

公共ボイドはattributeRemoved(HttpSessionBindingEventイベント)

公共ボイドはattributeRemoved(ServletRequestAttributeEventイベント)

 

●attributeReplaced方法

属性にリスナーのドメインオブジェクトを交換するときは●、Webコンテナの呼び出しイベントリスナーのattributeReplaced方法で応答します。

次のように各ドメインの完全な構文定義は、リスナーの属性:

公共ボイドはattributeReplaced(ServletContextAttributeEventイベント)

公共ボイドはattributeReplaced(HttpSessionBindingEventイベント)

公共ボイドはattributeReplaced(ServletRequestAttributeEventイベント)

ServletContextAttributeListenerリスナーの例:

●のServletContextドメインオブジェクトの属性値の変化を聞いて準備ServletContextAttributeListenerリスナー、次のように:

パッケージcom.xdl.listener。

輸入java.text.MessageFormatの。

輸入れるjavax.servlet.ServletContextAttributeEvent。

輸入javax.servlet.ServletContextAttributeListener。

/ **

* MyServletContextAttributeListenerクラスが実装

* ServletContextAttributeListenerインタフェース

*だから、ドメインオブジェクトのServletContextの性質の変化を聞くことができます

* /

パブリッククラスMyServletContextAttributeListener

    実装ServletContextAttributeListener {

    @オーバーライド

    公共のボイドattributeAdded(ServletContextAttributeEventイベント){

     文字列strの= MessageFormat.format(

         "追加されたのServletContextドメインオブジェクト属性:{0}、属性値は{1}"、

         event.getName()、event.getValue())。

     System.out.println(STR)。

    }

    @オーバーライド

    公共のボイドattributeRemoved(ServletContextAttributeEventイベント){

     文字列strの= MessageFormat.format(

         "削除のServletContextドメインオブジェクト属性:{0}、属性値は{1}"、

         event.getName()、event.getValue())。

     System.out.println(STR)。

    }

    公共のボイドattributeReplaced(ServletContextAttributeEventイベント){

     文字列strの= MessageFormat.format(

         "ドメインオブジェクトの属性を置き換えるのServletContext:値{0}"、event.getName())。

     System.out.println(STR)。

    }

}

web.xmlファイル内●登録済みリスナー

<?xml version = "1.0" エンコード= "UTF-8"?>

<web-appののxmlns:XSI = "http://www.w3.org/2001/XMLSchema-instance"

    xmlns = "http://xmlns.jcp.org/xml/ns/javaee"

    XSI:のschemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

    バージョン= "3.1">

    <聞き手>

        <説明> MyServletContextAttributeListener监听器</記述>

        <リスナクラス>

             com.xdl.listener.MyServletContextAttributeListener

         </リスナクラス>

    </リスナー>

</ web-app>の

●書き込みServletContextAttributeListenerTest.jspのテストページ

<%@ページ言語= "javaの" contentTypeの= "text / htmlの;のcharset = UTF-8"

    pageEncodingは= "UTF-8" %>

<!DOCTYPE HTML>

<HTML>

<ヘッド>

<メタ文字セット= "UTF-8">

<タイトル>バンドブラザーズのIT教育</ TITLE>

</ head>の

<身体>

    <%

        //アプリケーションドメインオブジェクトにプロパティを追加

        application.setAttribute( "名前"、 "30ドローの学生");

        //アプリケーションドメインのオブジェクトにname属性の値を置き換え

        application.setAttribute( "名前"、 "20ドローの学生");

        //プロパティの名前でアプリケーションドメインオブジェクトを削除

        application.removeAttribute( "名前");

    %>

</ BODY>

</ HTML>

Tomcatサーバを開き、図12に示した結果。

c8588c3829cb4252ba6f26dbe63718e2.png

コンソールで、図12 MyServletContextAttributeListener出力情報

 

実験の結果から見ることができ、のServletContextListenerリスナーはのServletContextドメインオブジェクト(アプリケーション)プロパティ値の変更の成功に耳を傾けます。

ServletRequestAttributeListenerとHttpSessionAttributeListenenrリスナーの例:

 

●準備リスナーは、HttpSessionのとHttpServletRequestのドメインオブジェクトの属性値の変化を聞いて、次のように:

パッケージcom.xdl.listener。

輸入java.text.MessageFormatの。

輸入javax.servlet.ServletRequestAttributeEvent。

輸入javax.servlet.ServletRequestAttributeListener。

輸入javax.servlet.http.HttpSessionAttributeListener。

輸入れるjavax.servlet.http.HttpSessionBindingEvent。

/ **

* MyRequestAndSessionAttributeListenerクラスが実装

* HttpSessionAttributeListenerに和ServletRequestAttributeListener接口

*だから、変化のHttpSessionとのServletRequestドメインオブジェクトのプロパティのために聞くことができます

* /

パブリッククラスMyRequestAndSessionAttributeListener

 実装HttpSessionAttributeListenerに、ServletRequestAttributeListener {

 @オーバーライド

 公共のボイドattributeAdded(ServletRequestAttributeEventイベント){

    文字列strの= MessageFormat.format(

         "のServletRequestドメインオブジェクトに追加された属性:{0}、属性値は{1}"、

         event.getName()、event.getValue())。

    System.out.println(STR)。

 }

 @オーバーライド

 公共のボイドattributeRemoved(ServletRequestAttributeEventイベント){

    文字列strの= MessageFormat.format(

         "削除のServletRequestドメインオブジェクト属性:{0}、属性値は{1}"、

         event.getName()、event.getValue())。

    System.out.println(STR)。

 }

 @オーバーライド

 公共のボイドattributeReplaced(ServletRequestAttributeEventイベント){

    文字列strの= MessageFormat.format(

         "ドメインオブジェクトの属性を置き換えるのServletRequest:値{0}"、event.getName())。

    System.out.println(STR)。

 }

 @オーバーライド

 公共のボイドattributeAdded(HttpSessionBindingEventイベント){

    文字列strの= MessageFormat.format(

         "追加されたHttpSessionのドメインオブジェクト属性:{0}、属性値は{1}"、

         event.getName()、event.getValue())。

    System.out.println(STR)。

 }

 @オーバーライド

 公共ボイドはattributeRemoved(HttpSessionBindingEventイベント){

    文字列strの= MessageFormat.format(

         "削除するHttpSessionのドメインオブジェクト属性:{0}、属性値は{1}"、

         event.getName()、event.getValue())。

    System.out.println(STR)。

 }

 @オーバーライド

 公共ボイドはattributeReplaced(HttpSessionBindingEventイベント){

    文字列strの= MessageFormat.format(

         "HttpSessionの交換ドメイン・オブジェクト属性:{0}である"、event.getName())。

    System.out.println(STR)。

 }

}

web.xmlファイル内●登録済みリスナー

<?xml version = "1.0" エンコード= "UTF-8"?>

<web-appののxmlns:XSI = "http://www.w3.org/2001/XMLSchema-instance"

    xmlns = "http://xmlns.jcp.org/xml/ns/javaee"

    XSI:のschemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

    バージョン= "3.1">

    <聞き手>

        <説明> MyRequestAndSessionAttributeListener监听器</記述>

        <リスナクラス>

             com.xdl.listener.MyRequestAndSessionAttributeリスナー

        </リスナクラス>

    </リスナー>

</ web-app>の

●書き込みRequestAndSessionAttributeListenerTest.jspのテストページ

<%@ページ言語= "javaの" contentTypeの= "text / htmlの;のcharset = UTF-8"

     pageEncodingは= "UTF-8" %>

<!DOCTYPE HTML>

<HTML>

<ヘッド>

<メタ文字セット= "UTF-8">

<タイトル>バンドブラザーズのIT教育</ TITLE>

</ head>の

<身体>

    <%

        //セッション内のドメインオブジェクトにプロパティを追加

        session.setAttribute( "名前"、 "30ドローの学生");

        // name属性ドメインのセッションオブジェクトの値を置き換え

        session.setAttribute( "名前"、 "20ドローの学生");

        //セッションオブジェクトのドメイン名のプロパティを削除

        session.removeAttribute( "名前");

        //ドメインオブジェクトを要求するプロパティを追加

        request.setAttribute( "名前"、 "30ドローの学生");

        //オブジェクト・リクエスト・フィールドのname属性の値を置き換え

        request.setAttribute( "名前"、 "20ドローの学生");

        //リクエストのドメインオブジェクトのname属性を削除します

        request.removeAttribute( "名前");

    %>

   </ BODY>

</ HTML>

Tomcatサーバを開き、図13に示した結果。

97fba8589c064481a5bb9b4d8597b1b4.png

コンソールで図13 MyRequestAndSessionAttributeListener出力情報

 

リスナーとServletRequestAttribute ListerenリスナーがドメインオブジェクトのHttpSessionとのHttpServletRequestドメインオブジェクトのプロパティ値の変更の成功に耳を傾けHttpSessionAttributeListeren、実行結果から見ることができます。

おすすめ

転載: www.cnblogs.com/itxdl/p/10961946.html