Java監聽器Listener使用說明

來源:互聯網
上載者:User

1、什麼是Java監聽器

監聽器也叫Listener,是Servlet的監聽器,它可以監聽用戶端的請求、服務端的操作等。通過監聽器,可以自動激發一些操作,比如監聽線上的使用者的數量。

 

2、Listener介面分類

1.1> ServletContextListener監聽ServletContext對象

1.2> ServletContextAttributeListener監聽對ServletContext屬性的操作,比如增加、刪除、修改

  

2.1> HttpSessionListener監聽Session對象

2.2> HttpSessionActivationListener監聽HTTP會話的active和passivate情況,passivate是指非活動的session被寫入持久裝置(比如硬碟),active相反。

2.3> HttpSessionAttributeListener監聽Session中的屬性操作

 

3.1> ServletRequestListener監聽Request對象

3.2> ServletRequestAttributeListener監聽Requset中的屬性操作

 

3、Java代碼

   1.1> ServletContextListener

 

contextInitialized(ServletContextEvent) 初始化時調用

contextDestoryed(ServletContextEvent) 銷毀時調用,即當伺服器重新載入時調用

 

   2.1>HttpSessionListener

 


sessionCreated(HttpSessionEvent) 初始化時調用

sessionDestoryed(HttpSessionEvent) 銷毀時調用,即當使用者登出時調用

 

   3.1>ServletRequestListener

 

requestinitialized(ServletRequestEvent) 對實現用戶端的請求進行監聽

requestDestoryed(ServletRequestEvent) 對銷毀用戶端進行監聽,即當執行request.removeAttribute("XXX")時調用

 

 

4、Demo

1>建立Java類,實現對應的介面

 

 

package com.lacom;

 

import javax.servlet.http.HttpSessionEvent;

import javax.servlet.http.HttpSessionListener;

 

public class TestSessionListener implements HttpSessionListener {

 

@Override

public void sessionCreated(HttpSessionEvent event) {

// session 建立時執行的操作

// 例如:線上人數+1

 

}

 

@Override

public void sessionDestroyed(HttpSessionEvent event) {

// session 銷毀時執行的操作

// 例如:線上人數-1

 

}

 

}

 

2>在web.xml中配置

 

 

<?xml version="1.0" encoding="UTF-8"?>

<web-appweb-appversion="2.5" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<listener>

<listener-class>com.lacom.TestSessionListener</listener-class>

</listener>

</web-app>

 

5、Java項目中用到的spring監聽器

ContextLoaderListener

作用:

啟動Web容器時,自動裝配ApplicationContext的配置資訊。

用法:

第一種:直接將applicationContext.xml(spring的設定檔)放到/WEB-INF下,只在web.xml中聲明一個listener

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

第二種:將之放到classpath下,但是此時要在web.xml中加入<context-param>,用它來指明你的applicationContext.xml的位置以供web容器來載入。如果有多個xml檔案,可以寫在一起並用“,”號分隔。例如近鐵項目中的配置:

 

 

 

Log4jConfigListener

好處:
   1. 動態改變記錄層級和策略,不需要重啟Web應用,如《Effective Enterprise Java》所說。
   2. 把log檔案定在 /WEB-INF/logs/ 而不需要寫絕對路徑。
因為 系統把web目錄的路徑壓入一個叫webapp.root的系統變數。這樣寫log檔案路徑時不用寫絕對路徑了.
log4j.appender.logfile.File=${webapp.root}/WEB-INF/logs/myfuse.log
   3. 可以把log4j.properties和其他properties一起放在/WEB-INF/ ,而不是Class-Path。
   4.log4jRefreshInterval為60000表示 開一條watchdog線程每60秒掃描一下設定檔的變化;

用法:
   在web.xml 添加
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>WEB-INF/log4j.properties</param-value>
    </context-param>

    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>60000</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.