【Spring】1、Spring 中的監聽器 Listener


一、接口

1、EventListener

2、HttpSessionAttributeListener   繼承EventListener接口

     HttpSessionAttributeListener是“屬性改變監聽器”,當在會話對象中加入屬性、移除屬性或替換屬性時,相對應的attributeAdded()、attributeRemoved()與

     attributeReplaced()方法就會被調用,並分別傳入HttpSessionBindingEvent。

    

package javax.servlet.http;

import java.util.EventListener;

public interface HttpSessionAttributeListener extends EventListener {
public void attributeAdded ( HttpSessionBindingEvent se );
public void attributeRemoved ( HttpSessionBindingEvent se );
public void attributeReplaced ( HttpSessionBindingEvent se );

}

 

如果希望容器在部署應用程序時,實例化實現HttpSessionAttributeListener的類並注冊給應用程序,則同樣也是在實現類上標注@WebListener:
...
@WebListener()
public class HttpSessionAttrListener
                   implements HttpSessionAttributeListener {
...
}

另一個方式是在web.xml下進行設置:
    ...
    <listener>
        <listener-class>cc.openhome.HttpSessionAttrListener</listener-class>
    </listener>

3、HttpSessionListener    繼承EventListener接口

public interface HttpSessionListener extends EventListener {
    public void sessionCreated(HttpSessionEvent se);
    public void sessionDestroyed(HttpSessionEvent se);
}

   在HttpSession對象初始化或結束前,會分別調用sessionCreated()與session- Destroyed()方法,可以通過傳入的HttpSessionEvent,使用getSession()取得HttpSession,    以針對會話對象作出相對應的創建或結束處理操作。

4、HttpSessionBindingListener

HttpSessionBindingListener是“對象綁定監聽器”,如果有個即將加入HttpSession的屬性對象,希望在設置給HttpSession成為屬性或從HttpSession中移除時,可以收到HttpSession的通知,則可以讓該對象實現HttpSessionBindingListener接口。

package javax.servlet.http;
import java.util.EventListener;
public interface HttpSessionBindingListener extends EventListener {
    public void valueBound(HttpSessionBindingEvent event);
    public void valueUnbound(HttpSessionBindingEvent event);
}

當用戶輸入正確的名稱與密碼時,首先會以用戶名來創建User實例,而后加入HttpSession中作為屬性。希望User實例被加入成為HttpSession屬性時,可以自動從數據庫中加載用戶的其他數據,如地址、照片等,或是在日志中記錄用戶登錄的信息,可以讓User類實現HttpSessionBindingListener接口。

5. HttpSessionActivationListener

HttpSessionActivationListener是“對象遷移監聽器”,其定義了兩個方法sessionWillPassivate()與sessionDidActivate()。很多情況下,幾乎不會使用到HttpSessionActivationListener。在使用到分布式環境時,應用程序的對象可能分散在多個JVM中。當HttpSession要從一個JVM遷移至另一個JVM時,必須先在原本的JVM上序列化(Serialize)所有的屬性對象,在這之前若屬性對象有實現HttpSessionActivationListener,就會調用sessionWillPassivate()方法,而 HttpSession遷移至另一個JVM后,就會對所有屬性對象作反序列化,此時會調用sessionDidActivate()方法。

要可以序列化的對象必須實現Serializable接口。如果HttpSession屬性對象中有些類成員無法作序列化,則可以在sessionWillPassivate()方法中做些替代處理來保存該成員狀態,而在sessionDidActivate()方法中做些恢復該成員狀態的動作。

 

概述:

Servlet監聽器用於監聽一些重要事件的發生,監聽器對象可以在事情發生前、發生后可以做一些必要的處理。

接口:

目前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

有的listener可用於統計網站在線人數及訪問量。 如下:

服務器啟動時(實現ServletContextListener監聽器contextInitialized方法),讀取數據庫,並將其用一個計數變量保存在application范圍內

session創建時(實現HttpSessionListener監聽器sessionCreated方法),讀取計數變量加1並重新保存

服務器關閉時(實現ServletContextListener監聽器contextDestroyed方法),更新數據庫


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM