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

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

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

    本站不再更新,歡迎光臨 java開發技術網
    隨筆-230  評論-230  文章-8  trackbacks-0



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

    Listener接口

    Event

    ServletContextListener

    ServletContextEvent

    ServletContextAttributeListener

    ServletContextAttributeEvent

    HttpSessionListener

    HttpSessionEvent

    HttpSessionActivationListener

    HttpSessionAttributeListener

    HttpSessionBindingEvent

    HttpSessionBindingListener

    ServletRequestListener

    ServletRequestEvent

    ServletRequestAttributeListener

    ServletRequestAttributeEvent


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

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


    1.ServletContextListener:
    用于監聽WEB 應用啟動和銷毀的事件,監聽器類需要實現javax.servlet.ServletContextListener 接口。
    ServletContextListener 是 ServletContext 的監聽者,如果 ServletContext 發生變化,如服務器啟動時 ServletContext 被創建,服務器關閉時 ServletContext 將要被銷毀。

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

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


    2.ServletContextAttributeListener:用于監聽WEB應用屬性改變的事件,包括:增加屬性、刪除屬性、修改屬性,監聽器類需要實現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相關監聽接口
    1.HttpSessionBindingListener接口
    注意:HttpSessionBindingListener接口是唯一不需要再web.xml中設定的Listener

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

    思考:如何實現記錄網站的客戶登錄日志, 統計在線人數?

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

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

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

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


    posted on 2008-06-22 10:54 有貓相伴的日子 閱讀(728) 評論(0)  編輯  收藏 所屬分類: j2ee
    本站不再更新,歡迎光臨 java開發技術網
    主站蜘蛛池模板: 一级特黄特色的免费大片视频| 亚洲日韩精品无码专区加勒比☆| 国产精品黄页免费高清在线观看| 国产精品久久香蕉免费播放 | 亚洲av女电影网| 成人性生交大片免费看中文| 亚洲一区爱区精品无码| 亚洲精品偷拍视频免费观看| 亚洲人成影院在线观看| selaoban在线视频免费精品| 国产成人精品日本亚洲专区| 中文字幕免费观看视频| 亚洲精品成人片在线播放| 免费视频成人手机在线观看网址| 亚洲国产成人久久精品动漫| 99在线在线视频免费视频观看| 中文字幕亚洲色图| 毛片免费观看的视频在线| 亚洲AV日韩AV一区二区三曲| 亚洲国产精品人人做人人爽| 国产在线观看免费av站| 亚洲综合激情六月婷婷在线观看| 在线观看免费人成视频色9| 久久亚洲AV成人无码国产电影 | 中文在线免费看视频| 少妇中文字幕乱码亚洲影视| 免费人成网站在线观看10分钟| 亚洲成a人片在线不卡一二三区| 亚洲精品网站在线观看不卡无广告| 99麻豆久久久国产精品免费| 噜噜噜亚洲色成人网站∨| 午夜两性色视频免费网站| 一级毛片完整版免费播放一区| 亚洲精品亚洲人成人网| av大片在线无码免费| 国产成人综合亚洲绿色| 久久精品国产99精品国产亚洲性色| 一本岛高清v不卡免费一三区| 深夜特黄a级毛片免费播放| 国产成人A人亚洲精品无码| 色窝窝免费一区二区三区|