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

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

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

    linlinyu

    linlinyu
    posts - 0, comments - 4, trackbacks - 0, articles - 15

    Servlet中的八大Listener

    Posted on 2007-11-23 09:49 linlin yu 閱讀(189) 評論(0)  編輯  收藏 所屬分類: servlet

     

    1. Servlet中的Listener和Event:

    在JSP 2.0/Servlet 2.4中,共有八個Listener接口,六個Event類別。
    ServletContextListener接口
    [接口方法] contextInitialized()與 contextDestroyed()
    [接收事件] ServletContextEvent
    [觸發場景] 在Container加載Web應用程序時(例如啟動 Container之后),會呼叫contextInitialized(),而當容器移除Web應用程序時,會呼叫contextDestroyed ()方法。

    ServletContextAttributeListener
    [接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
    [接收事件] ServletContextAttributeEvent
    [觸發場景] 若有對象加入為application(ServletContext)對象的屬性,則會呼叫attributeAdded(),同理在置換屬性與移除屬性時,會分別呼叫attributeReplaced()、attributeRemoved()。

    HttpSessionListener
    [接口方法] sessionCreated()與sessionDestroyed ()
    [接收事件] HttpSessionEvent
    [觸發場景] 在session (HttpSession)對象建立或被消滅時,會分別呼叫這兩個方法。

    HttpSessionAttributeListener
    [接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
    [接收事件] HttpSessionBindingEvent
    [觸發場景] 若有對象加入為session(HttpSession)對象的屬性,則會呼叫attributeAdded(),同理在置換屬性與移除屬性時,會分別呼叫attributeReplaced()、 attributeRemoved()。

    HttpSessionActivationListener
    [接口方法] sessionDidActivate()與 sessionWillPassivate()
    [接收事件] HttpSessionEvent
    [觸發場景] Activate與Passivate是用于置換對象的動作,當session對象為了資源利用或負載平衡等原因而必須暫時儲存至硬盤或其它儲存器時(透過對象序列化),所作的動作稱之為Passivate,而硬盤或儲存器上的session對象重新加載JVM時所采的動作稱之為Activate,所以容易理解的,sessionDidActivate()與 sessionWillPassivate()分別于Activeate后與將Passivate前呼叫。

    ServletRequestListener
    [接口方法] requestInitialized()與 requestDestroyed()
    [接收事件] RequestEvent
    [觸發場景] 在request(HttpServletRequest)對象建立或被消滅時,會分別呼叫這兩個方法。

    ServletRequestAttributeListener
    [接口方法] attributeAdded()、 attributeReplaced()、attributeRemoved()
    [接收事件] HttpSessionBindingEvent
    [觸發場景] 若有對象加入為request(HttpServletRequest)對象的屬性,則會呼叫attributeAdded(),同理在置換屬性與移除屬性時,會分別呼叫attributeReplaced()、 attributeRemoved()。

    HttpSessionBindingListener
    [接口方法] valueBound()與valueUnbound()
    [接收事件] HttpSessionBindingEvent
    [觸發場景] 實現HttpSessionBindingListener接口的類別,其實例如果被加入至session(HttpSession)對象的屬性中,則會呼叫 valueBound(),如果被從session(HttpSession)對象的屬性中移除,則會呼叫valueUnbound(),實現HttpSessionBindingListener接口的類別不需在web.xml中設定。

    2. 如何注冊Servlet中的事件 :
    實現上面這幾個接口的類別,除了HttpSessionBindingListener外,必須在web.xml中向容器注冊,容器才會在對應的事件發生時呼叫對應的類別,如:
     < listener > 
     < listener-class > demo.servlet.listener.CustomServletContextListener  

    3. Servlet事件的應用實例
    看到這里,你也許會有疑問: 了解這些 listener和event 有什么用呢?我平時開發沒有用到這些,一樣也能完成任務啊.

    不錯,在日常的開發中很少用到這些事件處理的方面,但是在某些情況下使用事件處理機制卻可以達到事半功倍的效果,例如下面兩個例子:

    4.Java類實例
    ==========
    //偵聽啟動和關閉
    import javax.servlet.ServletContextListener;
    import javax.servlet.*;

    public class TigerListen implements ServletContextListener {
     public void contextInitialized(ServletContextEvent sce)
     {
      System.out.print("Init") ;
     }
     public void contextDestroyed(ServletContextEvent sce)
     {
      System.out.print("Destroved") ;
     }
    }

    對應的web.xml是
    ============
    <!--l version="1.0" encoding="UTF-8-->
        TigerListen
      


    ============

    主站蜘蛛池模板: 中国亚洲呦女专区| 亚洲人成电影亚洲人成9999网| 亚洲一级毛片视频| 57pao国产成永久免费视频| 国产v亚洲v天堂无码网站| 特级做A爰片毛片免费看无码 | 无码一区二区三区AV免费| 亚洲日韩乱码久久久久久| 久久免费观看国产精品| 日韩精品亚洲人成在线观看| 日本免费大黄在线观看| 亚洲视频免费在线看| 精品久久久久国产免费| 亚洲熟妇无码AV| 又粗又大又长又爽免费视频| 特色特黄a毛片高清免费观看| 亚洲国产一区二区视频网站| 国产精品玖玖美女张开腿让男人桶爽免费看 | 18亚洲男同志videos网站| 91久久成人免费| 亚洲精品美女久久7777777| 四虎影库久免费视频| 一本久久A久久免费精品不卡| 亚洲乱亚洲乱妇无码麻豆| 69av免费观看| 亚洲色偷偷色噜噜狠狠99| 亚洲一级Av无码毛片久久精品| 久久er国产精品免费观看2| 亚洲国产中文在线视频| 亚洲av麻豆aⅴ无码电影| 可以免费观看的国产视频| 亚洲日本人成中文字幕| 免费二级毛片免费完整视频| 无人在线观看免费高清| 男人天堂2018亚洲男人天堂| 国产午夜亚洲精品国产成人小说| 日韩人妻一区二区三区免费| 亚洲国产成人久久一区二区三区| 国产AV无码专区亚洲AV手机麻豆| 成人免费黄色网址| 成人午夜影视全部免费看|