監聽器實例

常用的監聽接口   
  
1.ServletContextAttributeListener   
監聽對ServletContext屬性的操作,比如增加/刪除/修改   
2.ServletContextListener   
監聽ServletContext,當創建ServletContext時,激發contextInitialized(ServletContextEvent sce)方法;當銷燬ServletContext時,激發contextDestroyed(ServletContextEvent sce)方法。   
3.HttpSessionListener   
監聽HttpSession的操作。當創建一個Session時,激發session Created(SessionEvent se)方法;當銷燬一個Session時,激發sessionDestroyed (HttpSessionEvent se)方法。   
4.HttpSessionAttributeListener   
監聽HttpSession中的屬性的操作。當在Session增加一個屬性時,激發attributeAdded(HttpSessionBindingEvent se) 方法;當在Session刪除一個屬性時,激發attributeRemoved(HttpSessionBindingEvent se)方法;當在Session屬性被重新設置時,激發attributeReplaced(HttpSessionBindingEvent se) 方法。   
  
使用範例:   
由監聽器管理共享數據庫連接   
  
生命週期事件的一個實際應用由context監聽器管理共享數據庫連接。在web.xml中如下定義監聽器:   
<listener>   
    <listener-class>XXX.MyConnectionManager</listener-class>   
</listener> ?server創建監聽器的實例,接受事件並自動判斷實現監聽器接口的類型。要記住的是由於監聽器是配置在部署描述符web.xml中,所以不需要改變任何代碼就可以添加新的監聽器。   
  
public class MyConnectionManager implements ServletContextListener{     
public void contextInitialized(ServletContextEvent e) {    
        Connection con = // create connection    
        e.getServletContext().setAttribute("con", con);    
    }     
   public void contextDestroyed(ServletContextEvent e) {    
        Connection con = (Connection) e.getServletContext().getAttribute("con");    
        try {   
          con.close();    
        }    
       catch (SQLException ignored) { } // close connection    
    }    
}     
監聽器保證每新生成一個servlet context都會有一個可用的數據庫連接,並且所有的連接對會在context關閉的時候隨之關閉。     
  
在web.xml中加入:   
<listener><listener-class>servletlistener111111.SecondListener</listener-class> </listener>

==================================================

關於用戶超時的例子:

public class OnlineUserListener implements HttpSessionListener {
    public void sessionCreated(HttpSessionEvent event) {
    }
    public void sessionDestroyed(HttpSessionEvent event) {
        HttpSession session = event.getSession();
        ServletContext application = session.getServletContext();
        // 取得登錄的用戶名
        String username = (String) session.getAttribute("username");
        // 從在線列表中刪除用戶名
        List onlineUserList = (List) application.getAttribute("onlineUserList");
        onlineUserList.remove(username);
        System.out.println(username + "超時退出。");
    }
}

以下兩種情況下就會發生sessionDestoryed(會話銷燬)事件:

1.執行session.invalidate()方法時。例如:request.getSession().invalidate();

2.如果用戶長時間沒有訪問服務器,超過了會話最大超時時間,服務器就會自動銷燬超時的session。會話超時時間可以在web.xml中進行設置。

========================================

使用HttpSessionBindingListener

HttpSessionBindingListener雖然叫做監聽器,但使用方法與HttpSessionListener完全不同。我們實際看一下它是如何使用的。

我們的OnlineUserBindingListener實現了HttpSessionBindingListener接口,接口中共定義了兩個方法:valueBound()和valueUnbound(),分別對應數據綁定,和取消綁定兩個事件。

所謂對session進行數據綁定,就是調用session.setAttribute()把HttpSessionBindingListener保存進session中。我們在LoginServlet.java中進行這一步。

// 把用戶名放入在線列表
session.setAttribute("onlineUserBindingListener", new OnlineUserBindingListener(username));
       
這就是HttpSessionBindingListener和HttpSessionListener之間的最大區別:HttpSessionListener只需要設置到web.xml中就可以監聽整個應用中的所有session。HttpSessionBindingListener必須實例化後放入某一個session中,纔可以進行監聽。

從監聽範圍上比較,HttpSessionListener設置一次就可以監聽所有session,HttpSessionBindingListener通常都是一對一的。

