【JAVAEE——Listener】

目錄

一:什麼是監聽器?

二 :監聽器有哪些

 2.1按維度劃分

2.2監聽器的編寫步驟:

2.3監聽域對象創建與銷燬

2.4監聽域對象屬性變化

 2.5與session中的綁定的對象相關的監聽器(對象感知監聽器)


一:什麼是監聽器?

監聽器就是監聽某個對象的的狀態變化的組件

監聽器的相關概念:

事件源:被監聽的對象  ----- 三個域對象 request  session  servletContext

監聽器:監聽事件源對象  事件源對象的狀態的變化都會觸發監聽器 ---- 6+2

註冊監聽器:將監聽器與事件源進行綁定

響應行爲:監聽器監聽到事件源的狀態變化時 所涉及的功能代碼

二 :監聽器有哪些

 2.1按維度劃分

第一維度:按照被監聽的對象劃分:ServletRequest域   HttpSession域   ServletContext域

第二維度:監聽的內容分:監聽域對象的創建與銷燬的    監聽域對象的屬性變   化的

2.2監聽器的編寫步驟:

  1. 編寫一個監聽器類去實現監聽器接口

  2. 覆蓋監聽器的方法

  3. 需要在web.xml中進行配置---註冊

2.3監聽域對象創建與銷燬

ServletContextListener

public class MyServletContextListener implements ServletContextListener{

	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("servletContextListenner銷燬了");		
	}

	public void contextInitialized(ServletContextEvent sce) {
		ServletContext servletContext = sce.getServletContext();
		sce.getSource();
		System.out.println("servletContextListenner創建了");	
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date firstTime = null;
		try {
			firstTime = format.parse("2018-12-10 20:37:46");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Timer timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {			
			@Override
			public void run() {
               	System.out.println("銀行計息了。。。。");			
			}
		}, firstTime, 24*60*60*1000);
	}

	
}

配置文件

<listener>
    <listener-class>lx.test.Listener.MyServletContextListener</listener-class>
  </listener>

HttpSessionListener、ServletRequestListener同上

2.4監聽域對象屬性變化

ServletContextAttributeListener

public class MyServletContextAttibuteListener implements ServletContextAttributeListener{

	public void attributeAdded(ServletContextAttributeEvent scab) {
		System.out.println("添加:"+scab.getName()+":"+scab.getValue());
		
	}

	public void attributeRemoved(ServletContextAttributeEvent scab) {
		System.out.println("刪除:"+scab.getName()+":"+scab.getValue());
		
	}

	public void attributeReplaced(ServletContextAttributeEvent scab) {
		System.out.println("修改:"+scab.getName()+":"+scab.getValue());
		
	}

}

配置文件

 <listener>
    <listener-class>lx.test.Listener.MyServletContextAttibuteListener</listener-class>
  </listener>

測試類

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext context = this.getServletContext();
		context.setAttribute("name", "lx");
		context.setAttribute("name", "lx2");
		context.removeAttribute("name");
		
	}

 結果

添加:name:lx
           修改:name:lx
           刪除:name:lx2

HttpSessionAttributeListener、ServletRequestAriibuteListener同上

 2.5與session中的綁定的對象相關的監聽器(對象感知監聽器)

綁定狀態:就一個對象被放到session域中

解綁狀態:就是這個對象從session域中移除了

鈍化狀態:是將session內存中的對象持久化(序列化)到磁盤

活化狀態:就是將磁盤上的對象再次恢復到session內存中

綁定與解綁的監聽器HttpSessionBindingListener

實體:

public class Person implements HttpSessionBindingListener{

	private String id;
	private String name;
	public Person(String id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void valueBound(HttpSessionBindingEvent hsb) {
		System.out.println("Person被綁定了"+hsb.getName()+hsb.getValue());
		
	}
	public void valueUnbound(HttpSessionBindingEvent hsb) {
		System.out.println("Person被解綁了");
		
	}
}

測試類

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Person person = new Person("1","lx");
		session.setAttribute("person", person);
		session.removeAttribute("person");
	}

 

鈍化與活化的監聽器HttpSessionActivationListener

 實體:

public class Customer implements HttpSessionActivationListener,Serializable{

	private String id;
	private String name;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Customer(String id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public void sessionDidActivate(HttpSessionEvent hse) {
		System.out.println("customer被活化了");
		
	}
	public void sessionWillPassivate(HttpSessionEvent hse) {
		System.out.println("customer被鈍化了");
		
	}
	
}

測試類1:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Customer customer = new Customer("1","lx");
		session.setAttribute("customer", customer);
		System.out.println("customer被放到session域中了");
	}

測試類2;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Customer customer = (Customer) request.getSession().getAttribute("customer");
		System.out.println(customer.getName());		
	}

配置文件:context.xml(放到WebContent下的META-INF目錄下)

<Context>
 <!-- maxIdleSwap:session中的對象多長時間不使用就鈍化(分鐘) -->
 <!-- directory:鈍化後的對象的文件寫到磁盤的哪個目錄下  配置鈍化的對象文件在												work/catalina/localhost/鈍化文件 -->
 <Manager className="org.apache.catalina.session.PersistentManager" 																				maxIdleSwap="1">
  <Store className="org.apache.catalina.session.FileStore" directory="lxTestActivation" />
 </Manager>
</Context>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章