Java監聽器Listener使用說明


轉載: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類

  1. package web;
  2. import javax.servlet.ServletContext;
  3. import javax.servlet.http.HttpSession;
  4. import javax.servlet.http.HttpSessionEvent;
  5. import javax.servlet.http.HttpSessionListener;
  6. public class CountListener implements HttpSessionListener {
  7.     private int count = 0;
  8.         
  9.     public void sessionCreated(HttpSessionEvent se) {
  10.         count++;
  11.         HttpSession session = se.getSession();
  12.         ServletContext sct = session.getServletContext();
  13.         sct.setAttribute("count", count);
  14.     }
  15.     public void sessionDestroyed(HttpSessionEvent se) {
  16.         count--;
  17.         HttpSession session = se.getSession();
  18.         ServletContext sct = session.getServletContext();
  19.         sct.setAttribute("count", count);
  20.     }
  21. }

步驟二:配置監聽器

  1. <listener>
  2.     <listener-class>web.CountListener</listener-class>
  3. </listener>

步驟三:添加index.jsp頁面

  1. <%@ page contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <html>
  4.     <head>    
  5.         <title></title>
  6.     </head>
  7.     <body>
  8.         當前共有<%=application.getAttribute("count").toString()%>人在線<br>
  9.         <a href="logout">登出</a>
  10.     </body>
  11. </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等文件,都會一同被載入


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM