1. 程式人生 > 實用技巧 >JavaWeb學習之Listener&Filter

JavaWeb學習之Listener&Filter

Listener(監聽器)

> 監聽某個事件的發生,其內部機制就是介面回撥

一、介面回撥

* 需求: > A在執行迴圈,當迴圈到5的時候, 通知B。
> 事先先把一個物件傳遞給 A , 當A 執行到5的時候,通過這個物件,來呼叫B中的方法。 > 但是注意,不是直接傳遞B的例項,而是傳遞一個介面的例項過去。

二、Web監聽器

### 使用監聽器的步驟: 1. 定義一個類,實現介面   如:MyServletContextListenerimplements ServletContextListener 2. 註冊 | 配置監聽器   配置檔案路徑:WebContent/WEB-INF/web.xml
<listener>
<listener-class>實現監聽器介面的類的全名稱(xx.xxx.xxxx.MyServletContextListener)</listener-class>
</listener>

### 總共有8個 劃分成三種類型

Ⅰ、監聽三個作用域建立和銷燬

作用域有:

  pageContext --- 當前頁面

  request --- httpServletRequest

  session --- httpSession

  application--- ServletContext

1、ServletContextListener(application作用域):用於監聽ServletContext的建立和銷燬

  servletcontext建立: 啟動伺服器的時候

  servletContext銷燬:關閉伺服器. 從伺服器移除專案

2、ServletRequestListener(request作用域):用於監聽Request的建立和銷燬

  request建立:訪問伺服器上的任意資源都會有請求出現。

        訪問 html: 會
        訪問 jsp:會
        訪問 servlet : 會

  request銷燬:伺服器已經對這次請求作出了響應。

3、HttpSessionListener(Session作用域):用於監聽Request的建立和銷燬

  session的建立:只要呼叫getSession

        html:不會
        jsp:會 預設呼叫getSession();
        servlet::會(必須在程式碼中呼叫getSession())

  session的銷燬:超時(一般情況30分鐘)、伺服器非正常關閉

作用:

  ServletContextListener

    1. 完成自己想要的初始化工作

    2. 執行自定義任務排程。 執行某一個任務。

  HttpSessionListener

    統計線上人數

Ⅱ、 監聽三個作用域屬性狀態變更

### 可以監聽在作用域中值 新增 | 替換 | 移除的動作。

1、servletContext --- ServletContextAttributeListener

2、request --- ServletRequestAttributeListener

3、session --- HttpSessionAttributeListener

session.setAtrribute("name","zhangsan");//attributeAdded方法監聽

session.setAttribute("name","lisi");//attributeReplaced方法監聽

session.removedAttribute("name");//attributeRemoved方法監聽

Ⅲ、監聽httpSession裡面值的狀態變更

#### 這一類監聽器不用註冊

1、HttpSessionBindingListener:監聽物件與session 繫結和解除繫結 的動作

  ##讓javaBean 實現該介面即可

			@Override
			public void valueBound(HttpSessionBindingEvent event) {
				System.out.println("物件被繫結進來了");
			}
		
			@Override
			public void valueUnbound(HttpSessionBindingEvent event) {
				System.out.println("物件被解除繫結");
			}

  ##javaBean物件存放到session中

session.setAttribute(javaBean物件);
session.removedAttribute(javaBean物件);

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

###鈍化 (序列化):把記憶體中的資料 儲存到硬碟上

###活化 (反序列化):把硬碟中的資料讀取到記憶體中。

例項:

伺服器正常關閉,會使Session鈍化,伺服器啟動Session活化

①、JavaBean實現HttpSessionActivationListener和Serialzable(用於序列化和反序列化,不實現此介面就無法把硬碟資料讀到儲存中)

public class Bean02 implements HttpSessionActivationListener,Serialzable{
	private String name;
	
	public String getName(){
		return name;
	}
	
	public void setName(String name){
		this.name = name;
	}
	
	@Override
	public void sessionWillPassivate(HttpSessionEvent se){
		System.out.println("Session中的值(Bean02物件)被鈍化了...")
	}
	
	@Override
	public void sessionDidActivate(HttpSessionEvent se){
		System.out.println("Session中的值(Bean02物件)被活化了...")
	}
}

②、session儲存JavaBean物件

<%
Bean02 bean = new Bean02();
bean.setName("xiaoxiao");
session.setAttribute("sessionkey_bean",bean);
%>

③、獲取session中的資料

<%
sessionkey_bean.getName()
%>

可以通過正常關閉伺服器測試。

④、通過配置讓session在一定時間內鈍化

  ###在tomcat裡面 conf/context.xml 裡面配置:對所有的執行在這個伺服器的專案生效

  ###在conf/Catalina/localhost/context.xml 配置:對 localhost生效(localhost:8080)

  ### 在自己的web工程專案中的 META-INF/context.xml:只對當前的工程生效

  ####配置內容:

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

maxIdleSwap : 1分鐘不用就鈍化
directory : 鈍化後的那個檔案存放的目錄位置。(\tomcat\apache-tomcat-7.0.52\work\Catalina\localhost\專案名\xxx(directory的值))

Filter(過濾器)

> 其實就是對客戶端發出來的請求進行過濾。 瀏覽器發出, 然後伺服器派servlet處理。 在中間就可以過濾, 其實過濾器起到的是攔截的作用