<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    java學(xué)習(xí)

    java學(xué)習(xí)

     

    servlet各種監(jiān)聽器

    目前Servlet2.4和JSP2.0總共有8個監(jiān)聽器接口和6個Event類,其中 HttpSessionAttributeListener與HttpSessionBindingListener皆使用 HttpSessionBindingEvent;HttpSessionListener和 HttpSessionActivationListener則都使用HttpSessionEvent;其余Listener對應(yīng)的Event如下所 示:

    Listener接口

    Event

    ServletContextListener

    ServletContextEvent

    ServletContextAttributeListener

    ServletContextAttributeEvent

    HttpSessionListener

    HttpSessionEvent

    HttpSessionActivationListener

    HttpSessionAttributeListener

    HttpSessionBindingEvent

    HttpSessionBindingListener

    ServletRequestListener

    ServletRequestEvent

    ServletRequestAttributeListener

    ServletRequestAttributeEvent


    分別介紹:
    一 ServletContext相關(guān)監(jiān)聽接口
    補充知識:
    通過ServletContext 的實例可以存取應(yīng)用程序的全局對象以及初始化階段的變量。
    在JSP文件中,application 是 ServletContext 的實例,由JSP容器默認(rèn)創(chuàng)建。Servlet 中調(diào)用 getServletContext()方法得到 ServletContext 的實例。
    注意:
    全局對象即Application范圍對象,初始化階段的變量指在web.xml中,經(jīng)由<context-param>元素所設(shè)定的變量,它的范圍也是Application范圍,例如:

    <context-param>
    <param-name>Name</param-name>
    <param-value>browser</param-value>
    </context-param>
    當(dāng)容器啟動時,會建立一個Application范圍的對象,若要在JSP網(wǎng)頁中取得此變量時:
    String name = (String)application.getInitParameter("Name");
    或者使用EL時:
    ${initPara.name}
    若是在Servlet中,取得Name的值方法:
    String name = (String)ServletContext.getInitParameter("Name");


    1.ServletContextListener:
    用于監(jiān)聽WEB 應(yīng)用啟動和銷毀的事件,監(jiān)聽器類需要實現(xiàn)javax.servlet.ServletContextListener 接口。
    ServletContextListener 是 ServletContext 的監(jiān)聽者,如果 ServletContext 發(fā)生變化,如服務(wù)器啟動時 ServletContext 被創(chuàng)建,服務(wù)器關(guān)閉時 ServletContext 將要被銷毀。

    ServletContextListener接口的方法:
    void contextInitialized(ServletContextEvent sce)
    通知正在接受的對象,應(yīng)用程序已經(jīng)被加載及初始化。
    void contextDestroyed(ServletContextEvent sce)
    通知正在接受的對象,應(yīng)用程序已經(jīng)被載出。

    ServletContextEvent中的方法:
    ServletContext getServletContext()
    取得ServletContext對象


    2.ServletContextAttributeListener:用于監(jiān)聽WEB應(yīng)用屬性改變的事件,包括:增加屬性、刪除屬性、修改屬性,監(jiān)聽器類需要實現(xiàn)javax.servlet.ServletContextAttributeListener接口。

    ServletContextAttributeListener接口方法:
    void attributeAdded(ServletContextAttributeEvent scab)
    若有對象加入Application的范圍,通知正在收聽的對象
    void attributeRemoved(ServletContextAttributeEvent scab)
    若有對象從Application的范圍移除,通知正在收聽的對象
    void attributeReplaced(ServletContextAttributeEvent scab)
    若在Application的范圍中,有對象取代另一個對象時,通知正在收聽的對象


    ServletContextAttributeEvent中的方法:
    java.lang.String getName()
    回傳屬性的名稱
    java.lang.Object getValue()
    回傳屬性的值

    二、HttpSession相關(guān)監(jiān)聽接口
    1.HttpSessionBindingListener接口
    注意:HttpSessionBindingListener接口是唯一不需要再web.xml中設(shè)定的Listener

    當(dāng)我們的類實現(xiàn)了HttpSessionBindingListener接口后,只要對象加入Session范圍 (即調(diào)用HttpSession對象的setAttribute方法的時候)或從Session范圍中移出(即調(diào)用HttpSession對象的 removeAttribute方法的時候或Session Time out的時候)時,容器分別會自動調(diào)用下列兩個方法:
    void valueBound(HttpSessionBindingEvent event)
    void valueUnbound(HttpSessionBindingEvent event)

    思考:如何實現(xiàn)記錄網(wǎng)站的客戶登錄日志, 統(tǒng)計在線人數(shù)?

    2.HttpSessionAttributeListener接口
    HttpSessionAttributeListener監(jiān)聽HttpSession中的屬性的操作。
    當(dāng)在Session增加一個屬性時,激發(fā)attributeAdded(HttpSessionBindingEvent se) 方法;當(dāng)在Session刪除一個屬性時,激發(fā)attributeRemoved(HttpSessionBindingEvent se)方法;當(dāng)在Session屬性被重新設(shè)置時,激發(fā)attributeReplaced(HttpSessionBindingEvent se) 方法。這和ServletContextAttributeListener比較類似。

    3.HttpSessionListener接口
    HttpSessionListener監(jiān)聽HttpSession的操作。當(dāng)創(chuàng)建一個Session時,激發(fā)session Created(HttpSessionEvent se)方法;當(dāng)銷毀一個Session時,激發(fā)sessionDestroyed (HttpSessionEvent se)方法。

    4.HttpSessionActivationListener接口
    主要用于同一個Session轉(zhuǎn)移至不同的JVM的情形。

    四、ServletRequest監(jiān)聽接口
    1.ServletRequestListener接口
    和ServletContextListener接口類似的,這里由ServletContext改為ServletRequest
    2.ServletRequestAttributeListener接口
    和ServletContextListener接口類似的,這里由ServletContext改為ServletRequest

    posted on 2012-12-28 15:12 楊軍威 閱讀(481) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲欧美日韩一区二区三区在线| 成人免费看片又大又黄| 亚洲国产综合人成综合网站| 亚洲欧洲国产综合AV无码久久| 成年男女男精品免费视频网站| 中文字幕亚洲精品无码| 在线观看免费成人| 欧美日韩亚洲精品| 国产精品深夜福利免费观看| 国产亚洲精品美女久久久久| 亚洲第一区精品观看| 丰满妇女做a级毛片免费观看| 亚洲国产成人VA在线观看| 成人午夜影视全部免费看| 亚洲精品无码久久毛片| 精品久久久久久国产免费了| 不卡一卡二卡三亚洲| 久久久久国产免费| 亚洲欧洲日本国产| 成人黄18免费视频| 黄页网址大全免费观看12网站| 亚洲午夜精品久久久久久浪潮| 美女网站在线观看视频免费的 | 91九色老熟女免费资源站| 亚洲国产日韩在线| 免费无码又爽又刺激高潮| 一个人免费观看视频在线中文| 亚洲av永久无码精品漫画 | 苍井空亚洲精品AA片在线播放 | 巨胸狂喷奶水视频www网站免费| 亚洲国产精品SSS在线观看AV| 日本免费一区二区三区| 亚洲欧美日韩中文字幕一区二区三区| 亚洲午夜福利精品久久| 午夜免费1000部| 美景之屋4在线未删减免费| 亚洲AV无码一区东京热| 在线观看免费污视频| 黄桃AV无码免费一区二区三区| 亚洲丝袜中文字幕| 久久亚洲色一区二区三区|