Dev@Free

          zJun's Tech Weblog

          JSP/Servlet 中的事件處理

          寫過AWT或Swing程序的人一定對桌面程序的事件處理機制印象深刻:通過實現Listener接口的類可以在特定事件(Event)發生時,呼叫特定的方法來對事件進行響應。

          其實我們在編寫JSP/Servle程序時,也有類似的事件處理機制,所不同的是在JSP/Servlet中是在web.xml中注冊Listener,由Container在特定事件發生時呼叫特定的實現Listener的類。

          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 </ listener-class >
          ?
          </ listener >

          3. Servlet事件的應用實例

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

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

          利用HttpSessionListener實現網站在線人數統計功能

          使用ServletContextListener在服務器啟動和關閉時創建和關閉緩存

          posted on 2006-07-20 14:23 zJun's帛羅閣 閱讀(2499) 評論(4)  編輯  收藏

          評論

          # re: JSP/Servlet 中的事件處理 2006-10-28 16:45 y

          rt  回復  更多評論   

          # re: JSP/Servlet 中的事件處理 2006-11-14 11:50 小野貓

          demo.servlet.listener.CustomServletContextListener
          必須是一個class么?如果把這個類放到一個jsp里呢?  回復  更多評論   

          # re: JSP/Servlet 中的事件處理 2006-11-17 09:47 zjun

          @小野貓
          不是很明白你的意思,為什么要放到JSP里?  回復  更多評論   

          # re: JSP/Servlet 中的事件處理 2011-04-20 15:23 Hector

          剛學jsp的飄過  回復  更多評論   


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


          網站導航:
           

          導航

          <2006年7月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          統計

          常用鏈接

          留言簿(15)

          隨筆分類

          隨筆檔案

          相冊

          收藏夾

          博客

          文檔

          站點

          論壇

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 平湖市| 昌平区| 大新县| 广安市| 囊谦县| 昆山市| 柳林县| 突泉县| 临漳县| 岳阳县| 樟树市| 山阴县| 隆安县| 赤水市| 调兵山市| 虹口区| 鄂托克前旗| 沽源县| 仁怀市| 通州区| 桂东县| 错那县| 鄂托克前旗| 宜城市| 芒康县| 彰化县| 宜丰县| 鄄城县| 高要市| 灌阳县| 大关县| 莎车县| 印江| 瓦房店市| 阿拉善左旗| 沁源县| 内江市| 武城县| 寿宁县| 哈尔滨市| 隆尧县|