1. 監聽器

1.  什麼是監聽器html

  • 監聽器也叫Listener,是Servlet的監聽器,它能夠監聽客戶端的請求、服務端的操做等。經過監聽器,能夠自動激發一些操做,好比監聽在線的用戶的數量。
  • 所謂的監聽器,是指對整個web環境的監聽,當被見識的對象發生變化時,當即調用相應的方法進行處理。

 

2.監聽器最多見的應用場景:java

  • Java SE GUI編程;
  • Android 手機開發編程。

3. 監聽器術語web

  • 事件源:被監聽對象。(目標對象);
  • 監聽器對象:用於監聽"事件源"的對象;
  • 註冊(綁定):將"監聽器對象"註冊給"事件源"。當"事件源"發生某些行爲時,監聽對象將被執行;
  • 事件:事件源行爲的稱呼;
  • 事件對象:在「監聽器對象」中得到「事件源」。

4. 監聽器的編寫流程編程

  • 編寫監聽器實現類,須要實現指定的接口;
  • 在web.xml文件配置監聽器(部分監聽不須要配置);

5. JavaEE規範規定了8個監聽器接口用於監聽不一樣的web對象服務器

  • 監聽域對象建立與銷燬
  • 監聽域對象屬性變動(添加、替換、刪除)
  • 監聽session做用域特殊Javabean

6. 域對象監聽器session

  • ServletRequestListener
  • HttpSessionListener
  • ServletContext

7. 域對象屬性監聽器(app

4大域對象:ServletContext 、Request、Session、PageContextide

7.1 做用域屬性操做編碼

  setAttribute(k,v)    getAttribute(k)    removeAttribute(k)spa

7.2 域對象屬性變動監聽器

  • ServletRequestAttributeListener
  • HttpSessionAttributeListener
  • ServletContextAttributeListener

  7.3 特殊的Javabean監聽器(Session做用域)

  • HttpSessionBindingListener
  • HttpSessionActivationListener(鈍化和活化)

前面六個監聽器須要在web.xml中配置,最後的兩個監聽器不須要再web.xml中配置

 

8. 實例

  8.1 ServletRequestListener

  監聽request對象建立或銷燬,其中request對象的特色以下:

  • 類型:Javax.servlet.http.HttpServletRequest
  • 描述:來自客戶端的請求經Servlet容器處理後,由request對象進行封裝。注:客戶端和服務器的一次通訊就是一次請求(發送請求或獲得相應)。
  • 做用域:request。說明,此次請求結束後,它的生命週期 就結束了。
  • 舉例:
    • 建立ServletRequestListener實現類MyServletRequestListener;
 1 package com.eagle.listener;
 2 
 3 import javax.servlet.ServletRequestEvent;
 4 import javax.servlet.ServletRequestListener;
 5 
 6 public class MyServletRequestListener implements ServletRequestListener {
 7 
 8     public MyServletRequestListener() {
 9         super();
10         System.out.println("request對象的構造方法");
11     }
12 
13     public void requestInitialized(ServletRequestEvent sre) {
14         System.out.println("request建立了!");
15     }
16 
17     public void requestDestroyed(ServletRequestEvent sre) {
18         System.out.println("request對象銷燬了!");
19     }
20 
21 }
MyServletRequestListener
    • 在web.xml文件配置監聽器。
1   <listener>
2     <listener-class>com.eagle.listener.MyServletRequestListener</listener-class>
3   </listener>
web.xml

服務器啓動就先構造了request對象,而後每一個頁面請求都會觸發

MyServletRequestListener.requestInitialized(ServletRequestEvent sre)方法和

MyServletRequestListener.requestDestroyed(ServletRequestEvent sre)方法

  8.2 HttpSessionListener

  • 類型:Javax.servlet.http.HttpSession
  • 描述:表示一個會話,用來保存用戶信息,以便跟蹤每一個用戶的狀態。(不要用來保存業務數據,request)
  • 定義:是指在一段時間內客戶端和服務器之間的一連串的相關的交互過程。
  • 做用域:session。
  • 舉例
    • 建立MyHttpSessionListener
 1 package com.eagle.listener;
 2 
 3 import javax.servlet.http.HttpSessionEvent;
 4 import javax.servlet.http.HttpSessionListener;
 5 
 6 public class MyHttpSessionListener implements HttpSessionListener {
 7 
 8     public MyHttpSessionListener() {
 9 
10         super();
11         System.out.println("session對象構造方法");
12     }
13 
14     public void sessionCreated(HttpSessionEvent arg0) {
15         System.out.println("建立session對象");
16     }
17 
18     public void sessionDestroyed(HttpSessionEvent arg0) {
19         System.out.println("銷燬session對象");
20     }
21 
22 }
MyHttpSessionListener
    • 建立servlet CreateSession
 1 package com.eagle.web;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 import javax.servlet.http.HttpSession;
 9 
10 public class CreateSession extends HttpServlet {
11 
12     private static final long serialVersionUID = 1L;
13 
14     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
15         request.setCharacterEncoding("utf-8");
16         response.setContentType("text/html;charset=utf-8"); 
17         HttpSession httpSession = request.getSession();
18         httpSession.setMaxInactiveInterval(10);
19         String id = httpSession.getId();
20         if(httpSession.isNew()) {
21             httpSession.setAttribute("name", "狼");        
22             String name = (String) httpSession.getAttribute("name");
23             response.getWriter().print("恭喜session建立成功,sessionid="+id+";name="+name);
24         }else {
25             String name = (String) httpSession.getAttribute("name");
26             response.getWriter().print("服務器已經存在session了,sessionid="+id+";name="+name);
27         }
28     }
29 
30     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
31         doGet(request, response);
32     }
33 }
CreateSession
    • web.xml文件中配置監聽器
