監聽器

監聽器概述

1.ListenerServlet的監聽器

2.可以監聽客戶端的請求、服務端的操作等。

3.通過監聽器,可以自動激發一些操作,如監聽在線用戶數量,當增加一個HttpSession時,給在線人數加1

4.編寫監聽器需要實現相應的接口

5.編寫完成後在web.xml文件中配置一下,就可以起作用了

 

 

常用的監聽接口

應用事件模型提供了當ServletContextHttpSessionServletRequest狀態改變時的通知功能。可以編寫事件監聽類來響應這些狀態的改變,並且可以配置和部署應用事件和監聽類到Web應用。

對於ServletContext事件,當Web應用部署、卸載和對context增加屬性時,事件監聽類可以得到通知。下表列出了ServletContext的事件類型,對應特定事件的監聽類必須實現的接口和當事件發生時調用的方法。

事件類型

接口

方法

Servlet context被創建

javax.servlet.ServletContextListener

contextInitialized()

Servlet context被註銷

javax.servlet.ServletContextListener

contextDestroyed()

增加屬性

javax.servlet. ServletContextAttributeListener

attributeAdded()

刪除屬性

javax.servlet. ServletContextAttributeListener

attributeRemoved()

屬性被替換

javax.servlet. ServletContextAttributeListener

attributeReplaced()

 

對於HttpSession事件,當session激活、刪除或者session屬性的增加、刪除和替換時,事件監聽類得到通知。下表列出了HttpSession的事件類型,對應特定事件的監聽類必須實現的接口和當事件發生時調用的方法。

事件類型

接口

方法

session激活

javax.servlet.http. HttpSessionListener

sessionCreated()

session刪除

javax.servlet.http. HttpSessionListener

sessionDestroyed()

增加屬性

javax.servlet.http. HttpSessionAttributeListener

attributeAdded()

刪除屬性

javax.servlet.http. HttpSessionAttributeListener

attributeRemoved()

屬性被替換

javax.servlet.http. HttpSessionAttributeListener

attributeReplaced()

對於ServletRequest事件,當request初始化、銷燬或者request屬性的增加、刪除和替換時,事件監聽類得到通知。下表列出了ServletRequest的事件類型,對應特定事件的監聽類必須實現的接口和當事件發生時調用的方法。

事件類型

接口

方法

Request初始化

javax.servlet.ServletRequestListener

requestInitialized()

request銷燬

javax.servlet.ServletRequestListener

requestDestroyed()

增加屬性

javax.servlet.ServletRequestAttributeListener

attributeAdded()

刪除屬性

javax.servlet.ServletRequestAttributeListener

attributeRemoved()

屬性被替換

javax.servlet.ServletRequestAttributeListener

attributeReplaced()

 

配置事件監聽類的步驟:

<!--[if !supportLists]-->1.     <!--[endif]-->打開Web應用的部署描述文件web.xml

<!--[if !supportLists]-->2.     <!--[endif]-->增加事件聲明標記<listener>事件聲明定義的事件監聽類在事件發生時被調用。<listener>標記必須在<filter>標記和<servlet>標記之<!--[if !supportAnnotations]-->[番茄花園1]<!--[endif]--> 可以爲每種事件定義多個事件監聽類,Apusic應用服務器按照它們在部署描述文件聲明的順序調用。例如:

    <listener>

       <listener-class>

com.puckasoft.video.servlet.TestSessionListener

</listener-class>

</listener>編寫和部署監聽類。

編寫事件監聽類

編寫事件監聽類的步驟:

<!--[if !supportLists]-->1.     <!--[endif]-->創建新的類並實現事件對應的接口

<!--[if !supportLists]-->2.     <!--[endif]-->定義不接受參數、訪問屬性爲public的構造函數

<!--[if !supportLists]-->3.     <!--[endif]-->實現接口的方法

<!--[if !supportLists]-->4.     <!--[endif]-->編譯並拷貝到對應Web應用的WEB-INF/classes目錄下,或者打包成jar文件拷貝到WEB-INF/lib目錄下

監控session創建和銷燬的例子:

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;

import com.puckasoft.video.util.InfoWebService;

public class TestSessionListener implements HttpSessionListener <!--[if !supportAnnotations]-->[p2]<!--[endif]--> {

      public void sessionCreated(HttpSessionEvent arg0<!--[if !supportAnnotations]-->[p3]<!--[endif]--> ) {

           System.out.println("系統創建了一個HttpSession對象");

           InfoWebService.addSessionNum();

      }

 

      public void sessionDestroyed<!--[if !supportAnnotations]-->[p4]<!--[endif]--> (HttpSessionEvent arg0) {

           // TODO Auto-generated method stub

           System.out.println("系統銷燬了一個HttpSession對象");

           InfoWebService.decreaseSessionNum();

      }

 

}

 

其他知識點:

<!--[if !supportLists]-->1.    <!--[endif]-->屬性監聽器中可以通過event.getName(),得到所創建屬性的名稱,event.getValue()屬性得到所創建屬性的值。

<!--[if !supportLists]-->2.    <!--[endif]-->使用某個監聽器時,除了要在web.xml裏配置,還要在使用監聽器的頁面裏<%@ page language="java" import="java.util.*,com.suppervideo.listener.UserAttrListener,com.suppervideo.listener.UserListener" pageEncoding="gbk"%>引入

 

<!--[if !supportAnnotations]-->
<!--[endif]-->
<!--[if !supportAnnotations]-->
<!--[endif]--><!--[if !supportAnnotations]--><!--[endif]-->

 <!--[if !supportAnnotations]-->[番茄花園1]<!--[endif]-->注意<listener>的位置!!!

<!--[if !supportAnnotations]-->
<!--[endif]-->
<!--[if !supportAnnotations]-->
<!--[endif]--><!--[if !supportAnnotations]--><!--[endif]-->

 <!--[if !supportAnnotations]-->[p2]<!--[endif]-->監聽器也是實現的接口

<!--[if !supportAnnotations]-->
<!--[endif]-->
<!--[if !supportAnnotations]-->
<!--[endif]--><!--[if !supportAnnotations]--><!--[endif]-->

 <!--[if !supportAnnotations]-->[p3]<!--[endif]-->一般把這個變量改爲event

<!--[if !supportAnnotations]-->
<!--[endif]-->
<!--[if !supportAnnotations]-->
<!--[endif]--><!--[if !supportAnnotations]--><!--[endif]-->

 <!--[if !supportAnnotations]-->[p4]<!--[endif]-->Session失效時,調用;attributeRemoved是在session失效和屬性被remove

<!--[if !supportAnnotations]-->
<!--[endif]-->
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章