域對象中屬性變更及感知session綁定的事件監聽器


域對象中屬性的變更的時間監聽器就是用來監聽ServletContext,HttpSession,HttpServletRequest這三個對象中的屬性變更信息事件的監聽器。這三個監聽器接口分別是ServletContextAttributeListener, HttpSessionAttributeListener 和ServletRequestAttributeListener,這三個接口中都定義了三個方法來處理被監聽對象中的屬性的增加,刪除和替換的事件,同一個事件在這三個接口中對應的方法名稱完全相同,只是接受的參數類型不同。

1.1 attributeAdded方法

當向被監聽對象中增加一個屬性時,web容器就調用事件監聽器的attributeAdded方法進行響應,這個方法接收一個事件類型的參數,監聽器可以通過這個參數來獲得正在增加屬性的域對象和被保存到域中的屬性對象。

各個域屬性監聽器中的完整語法定義為:

public void attributeAdded(ServletContextAttributeEvent scae)
public void attributeReplaced(HttpSessionBindingEvent  hsbe)
public void attributeRmoved(ServletRequestAttributeEvent srae)

1.2 attributeRemoved方法

當刪除被監聽對象中的一個屬性時,web容器調用事件監聽器的attributeRemoved方法進行響應各個域屬性監聽器中的完整語法定義為:

public void attributeRemoved(ServletContextAttributeEvent scae)
public void attributeRemoved (HttpSessionBindingEvent  hsbe)
public void attributeRemoved (ServletRequestAttributeEvent srae)

1.3 attributeReplaced方法

當監聽器的域對象中的某個屬性被替換時,web容器調用事件監聽器的attributeReplaced方法進行響應各個域屬性監聽器中的完整語法定義為:

public void attributeReplaced(ServletContextAttributeEvent scae)
public void attributeReplaced (HttpSessionBindingEvent  hsbe)
public void attributeReplaced (ServletRequestAttributeEvent srae)

2.域對象中屬性變更的監聽器xxxAttributeListener

①監聽ServletContext,HttpSession,ServletRequest中添加屬性,替換屬性,移除屬性的事件監聽器。

②以ServletRequestArrributeListener為例,

//添加屬性時被調用
@Override public void attributeAdded(ServletRequestAttributeEvent arg0) { } //移除屬性時被調用
@Override public void attributeRemoved(ServletRequestAttributeEvent arg0) { } //替換屬性時被調用
@Override public void attributeReplaced(ServletRequestAttributeEvent arg0) { }

③這三個監聽器ServletContextAttributeListener, ServletRequestAttributeListener, HttpSessionAttributeListener用的都比較少

④使用API中的getName()和getValue()方法可以知道修改的屬性名和屬性值是什么,例如:

public void attributeAdded(ServletRequestAttributeEvent arg0) {
    System.out.println("向request中添加了一個屬性:"+arg0.getName()+":"+arg0.getValue());
}

3.感知session綁定的事件監聽器

保存在Session域中的對象可以有多種狀態:綁定(session.setAttribute("bean",Object))到Session中;從 Session域中解除(session.removeAttribute("bean"))綁定;隨Session對象持久化到一個存儲設備中;隨Session對象從一個存儲設備中恢復。

Servlet 規范中定義了兩個特殊的監聽器接口"HttpSessionBindingListener和HttpSessionActivationListener"來幫助JavaBean 對象了解自己在Session域中的這些狀態: ,實現這兩個接口的類不需要 web.xml 文件中進行注冊。

3.1 HttpSessionBindingListener

實現了HttpSessionBindingListener接口的JavaBean對象可以感知自己被綁定到Session中和 Session中刪除的事件。當對象被綁定到HttpSession對象中時,web服務器調用該對象的void valueBound(HttpSessionBindingEvent event)方法;當對象從HttpSession對象中解除綁定時,web服務器調用該對象的void valueUnbound(HttpSessionBindingEvent event)方法。例如:

Customer.java

package com.javaweb.Listener;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

public class Customer implements HttpSessionBindingListener {

	@Override
	public void valueBound(HttpSessionBindingEvent arg0) {
		System.out.println("綁定到session");
		Object value=arg0.getValue();
		System.out.println(this==value);
		System.out.println(arg0.getName());
	}

	@Override
	public void valueUnbound(HttpSessionBindingEvent arg0) {
		System.out.println("從session中解除綁定");
	}

}

HttpSessionBindingListenerTest.jsp

  <body>
  <h4>HttpSessionBindingListener</h4>
    <%
    Customer customer=new Customer();
    session.setAttribute("customer",customer);
    System.out.println("-------------------");
    session.removeAttribute("customer");
    %>
  </body>

 運行后輸出:

3.2 HttpSessionActivationListener

 實現了HttpSessionActivationListener接口的JavaBean對象可以感知自己被活化(反序列化)和鈍化(序列化)的事件。

  • 活化:從磁盤中讀取session對象
  • 鈍化:向磁盤中寫入session對象
  • session對象存儲在Tomcat服務器的work\Catalina\localhost\contextPath目錄下。SESSION.SER

java中的序列化和反序列化概念:

把對象轉換為字節序列的過程稱為對象的序列化,把字節序列恢復為對象的過程稱為對象的反序列化。對象的序列化主要有兩種用途:

1) 把對象的字節序列永久地保存到硬盤上,通常存放在一個文件中;
2) 在網絡上傳送對象的字節序列。

在很多應用中,需要對某些對象進行序列化,讓它們離開內存空間,入住物理硬盤,以便長期保存。比如最常見的是Web服務器中的Session對象,當有 10萬用戶並發訪問,就有可能出現10萬個Session對象,內存可能吃不消,於是Web容器就會把一些seesion先序列化到硬盤中,等要用了,再把保存在硬盤中的對象還原到內存中。

當兩個進程在進行遠程通信時,彼此可以發送各種類型的數據。無論是何種類型的數據,都會以二進制序列的形式在網絡上傳送。發送方需要把這個Java對象轉換為字節序列,才能在網絡上傳送;接收方則需要把字節序列再恢復為Java對象。

當綁定到HttpSession對象中的javabean對象將要隨HttpSession對象被鈍化(序列化)之前,web服務器調用該javabean對象的void sessionWillPassivate(HttpSessionEvent event) 方法。這樣javabean對象就可以知道自己將要和HttpSession對象一起被序列化(鈍化)到硬盤中。

當綁定到HttpSession對象中的javabean對象將要隨HttpSession對象被活化(反序列化)之后,web服務器調用該javabean對象的void sessionDidActive(HttpSessionEvent event)方法。這樣javabean對象就可以知道自己將要和 HttpSession對象一起被反序列化(活化)回到內存中。

 

監聽域對象自身創建和銷毀的監聽器:ServletContextListener,HttpSessionListener,ServletRequestListener

域對象中屬性變更的監聽器:ServletContextAttributeListener, HttpSessionAttributeListener 和ServletRequestAttributeListener

監聽綁定到HttpSession域中的某個對象的狀態的事件監聽器HttpSessionBindingListener,HttpSessionActivationListener

 

wx搜索“程序員考拉”,專注java領域,一個伴你成長的公眾號!

 


免責聲明!

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



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