監聽器是指專門用于對其他對象身上發生的事件或狀態改變進行監聽和相應處理的對象,當被監聽的對象發生變化時,立即采取相應的行動。
Web 監聽器
Servlet 規范
- Servlet規范中定義的一種特殊類
- 用于監聽 ServletContext、HttpSession和ServletRequest等域對象的創建與銷毀事件
- 用于監聽域對象的屬性發生修改的事件
- 可以再事件發生前、發生后做一些必要的處理
用途
- 統計在線人數和在線用戶
- 系統啟動時加載初始化信息
- 統計網站訪問量
- 跟Spring結合
創建監聽器步驟
- 創建一個實現監聽器接口的類
- 配置web.xml 或者注解進行注冊
創建類
選擇接口,例如ServletContextListener。
在IDEA中創建listener默認接口: ServletContextListener,HttpSessionListener, HttpSessionAttributeListener。
【代碼】
public class Listener implements ServletContextListener,
HttpSessionListener, HttpSessionAttributeListener {
// Public constructor is required by servlet spec
public Listener() {
}
// -------------------------------------------------------
// ServletContextListener implementation
// -------------------------------------------------------
public void contextInitialized(ServletContextEvent sce) {
/* This method is called when the servlet context is
initialized(when the Web application is deployed).
You can initialize servlet context related data here.
*/
String initParam=sce.getServletContext().getInitParameter("initParam");
System.out.println("contextInitialized:initParam="+initParam);
}
public void contextDestroyed(ServletContextEvent sce) {
/* This method is invoked when the Servlet Context
(the Web application) is undeployed or
Application Server shuts down.
*/
System.out.println("contextDestroyed");
}
// -------------------------------------------------------
// HttpSessionListener implementation
// -------------------------------------------------------
public void sessionCreated(HttpSessionEvent se) {
/* Session is created. */
System.out.println("sessionCreated");
}
public void sessionDestroyed(HttpSessionEvent se) {
/* Session is destroyed. */
System.out.println("sessionDestroyed");
}
// -------------------------------------------------------
// HttpSessionAttributeListener implementation
// -------------------------------------------------------
public void attributeAdded(HttpSessionBindingEvent sbe) {
/* This method is called when an attribute
is added to a session.
*/
System.out.println("attributeAdded");
}
public void attributeRemoved(HttpSessionBindingEvent sbe) {
/* This method is called when an attribute
is removed from a session.
*/
System.out.println("attributeRemoved");
}
public void attributeReplaced(HttpSessionBindingEvent sbe) {
/* This method is invoked when an attibute
is replaced in a session.
*/
System.out.println("attributeReplaced");
}
}
注冊
xml配置
<listener>
<listener-class>com.zdy.listener.Listener</listener-class>
</listener>
注解
多個監聽器時,無法定義加載順序(同過濾器)
@WebListener
被標注的類必須實現一下至少一個接口:
ServletContextListener
ServletContextAttributeListener
ServletRequestListener
ServletRequestAttributeListener
HttpSessionListener
HttpSessionAttributeListener
屬性
屬性名 | 類型 | 是否可選 | 描述 |
---|---|---|---|
value | String | 是 | 該監聽器的描述信息 |
【實例】
@WebListener("This is Servlet 3.0+ Listener")
監聽器啟動順序
監聽器啟動順序
監聽器的分類
按照監聽對象劃分
- ServletContext:用于監聽應用程序環境對象
- HttpSession:用于監聽用戶會話對象
- ServletRequest:用于監聽請求消息對象
按照事件的時間劃分
- 監聽域對象自身的創建和銷毀的時間監聽器
- 監聽域對象中的屬性的增加和刪除的時間監聽器
- 監聽綁定到HttpSession域中的某個對象的狀態的時間監聽器
對象創建銷毀
ServletContext--ServletContextListener
ServletContext
HttpSession--HttpSessionListener
HttpSession
ServletRequest--ServletRequestListener
ServletRequest
對象屬性增刪改
對象屬性增刪改
HttpSession中的對象狀態
HttpSession中的對象狀態
Session鈍化機制
一臉懵逼的我回頭需要再研究一下吧。po一下慕課網視頻教程()
統計在線人數實例
【Listener代碼】
@WebListener()
public class Listener implements HttpSessionListener {
private int userNumber;
public Listener() {
}
public void sessionCreated(HttpSessionEvent se) {
userNumber++;
se.getSession().getServletContext().setAttribute("userNumber",userNumber);
}
public void sessionDestroyed(HttpSessionEvent se) {
userNumber--;
se.getSession().getServletContext().setAttribute("userNumber",userNumber);
}
}
【JSP代碼】
當前在線用戶人數:${userNumber}