轉載:http://blog.csdn.net/meng2602956882/article/details/13511587
1、什么是Java監聽器
監聽器也叫Listener,是Servlet的監聽器,它可以監聽客戶端的請求、服務端的操作等。通過監聽器,可以自動激發一些操作,比如監聽在線的用戶的數量。
2、Listener接口分類
1.1> ServletContextListener監聽ServletContext對象
1.2> ServletContextAttributeListener監聽對ServletContext屬性的操作,比如增加、刪除、修改
2.1> HttpSessionListener監聽Session對象
2.2> HttpSessionActivationListener監聽HTTP會話的active和passivate情況,passivate是指非活動的session被寫入持久設備(比如硬盤),active相反。
2.3> HttpSessionAttributeListener監聽Session中的屬性操作
3.1> ServletRequestListener監聽Request對象
3.2> ServletRequestAttributeListener監聽Requset中的屬性操作
3、Java代碼
1.1> ServletContextListener
contextInitialized(ServletContextEvent) 初始化時調用
contextDestoryed(ServletContextEvent) 銷毀時調用,即當服務器重新加載時調用
2.1>HttpSessionListener
sessionCreated(HttpSessionEvent) 初始化時調用
sessionDestoryed(HttpSessionEvent) 銷毀時調用,即當用戶注銷時調用
3.1>ServletRequestListener
requestinitialized(ServletRequestEvent) 對實現客戶端的請求進行監聽
requestDestoryed(ServletRequestEvent) 對銷毀客戶端進行監聽,即當執行request.removeAttribute("XXX")時調用
4、Demo
步驟一:新建CountListener類
- package web;
- import javax.servlet.ServletContext;
- import javax.servlet.http.HttpSession;
- import javax.servlet.http.HttpSessionEvent;
- import javax.servlet.http.HttpSessionListener;
- public class CountListener implements HttpSessionListener {
- private int count = 0;
- public void sessionCreated(HttpSessionEvent se) {
- count++;
- HttpSession session = se.getSession();
- ServletContext sct = session.getServletContext();
- sct.setAttribute("count", count);
- }
- public void sessionDestroyed(HttpSessionEvent se) {
- count--;
- HttpSession session = se.getSession();
- ServletContext sct = session.getServletContext();
- sct.setAttribute("count", count);
- }
- }
步驟二:配置監聽器
- <listener>
- <listener-class>web.CountListener</listener-class>
- </listener>
步驟三:添加index.jsp頁面
- <%@ page contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <html>
- <head>
- <title></title>
- </head>
- <body>
- 當前共有<%=application.getAttribute("count").toString()%>人在線<br>
- <a href="logout">登出</a>
- </body>
- </html>
步驟四:添加LogoutServlet類
package web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LogoutServlet extends HttpServlet {
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
session.invalidate();
out.close();
}
}
步驟四:配置LogoutServlet
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>web.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>
步驟五:運行查看結果
由於Chrome瀏覽器再打開選項卡也是與第一個窗口共用sessionId,所以使用火狐瀏覽器來模擬第二個上線的用戶
回到Chrome瀏覽器,點擊刷新
點擊火狐瀏覽器中的登出,然后回到Chrome瀏覽器刷新頁面
補充:ContextLoaderListener的作用就是啟動Web容器時,自動裝配ApplicationContext的配置信息。因為它實現了ServletContextListener這個接口,在web.xml配置這個監聽器,啟動容器時,就會默認執行它實現的方法。
<context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:applicationContext-*.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
第一段說明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。如果查看ContextLoaderServlet的API,可以看到它也關聯了ContextLoader這個類而且它實現了HttpServlet這個接口
第二段,ContextLoader創建的是 XmlWebApplicationContext這樣一個類,它實現的接口是WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->
BeanFactory這樣一來spring中的所有bean都由這個類來創建
第三段,講如何部署applicationContext的xml文件,如果在web.xml中不寫任何參數配置信息,默認的路徑是"/WEB-INF/applicationContext.xml,在WEB-INF目錄下創建的xml文件的名稱必須是applicationContext.xml。如果是要自定義文件名可以在web.xml里加入contextConfigLocation這個context參數:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext-*.xml
</param-value>
</context-param>
在<param-value> </param-value>里指定相應的xml文件名,如果有多個xml文件,可以寫在一起並一“,”號分隔。上面的applicationContext-*.xml采用通配符,比如這那個目錄下有applicationContext-ibatis-base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml等文件,都會一同被載入