JavaWeb中監聽器,JavaWeb監聽器

來源:互聯網
上載者:User

JavaWeb中監聽器,JavaWeb監聽器
 一、事件來源:三大域!

  • ServletContext
    • 生命週期監聽:ServletContextListener,它有兩個方法,一個在建立時調用,一個在銷毀時調用;
      • void contextInitialized(ServletContextEvent sce):建立Servletcontext時
      • void contextDestroyed(ServletContextEvent sce):銷毀Servletcontext時
    • 屬性監聽:ServletContextAttributeListener,它有三個方法,一個在添加屬性時調用,一個在替換屬性時調用,最後一個是在移除屬性時調用。
      • void attributeAdded(ServletContextAttributeEvent event):添加屬性時;
      • void attributeReplaced(ServletContextAttributeEvent event):替換屬性時;
      • void attributeRemoved(ServletContextAttributeEvent event):移除屬性時;
  • HttpSession
    • 生命週期監聽:HttpSessionListener,它有兩個方法,一個在建立時調用,一個在銷毀時調用;
      • void sessionCreated(HttpSessionEvent se):建立session時
      • void sessionDestroyed(HttpSessionEvent se):銷毀session時
    •  屬性監聽:HttpSessioniAttributeListener,它有三個方法,一個在添加屬性時調用,一個在替換屬性時調用,最後一個是在移除屬性時調用。
      • void attributeAdded(HttpSessionBindingEvent event):添加屬性時;
      • void attributeReplaced(HttpSessionBindingEvent event):替換屬性時
      • void attributeRemoved(HttpSessionBindingEvent event):移除屬性時
  •  ServletRequest
    •  生命週期監聽:ServletRequestListener,它有兩個方法,一個在建立時調用,一個在銷毀時調用;
      • void requestInitialized(ServletRequestEvent sre):建立request時
      • void requestDestroyed(ServletRequestEvent sre):銷毀request時
    • 屬性監聽:ServletRequestAttributeListener,它有三個方法,一個在添加屬性時調用,一個在替換屬性時調用,最後一個是在移除屬性時調用。
      • void attributeAdded(ServletRequestAttributeEvent srae):添加屬性時
      • void attributeReplaced(ServletRequestAttributeEvent srae):替換屬性時
      • void attributeRemoved(ServletRequestAttributeEvent srae):移除屬性時
二、javaWeb中完成編寫監聽器:
  • 寫一個監聽器類:要求必須去實現某個監聽器介面;
  • 註冊,是在web.xml中配置來完成註冊!
 1 package web.listener; 2  3 import javax.servlet.ServletContextEvent; 4 import javax.servlet.ServletContextListener; 5 import javax.servlet.annotation.WebListener; 6  7 /** 8  * ServletContext監聽器 9  * 可以在這個監聽器存放一些在Tomcat啟動時就要完成的代碼!10  * */11 @WebListener()12 public class AListener implements ServletContextListener {13     @Override14     public void contextInitialized(ServletContextEvent sce) {15         System.out.println("哇,我來了");16     }17 18     @Override19     public void contextDestroyed(ServletContextEvent sce) {20         System.out.println("哇,再見");21     }22 }
三、 事件對象:
  • ServletContextEvent:ServletContext getServletContext()
  • HttpSessionEvent:HttpSession getSession()
  • ServletRequest:
    • ServletContext getServletContext();
    • ServletReques getServletRequest();
  • ServletContextAttributeEvent:
    • ServletContext getServletContext();
    • String getName():擷取屬性名稱
    • Object getValue():擷取屬性值
  • HttpSessionBindingEvent:略
  • ServletRequestAttributeEvent :略
 四、感知監聽(都與HttpSession相關)
  • 它用來添加到JavaBean上,而不是添加到三大域上!
  • 這兩個監聽器都不需要在web.xml中註冊! 
HttpSessionBindingListener:添加到javabean上,javabean就知道自己是否添加到session中了。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.