Java監聽器listener的介紹


Java監聽器listener的介紹

listener

  • 能做什么

    當web中某些動作發生之后,服務器就調用listener中對應的方法。

  • 內部機制

    接口回調

Web監聽器

  • 步驟

    1. 創建需要的監聽器類,實現接口

    2. 注冊|配置(有些不需要注冊)監聽器。

      servlet4.0可以用反射實現@WebListener()

      servlet2.5是需要在web.xml中配置

      <listener>
      	<listener-class>com.itheima.listener.MyRequestListener</listener-class>
      </listener>
      

三個作用域的監聽器(需要注冊)

request  ---httpServletRequest
session  ---httpSession
aapplication  --- ServletContext
  1. 用途

    主要是這3個域的創建與銷毀時調用方法

    • ServletContextListener

    利用它來,在servletcontext創建的時候,

    1. 完成自己想要的初始化工作
    2. 執行自定義任務調度。 執行某一個任務。 Timer
    • HttpSessionListener

    統計在線人數

  2. ServletRequestListener

    • request創建:

    訪問服務器上的任意資源都會有請求出現。

    訪問 html: 會

    訪問 jsp: 會

    訪問 servlet: 會

    • request銷毀:

    服務器已經對這次請求作出了響應。

  3. ServletContextListener

    • servletcontext創建:

    啟動服務器的時候

    • servletContext銷毀:

    關閉服務器. 從服務器移除項目

  4. HttpSessionListener

    • session的創建

    只要調用 getSession

    html: 不會

    jsp: 會

    servlet:會 要寫getSession();

    • session的銷毀

    超時 30分鍾

    非正常關閉 銷毀

    正常關閉服務器(序列化)

監聽三個作用域屬性狀態變更(需要注冊)

​ servletContext --- ServletContextAttributeListener

​ request --- ServletRequestAttributeListener

​ session --- HttpSessionAttributeListener

  • 條件

    可以監聽在作用域中值 添加 | 替換 | 移除的動作。

    調用的相應的方法。

  • 用法(重載,只是3方法的參數不同,查API就行了)

    @Override
    public void attributeAdded(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute 
         is added to a session.
      */
    }
    
    @Override
    public void attributeRemoved(HttpSessionBindingEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
    }
    
    @Override
    public void attributeReplaced(HttpSessionBindingEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
    }
    

監聽httpSession里面存值的狀態變更(不用注冊)

  1. HttpSessionBindingListener

    監聽對象與session 綁定session.setAttribute("鍵名",對象)和解除綁定session.removeAttribute("鍵名")的動作

  • 作用

    JavaBean被綁定了和解綁時調用

  • 步驟

    讓JavaBean實現該接口

  1. HttpSessionActivationListener

    用於監聽現在session的值 是 鈍化 (序列化)還是活化 (反序列化)的動作

  • 作用

    鈍化 (序列化)將內存中的數據存到硬盤中

    活化 (反序列化)將硬盤的數據加載到內存中

    session中的值可能會很多, 並且我們有很長一段時間不使用這個內存中的值, 那么可以考慮把session的值可以存儲到硬盤上【鈍化】,等下一次在使用的時候,在從硬盤上提取出來。 【活化】

  • 步驟

  • 創建

    讓JavaBean實現該接口和Serializable接口

  • session中的值一段時間不用自動鈍化的配置

  • 1.在tomcat里面 conf/context.xml 里面配置

    對所有的運行在這個服務器的項目生效

    2.在conf/Catalina/localhost/context.xml 配置

    對 localhost生效。 localhost:8080

    3.在自己的web工程項目中的 META-INF/context.xml

    只對當前的工程生效。

    存儲地址 tomcat目錄\work\Catalina\localhost\ListenerDemo\itheima

    maxIdleSwap : 1分鍾不用就鈍化
    directory : 鈍化后的那個文件存放的目錄位置。

    <Context>
    	<Manager className="org.apache.catalina.session.PersistentManager" maxIdleSwap="1">
    		<Store className="org.apache.catalina.session.FileStore" directory="itheima"/>
    	</Manager>
    </Context>
    


免責聲明!

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



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