正是這種區別成就了HttpSessionBindingListener的優勢,我們可以讓每個listener對應一個username,這樣就不需要每次再去session中讀取username,進一步可以將所有操作在線列表的代碼都移入listener,更容易維護。

valueBound()方法的代碼如下:

public void valueBound(HttpSessionBindingEvent event) {
    HttpSession session = event.getSession();
    ServletContext application = session.getServletContext();

    // 把用戶名放入在線列表
    List onlineUserList = (List) application.getAttribute("onlineUserList");
    // 第一次使用前,需要初始化
    if (onlineUserList == null) {
        onlineUserList = new ArrayList();
        application.setAttribute("onlineUserList", onlineUserList);
    }
    onlineUserList.add(this.username);
}
       
username已經通過構造方法傳遞給listener,在數據綁定時,可以直接把它放入用戶列表。

與之對應的valueUnbound()方法,代碼如下:

public void valueUnbound(HttpSessionBindingEvent event) {
    HttpSession session = event.getSession();
    ServletContext application = session.getServletContext();

    // 從在線列表中刪除用戶名
    List onlineUserList = (List) application.getAttribute("onlineUserList");
    onlineUserList.remove(this.username);

    System.out.println(this.username + "退出。");
}


       
這裏可以直接使用listener的username操作在線列表,不必再去擔心session中是否存在username。

valueUnbound的觸發條件是以下三種情況:

1.執行session.invalidate()時。

2.session超時,自動銷燬時。

3.執行session.setAttribute("onlineUserListener", "其他對象");或session.removeAttribute("onlineUserListener");將listener從session中刪除時。

因此,只要不將listener從session中刪除,就可以監聽到session的銷燬。

 

 

ServletContextAttributeListener

/**
*
*/
package org.insurance.servlet;

import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;

/**
* @author TEMP
*
*/
public class PageCountListener implements ServletContextAttributeListener {

private ServletContext ctx=null;
   //以下代碼實現ServleContextListener
   public void contextDestroyed(ServletContextEvent sce){
     this.ctx=null;
     System.out.println(new Date().toLocaleString()+"-------ServletContext被銷燬");
   }
   public void contextInitialized(ServletContextEvent sce){
     this.ctx=sce.getServletContext();
     System.out.println(new Date().toLocaleString()+"-------ServletContext被創建");
   }
/* (non-Javadoc)
* @see javax.servlet.ServletContextAttributeListener#attributeAdded(javax.servlet.ServletContextAttributeEvent)
*/
//增加一個新的屬性時激發
public void attributeAdded(ServletContextAttributeEvent event) {
   // TODO Auto-generated method stub
     System.out.println("增加了一個ServletContext屬性-------("+event.getName()+","+event.getValue()+")");
}

/* (non-Javadoc)
* @see javax.servlet.ServletContextAttributeListener#attributeRemoved(javax.servlet.ServletContextAttributeEvent)
*/
//刪除一個新的屬性時激發
public void attributeRemoved(ServletContextAttributeEvent event) {
   // TODO Auto-generated method stub
   System.out.println("刪除了一個ServletContext屬性-------("+event.getName()+","+event.getValue()+")");
}

/* (non-Javadoc)
* @see javax.servlet.ServletContextAttributeListener#attributeReplaced(javax.servlet.ServletContextAttributeEvent)
*/
//屬性被替代時激發
public void attributeReplaced(ServletContextAttributeEvent event) {
   // TODO Auto-generated method stub
   System.out.println("改變了一個ServletContext屬性-------("+event.getName()+","+event.getValue()+")");

}

}

web.xml

   <listener>
    <listener-class>org.insurance.servlet.PageCountListener</listener-class>
</listener>

jsp


if(getServletContext().getAttribute("usernames")!=null)
{
System.out.println("getServletContext().getAttribute(usernames)========="+getServletContext().getAttribute("usernames"));
Integer str=(Integer)getServletContext().getAttribute("usernames");
str=str+1;
getServletContext().setAttribute("usernames",str);
System.out.println("getServletContext().getAttribute(usernames)========="+getServletContext().getAttribute("usernames"));
}else{
getServletContext().setAttribute("usernames",1);
}

//out.println("Step 2:刪除一個ServletContext屬性");
//getServletContext().removeAttribute("usernames");

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