1   <listener>
2     <listener-class>com.eagle.listener.MyHttpSessionListener</listener-class>
3   </listener>
web.xml

 

 

  8.3 servletContextListener

  • 類型:javax.servlet.ServletContext
  • 描述:從servlet配置對象得到的servlet上下文
  • 做用域:application
  • 這個對象的生命週期是最長的。服務器啓動的時候就會建立application對象。從服務器存在到服務器終止,都一直存在,且只保留一個對象,全部用戶共享一個application。不是很經常使用。
  • 舉例:
    • 建立MyServletContextListener監聽器
 1 package com.eagle.listener;
 2 
 3 import javax.servlet.ServletContextEvent;
 4 import javax.servlet.ServletContextListener;
 5 
 6 public class MyServletContextListener implements ServletContextListener {
 7 
 8     public MyServletContextListener() {
 9         System.out.println("ServletContext對象構建!");
10     }
11 
12     public void contextInitialized(ServletContextEvent sce) {
13         System.out.println("ServletContext對象初始化!");
14     }
15     
16     public void contextDestroyed(ServletContextEvent sce) {
17         System.out.println("ServletContext對象銷燬!");
18     }
19 }
MyServletContextListener
    • 在web.xml文件中配置監聽器

 

1  <listener>
2       <listener-class>com.eagle.listener.MyServletContextListener</listener-class>
3   </listener>
web.xml

 

 ./shutdown.sh   //中止Apache服務

  

  8.4 ServletRequestAttributeListener

  • 編寫servlet    ServletRequestAttribute;
 1 package com.eagle.web;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 public class ServletRequestAttribute extends HttpServlet {
10 
11     private static final long serialVersionUID = 1L;
12 
13     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
14         // 編碼
15         request.setCharacterEncoding("utf-8");
16         response.setContentType("text/html;charset=utf-8");
17         request.setAttribute("name", "first");
18         String name = (String) request.getAttribute("name");
19         response.getWriter().print("name="+name);
20         request.removeAttribute("name");
21     }
22 
23     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
24         doGet(request, response);
25     }
26 }
ServletRequestAttribute
  • 編寫ServletRequestAttributeListener;
 1 package com.eagle.listener;
 2 
 3 import javax.servlet.ServletRequestAttributeEvent;
 4 import javax.servlet.ServletRequestAttributeListener;
 5 
 6 public class MyRequestAttribute implements ServletRequestAttributeListener {
 7 
 8     public MyRequestAttribute() {
 9         super();
10         System.out.println("requestattribute初始化!");
11     }
12 
13     public void attributeAdded(ServletRequestAttributeEvent sre) {
14         System.out.println("request對象添加了新屬性!");
15     }
16 
17     public void attributeRemoved(ServletRequestAttributeEvent sre) {
18         System.out.println("reqeust對象移除了屬性!");
19     }
20 
21     public void attributeReplaced(ServletRequestAttributeEvent sre) {
22         System.out.println("request修改了屬性!");
23     }
24 
25 }
MyRequestAttribute
  • 配置web.xml。
<listener>
    <listener-class>com.eagle.listener.MyRequestAttribute</listener-class>
  </listener>
web.xml

 

  8.5 HttpSessionAttributeListener

  • 編寫servlet
 1 package com.eagle.web;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 import javax.servlet.http.HttpSession;
 9 
