日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Java如何使用Listener

發布時間:2025/3/20 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java如何使用Listener 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

定義

用于監聽Web應用的內部事件的實現類??梢员O聽用戶session的開始與結束,用戶請求的到達等等,當事件發生時,會回調監聽器的內部方法

使用Listener步驟

  • 通過實現具體接口創建實現類(可實現多個監聽器接口)
  • 配置實現類成為監聽器,有兩種配置方式:
  • 直接用@WebListener注解修飾實現類
  • 通過web.xml方式配置,代碼如下:
  • <listener><listener-class>com.zrgk.listener.MyListener</lisener-class> </listener>

    常用Web事件監聽器接口

    1. ServletContextListener

  • 該接口用于監聽Web應用的啟動與關閉
  • 該接口的兩個方法:
  • contextInitialized(ServletContextEvent event); // 啟動web應用時調用
  • contextDestroyed(ServletContextEvent event); // 關閉web應用時調用
  • 如何獲得application對象:
    ServletContext application = event.getServletContext();
  • 示例
  • @WebListener public class MyServetContextListener implements ServletContextListener{//web應用關閉時調用該方法@Overridepublic void contextDestroyed(ServletContextEvent event) {ServletContext application = event.getServletContext();String userName = application.getInitParameter("userName"); System.out.println("關閉web應用的用戶名字為:"+userName);}//web應用啟動時調用該方法@Overridepublic void contextInitialized(ServletContextEvent event) {ServletContext application = event.getServletContext();String userName = application.getInitParameter("userName"); System.out.println("啟動web應用的用戶名字為:"+userName);}}

    2. ServletContextAttributeListener

  • 該接口 用于監聽ServletContext范圍(application)內屬性的改變。
  • 該接口的兩個方法:
  • attributeAdded(ServletContextAttributeEvent event); // 當把一個屬性存進application時觸發
  • attributeRemoved(ServletContextAttributeEvent event); // 當把一個屬性從application刪除時觸發
  • attributeReplaced(ServletContextAttributeEvent event); // 當替換application內的某個屬性值時觸發
  • 如何獲得application對象:
    ServletContext application = event.getServletContext();
  • 示例
  • @WebListener public class MyServletContextAttributeListener implements ServletContextAttributeListener{//向application范圍內添加一個屬性時觸發@Overridepublic void attributeAdded(ServletContextAttributeEvent event) {String name = event.getName();//向application范圍添加的屬性名Object val = event.getValue(); //向application添加的屬性對應的屬性值System.out.println("向application范圍內添加了屬性名為:"+name+",屬性值為:"+val+"的屬性");}//刪除屬性時觸發@Overridepublic void attributeRemoved(ServletContextAttributeEvent event) {// ... }//替換屬性值時觸發@Overridepublic void attributeReplaced(ServletContextAttributeEvent event) {// ... }}

    3. ServletRequestListener與ServletRequestAttributeListener

  • ServletRequestListener用于監聽用戶請求,而ServletRequestAttributeListener用于監聽request范圍內屬性的變化。
  • ServletRequestListener兩個需要實現的方法
  • requestInitialized(ServletRequestEvent event); //用戶請求到達、被初始化時觸發
  • requestDestroyed(ServletRequestEvent event); // 用戶請求結束、被銷毀時觸發
  • ServletRequestAttributeListener兩個需要實現的方法
  • attributeAdded(ServletRequestAttributeEvent event); // 向request范圍內添加屬性時觸發
  • attributeRemoved(ServletRequestAttributeEvent event); // 從request范圍內刪除某個屬性時觸發
  • attributeReplaced(ServletRequestAttributeEvent event); // 替換request范圍內某個屬性值時觸發
  • 獲取reqeust對象
    HttpServletRequest req = (HttpServletRequest)event.getServletRequest();
  • 代碼片
  • @WebListener public class MyRequestListener implements ServletRequestListener,ServletRequestAttributeListener{//用戶請求結束、被銷毀時觸發@Overridepublic void requestDestroyed(ServletRequestEvent event) {HttpServletRequest req = (HttpServletRequest) event.getServletRequest();String ip = req.getRemoteAddr();System.out.println("IP為:"+ip+"的用戶發送到"+req.getRequestURI()+"的請求結束");}//用戶請求到達、被初始化時觸發@Overridepublic void requestInitialized(ServletRequestEvent event) {HttpServletRequest req = (HttpServletRequest) event.getServletRequest();String ip = req.getRemoteAddr();System.out.println("IP為:"+ip+"的用戶發送到"+req.getRequestURI()+"的請求被初始化");}//向request范圍內添加屬性時觸發@Overridepublic void attributeAdded(ServletRequestAttributeEvent event) {String name = event.getName();Object val = event.getValue();System.out.println("向request范圍內添加了名為:"+name+",值為:"+val+"的屬性");}//刪除request范圍內某個屬性時觸發@Overridepublic void attributeRemoved(ServletRequestAttributeEvent event) {//... }//替換request范圍內某個屬性值時觸發@Overridepublic void attributeReplaced(ServletRequestAttributeEvent event) {// ... } }

    4. HttpSessionListener與HttpSessionAttributeListener

  • HttpSessionListener監聽用戶session的開始與結束,而HttpSessionAttributeListener監聽HttpSession范圍(session)內的屬性的改變。
  • HttpSessionListener要實現的方法:
  • sessionCreated(HttpSessionEvent event); // 用戶與服務器的會話開始、創建時觸發
  • sessionDestroyed(HttpSessionEvent event); // 用戶與服務器的會話結束時觸發
  • HttpSessionAttributeListener要實現的方法:
  • attributeAdded(HttpSessionBindingEvent event) ; // 向session范圍內添加屬性時觸發
  • attributeRemoved(HttpSessionBindingEvent event); // 刪除session范圍內某個屬性時觸發
  • attributeReplaced(HttpSessionBindingEvent event); // 替換session范圍內某個屬性值時觸發
  • 如何得到session對象
    HttpSession session = event.getSession();
  • 代碼片
  • @WebListener public class MySessionListener implements HttpSessionListener,HttpSessionAttributeListener {//建立session會話時觸發@Overridepublic void sessionCreated(HttpSessionEvent event) {HttpSession session = event.getSession();String sessionId = session.getId();System.out.println("建立了會話,會話ID為:"+sessionId);}@Overridepublic void sessionDestroyed(HttpSessionEvent event) {// ... }//向session范圍內添加屬性時觸發@Overridepublic void attributeAdded(HttpSessionBindingEvent event) {String name = event.getName();Object val = event.getValue();System.out.println("向session范圍內添加了名為:"+name+",值為:"+val+"的屬性");}@Overridepublic void attributeRemoved(HttpSessionBindingEvent event) {// ... }@Overridepublic void attributeReplaced(HttpSessionBindingEvent event) {// ... }}

    總結

    以上是生活随笔為你收集整理的Java如何使用Listener的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。