Servlet Listener,幾個常用監聽器。
Servlet監聽器
Listener
觀察者模式。
本部落格中關于觀察者模式的博文:
http://www.cnblogs.com/mengdd/archive/2013/02/08/2909206.html
其參考資料中列出了更多的博文。
Listener是Servlet的監聽器,它可以監聽用戶端的請求、服務端的操作等。
通過監聽器,可以自動激發一些操作,比如監聽線上的使用者的數量。
常用監聽接口
ServletContextListener
監聽ServletContext。
當建立ServletContext時激發contextInitialized(ServletContextEvent sce);
所有的ServletContextListeners會在所有filters和servlets初始化之前收到初始化通知。
當銷毀ServletContext時激發contextDestroyed(ServletContextEvent sce)。
所有filters和servlets銷毀之後,ServletContextListeners才得到context銷毀通知。
也即,ServletContextListeners是早出晚歸型的。
ServletContextAttributeListener
監聽對ServletContext屬性的操作,比如增加、删除、修改屬性。
方法分别為:
attributeAdded(ServletContextAttributeEvent event)
attributeRemoved(ServletContextAttributeEvent event)
attributeReplaced(ServletContextAttributeEvent event)
HttpSessionListener
監聽HttpSession的操作。
當建立一個HttpSession時,就激發sessionCreated(HttpSessionEvent se)方法,(這樣就可以給線上人數加1)。
當銷毀一個Session時,激發sessionDestroyed(HttpSessionEvent se)方法。
HttpSessionAttributeListener
監聽HttpSession的屬性變化,監聽增加、移除和修改事件:
attributeAdded(HttpSessionBindingEvent event)
attributeRemoved(HttpSessionBindingEvent event)
attributeReplaced(HttpSessionBindingEvent event)
使用執行個體
執行個體1:ServletContextListener:

package com.mengdd.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyServletContextListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("contextDestroyed: " + sce.getServletContext());
}
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("contextInitialized: " + sce.getServletContext());
}
}
MyServletContextListener
配置在web.xml中:
<listener>
<listener-class>com.mengdd.listener.MyServletContextListener</listener-class>
</listener>
執行個體2:ServletContextAttributeListener

package com.mengdd.listener;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
public class MyServletContextAttributeListener implements
ServletContextAttributeListener {
@Override
public void attributeAdded(ServletContextAttributeEvent event) {
System.out.println("attributeAdded");
System.out.println(event.getName() + " : " + event.getValue());
}
@Override
public void attributeRemoved(ServletContextAttributeEvent event) {
System.out.println("attributeRemoved");
System.out.println(event.getName() + " : " + event.getValue());
}
@Override
public void attributeReplaced(ServletContextAttributeEvent event) {
System.out.println("attributeReplaced");
System.out.println(event.getName() + " : " + event.getValue());
// 注意Replaced方法參數的getValue()擷取的是被替換掉的值,即上一個value
}
}
MyServletContextAttributeListener
配置:
<listener>
<listener-class>com.mengdd.listener.MyServletContextAttributeListener</listener-class>
</listener>
測試用JSP頁面:
頁面1的body:
<body>
<%
application.setAttribute("attr1", "hello");
%>
<%
application.setAttribute("attr1", "world");
%>
<%
application.setAttribute("attr1", "aa");
%>
<%
application.setAttribute("attr1", "bb");
%>
</body>
頁面2的body:
<body>
<%
application.removeAttribute("attr1");
%>
</body>
HttpSessionListener、HttpSessionAttributeListener等的執行個體就不一一列舉了。
參考資料
北京聖思園張龍老師Java Web視訊教程。
作者: 聖騎士Wind
出處: 部落格園: 聖騎士Wind
Github: https://github.com/mengdd
微信公衆号: 聖騎士Wind