10 public class SessionAttribute extends HttpServlet {
11 
12     private static final long serialVersionUID = 1L;
13 
14     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
15         HttpSession session = request.getSession();
16         session.setAttribute("name", "first");
17         session.setAttribute("name", "second");
18         session.getAttribute("name");
19         session.removeAttribute("name");
20         response.getWriter().write("hello haohao...");
21     }
22 
23     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
24         doGet(request, response);
25     }
26 }
SessionAttribute
  • 編寫HttpSessionAttributeListener
 1 package com.eagle.listener;
 2 
 3 import javax.servlet.http.HttpSessionAttributeListener;
 4 import javax.servlet.http.HttpSessionBindingEvent;
 5 
 6 public class MySessionAttributeListener implements HttpSessionAttributeListener {
 7 
 8     public MySessionAttributeListener() {
 9         super();
10         System.out.println("sessionAttributeListener初始化!");
11     }
12 
13     public void attributeAdded(HttpSessionBindingEvent hbe) {
14         System.out.println("sessionAttributeListener 添加了新屬性!");
15     }
16 
17     public void attributeRemoved(HttpSessionBindingEvent hbe) {
18         System.out.println("sessionAttributeListener 移除了屬性!");
19     }
20 
21     public void attributeReplaced(HttpSessionBindingEvent hbe) {
22         System.out.println("sessionAttributeListener 修改了屬性!");
23     }
24 
25 }
MySessionAttributeListener
  • 配置web.xml
1   <listener>
2     <listener-class>com.eagle.listener.MySessionAttributeListener</listener-class>
3   </listener>
web.xml

注:設置session引起request屬性修改監聽器

  8.6 ServletContextAttributeListener

  • 編寫servlet
 1 package com.eagle.web;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletContext;
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 public class ServletContextAttribute extends HttpServlet {
12 
13     private static final long serialVersionUID = 1L;
14 
15     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
16         ServletContext servletContext = request.getServletContext();
17         servletContext.setAttribute("name", "first");
18         servletContext.setAttribute("name", "second");
19         servletContext.removeAttribute("name");
20         response.getWriter().write("hello haohao...");
21     }
22 
23     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
24         doGet(request, response);
25     }
26 }
ServletContextAttribute
  • 編寫ServletContextAttributeListener
 1 package com.eagle.listener;
 2 
 3 import javax.servlet.ServletContextAttributeEvent;
 4 import javax.servlet.ServletContextAttributeListener;
 5 
 6 public class MyServletContextAttributeListener implements ServletContextAttributeListener {
 7 
 8     public MyServletContextAttributeListener() {
 9         super();
10         System.out.println("ServletContextAttributeListener對象構造!");
11     }
12 
13     public void attributeAdded(ServletContextAttributeEvent sce) {
14         System.out.println("ServletContextAttributeListener對象添加屬性!");
15     }
16 
17     public void attributeReplaced(ServletContextAttributeEvent sce) {
18         System.out.println("ServletContextAttributeListener對象修改屬性!");
19     }
20 
21     public void attributeRemoved(ServletContextAttributeEvent sce) {
22         System.out.println("ServletContextAttributeListener對象移除屬性!");
23     }
24 }
MyServletContextAttributeListener
  • 配置web.xml
1   <listener>
2     <listener-class>com.eagle.listener.MyServletContextAttributeListener</listener-class>
3   </listener>
web.xml

  

  8.7 HttpSessionBindingListener

  • 編寫servlet
 1 package com.eagle.web;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 import javax.servlet.http.HttpSession;
 9 
10 import com.eagle.listener.MyHttpSessionBindingListener;
11 
12 public class ServletHttpSessionBindingListener extends HttpServlet {
13 
14     private static final long serialVersionUID = 1L;
15 
16     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
17         MyHttpSessionBindingListener ml = new MyHttpSessionBindingListener();
18         HttpSession hs = request.getSession();
19         hs.setAttribute("ml", ml);
20         hs.invalidate();
21         response.getWriter().write("hello haohao...");
22     }
23 
24     public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
25         doGet(request, response);
26     }
27 }
ServletHttpSessionBindingListener
  • 編寫HttpSessionBindingListener
 1 package com.eagle.listener;
 2 
 3 import javax.servlet.http.HttpSessionBindingEvent;
 4 import javax.servlet.http.HttpSessionBindingListener;
 5 
 6 public class MyHttpSessionBindingListener implements HttpSessionBindingListener {
 7 
 8     public MyHttpSessionBindingListener() {
 9         super();
10         System.out.println("httpSessionBindingListening對象初始化");
11     }
12 
13     public void valueBound(HttpSessionBindingEvent hse) {
14         System.out.println("httpSessionBindingListening對象綁定!");
15     }
16 
17     public void valueUnbound(HttpSessionBindingEvent hse) {
18         System.out.println("httpSessionBindingListening對象解綁!");
19     }
20 
21 }
MyHttpSessionBindingListener

 

  8.8 HttpSessionActivationListener

   參照:https://www.cnblogs.com/eaglesour/p/9474540.html

相關文章
相關標籤/搜索