* Java Listener
1. Java Listener(即:Java 監聽器):用於監聽 ServletRequest、HttpSession、ServletContext 做用域對象的建立、銷燬、及其屬性修改變化
2. 在 Web 項目中能夠有多個 Java 監聽器
3. 應用場景:
a.統計在線人數
b.統計頁面訪問量
c.服務器加載時數據初始化
* Listener 的使用
1. 在 web.xml 中配置監聽器:
<listener>
<listener-class>com.ncdx.listener.MyListener</listener-class>
</listener>
2. 建立 Java 類,根據需求實現相應的接口:
1)ServletRequestListener 接口 //聲明瞭 ServletRequest 做用域對象的建立、銷燬的監聽方法
2)ServletRequestAttributeListener 接口 //聲明瞭 ServletRequest 做用域對象中屬性的添加、修改、刪除的監聽方法
3)HttpSessionListener 接口 //聲明瞭 HttpSession 做用域對象的建立、銷燬的監聽方法
4)HttpSessionAttributeListener 接口 //聲明瞭 HttpSession 做用域對象中屬性的添加、修改、刪除的監聽方法
5)ServletContextListener 接口 //聲明瞭 ServletContext 做用域對象的建立、銷燬的監聽方法
6)ServletContextAttributeListener 接口 //聲明瞭 ServletContext 做用域對象中屬性的添加、修改、刪除的監聽方法
3. 重寫相應的監聽方法
import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.ServletRequestAttributeEvent; import javax.servlet.ServletRequestAttributeListener; import javax.servlet.ServletRequestEvent; import javax.servlet.ServletRequestListener; import javax.servlet.http.HttpSessionAttributeListener; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; public class MyListener implements ServletRequestListener,ServletRequestAttributeListener, HttpSessionListener,HttpSessionAttributeListener, ServletContextListener,ServletContextAttributeListener{ @Override//監聽request對象銷燬 public void requestDestroyed(ServletRequestEvent sre) { } @Override//監聽request對象建立 public void requestInitialized(ServletRequestEvent sre) { } @Override//監聽request做用域數據的添加 public void attributeAdded(ServletRequestAttributeEvent srae) { } @Override//監聽request做用域數據的刪除 public void attributeRemoved(ServletRequestAttributeEvent srae) { } @Override//監聽request做用域數據的修改 public void attributeReplaced(ServletRequestAttributeEvent srae) { } /*.....................................................................*/ @Override//監聽session對象的建立 public void sessionCreated(HttpSessionEvent se) { } @Override//監聽session對象的銷燬 public void sessionDestroyed(HttpSessionEvent se) { } @Override//監聽session做用域數據的添加 public void attributeAdded(HttpSessionBindingEvent se) { } @Override//監聽session做用域數據的刪除 public void attributeRemoved(HttpSessionBindingEvent se) { } @Override//監聽session做用域數據的更改 public void attributeReplaced(HttpSessionBindingEvent se) { } /*.........................................................................*/ @Override//監聽application對象銷燬 public void contextDestroyed(ServletContextEvent sce) { } @Override//監聽application對象建立 public void contextInitialized(ServletContextEvent sce) { } @Override//監聽application做用域數據的添加 public void attributeAdded(ServletContextAttributeEvent scae) { } @Override//監聽application做用域數據的刪除 public void attributeRemoved(ServletContextAttributeEvent scae) { } @Override//監聽application做用域數據的更改 public void attributeReplaced(ServletContextAttributeEvent scae) { } }
/** * 模擬統計在線人數 */ public class MyListener implements ServletRequestListener,ServletRequestAttributeListener, HttpSessionListener,HttpSessionAttributeListener, ServletContextListener,ServletContextAttributeListener{ @Override public void contextInitialized(ServletContextEvent sce) {//在ServletContext對象中存儲變量用來統計在線人數 //獲取ServletContext ServletContext sc = sce.getServletContext(); //在ServletContext對象中存儲變量並初始化 sc.setAttribute("count", 0); } @Override public void sessionCreated(HttpSessionEvent se) {//session被建立時人數自增 //獲取SerevletContext對象 ServletContext sc = se.getSession().getServletContext(); //獲取在線人數的變量 int count = (int)sc.getAttribute("count"); //存儲 sc.setAttribute("count", ++count); //顯示在線人數 System.out.println("目前在線人數:" + se.getSession().getServletContext().getAttribute("count")); } @Override public void sessionDestroyed(HttpSessionEvent se) {//session被銷燬時人數自減 //獲取SerevletContext對象 ServletContext sc = se.getSession().getServletContext(); //獲取在線統計人數的變量 int count = (int)sc.getAttribute("count"); //存儲 sc.setAttribute("count", --count); //顯示在線人數 System.out.println("目前在線人數:" + se.getSession().getServletContext().getAttribute("count")); } }