生活随笔
收集整理的這篇文章主要介紹了
Java如何使用Listener
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
定義
用于監(jiān)聽Web應(yīng)用的內(nèi)部事件的實(shí)現(xiàn)類。可以監(jiān)聽用戶session的開始與結(jié)束,用戶請(qǐng)求的到達(dá)等等,當(dāng)事件發(fā)生時(shí),會(huì)回調(diào)監(jiān)聽器的內(nèi)部方法
使用Listener步驟
通過實(shí)現(xiàn)具體接口創(chuàng)建實(shí)現(xiàn)類(可實(shí)現(xiàn)多個(gè)監(jiān)聽器接口)配置實(shí)現(xiàn)類成為監(jiān)聽器,有兩種配置方式:
直接用@WebListener注解修飾實(shí)現(xiàn)類通過web.xml方式配置,代碼如下:
<listener><listener-class>com.zrgk.listener.MyListener
</lisener-class>
</listener>
常用Web事件監(jiān)聽器接口
1. ServletContextListener
該接口用于監(jiān)聽Web應(yīng)用的啟動(dòng)與關(guān)閉該接口的兩個(gè)方法:
contextInitialized(ServletContextEvent event); // 啟動(dòng)web應(yīng)用時(shí)調(diào)用contextDestroyed(ServletContextEvent event); // 關(guān)閉web應(yīng)用時(shí)調(diào)用如何獲得application對(duì)象:
ServletContext application = event.getServletContext();示例
@WebListener
public class MyServetContextListener implements ServletContextListener{@Overridepublic void contextDestroyed(ServletContextEvent event) {ServletContext application = event.getServletContext();String userName = application.getInitParameter(
"userName"); System.out.println(
"關(guān)閉web應(yīng)用的用戶名字為:"+userName);}
@Overridepublic void contextInitialized(ServletContextEvent event) {ServletContext application = event.getServletContext();String userName = application.getInitParameter(
"userName"); System.out.println(
"啟動(dòng)web應(yīng)用的用戶名字為:"+userName);}}
2. ServletContextAttributeListener
該接口 用于監(jiān)聽ServletContext范圍(application)內(nèi)屬性的改變。該接口的兩個(gè)方法:
attributeAdded(ServletContextAttributeEvent event); // 當(dāng)把一個(gè)屬性存進(jìn)application時(shí)觸發(fā)attributeRemoved(ServletContextAttributeEvent event); // 當(dāng)把一個(gè)屬性從application刪除時(shí)觸發(fā)attributeReplaced(ServletContextAttributeEvent event); // 當(dāng)替換application內(nèi)的某個(gè)屬性值時(shí)觸發(fā)如何獲得application對(duì)象:
ServletContext application = event.getServletContext();示例
@WebListener
public class MyServletContextAttributeListener implements ServletContextAttributeListener{@Overridepublic void attributeAdded(ServletContextAttributeEvent event) {String name = event.getName();Object val = event.getValue(); System.out.println(
"向application范圍內(nèi)添加了屬性名為:"+name+
",屬性值為:"+val+
"的屬性");}
@Overridepublic void attributeRemoved(ServletContextAttributeEvent event) {}
@Overridepublic void attributeReplaced(ServletContextAttributeEvent event) {}}
3. ServletRequestListener與ServletRequestAttributeListener
ServletRequestListener用于監(jiān)聽用戶請(qǐng)求,而ServletRequestAttributeListener用于監(jiān)聽request范圍內(nèi)屬性的變化。ServletRequestListener兩個(gè)需要實(shí)現(xiàn)的方法
requestInitialized(ServletRequestEvent event); //用戶請(qǐng)求到達(dá)、被初始化時(shí)觸發(fā)requestDestroyed(ServletRequestEvent event); // 用戶請(qǐng)求結(jié)束、被銷毀時(shí)觸發(fā)ServletRequestAttributeListener兩個(gè)需要實(shí)現(xiàn)的方法
attributeAdded(ServletRequestAttributeEvent event); // 向request范圍內(nèi)添加屬性時(shí)觸發(fā)attributeRemoved(ServletRequestAttributeEvent event); // 從request范圍內(nèi)刪除某個(gè)屬性時(shí)觸發(fā)attributeReplaced(ServletRequestAttributeEvent event); // 替換request范圍內(nèi)某個(gè)屬性值時(shí)觸發(fā)獲取reqeust對(duì)象
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+
"的用戶發(fā)送到"+req.getRequestURI()+
"的請(qǐng)求結(jié)束");}
@Overridepublic void requestInitialized(ServletRequestEvent event) {HttpServletRequest req = (HttpServletRequest) event.getServletRequest();String ip = req.getRemoteAddr();System.out.println(
"IP為:"+ip+
"的用戶發(fā)送到"+req.getRequestURI()+
"的請(qǐng)求被初始化");}
@Overridepublic void attributeAdded(ServletRequestAttributeEvent event) {String name = event.getName();Object val = event.getValue();System.out.println(
"向request范圍內(nèi)添加了名為:"+name+
",值為:"+val+
"的屬性");}
@Overridepublic void attributeRemoved(ServletRequestAttributeEvent event) {}
@Overridepublic void attributeReplaced(ServletRequestAttributeEvent event) {}
}
4. HttpSessionListener與HttpSessionAttributeListener
HttpSessionListener監(jiān)聽用戶session的開始與結(jié)束,而HttpSessionAttributeListener監(jiān)聽HttpSession范圍(session)內(nèi)的屬性的改變。HttpSessionListener要實(shí)現(xiàn)的方法:
sessionCreated(HttpSessionEvent event); // 用戶與服務(wù)器的會(huì)話開始、創(chuàng)建時(shí)觸發(fā)sessionDestroyed(HttpSessionEvent event); // 用戶與服務(wù)器的會(huì)話結(jié)束時(shí)觸發(fā)HttpSessionAttributeListener要實(shí)現(xiàn)的方法:
attributeAdded(HttpSessionBindingEvent event) ; // 向session范圍內(nèi)添加屬性時(shí)觸發(fā)attributeRemoved(HttpSessionBindingEvent event); // 刪除session范圍內(nèi)某個(gè)屬性時(shí)觸發(fā)attributeReplaced(HttpSessionBindingEvent event); // 替換session范圍內(nèi)某個(gè)屬性值時(shí)觸發(fā)如何得到session對(duì)象
HttpSession session = event.getSession();代碼片
@WebListener
public class MySessionListener implements HttpSessionListener,HttpSessionAttributeListener {@Overridepublic void sessionCreated(HttpSessionEvent event) {HttpSession session = event.getSession();String sessionId = session.getId();System.out.println(
"建立了會(huì)話,會(huì)話ID為:"+sessionId);}
@Overridepublic void sessionDestroyed(HttpSessionEvent event) {}
@Overridepublic void attributeAdded(HttpSessionBindingEvent event) {String name = event.getName();Object val = event.getValue();System.out.println(
"向session范圍內(nèi)添加了名為:"+name+
",值為:"+val+
"的屬性");}
@Overridepublic void attributeRemoved(HttpSessionBindingEvent event) {}
@Overridepublic void attributeReplaced(HttpSessionBindingEvent event) {}}
總結(jié)
以上是生活随笔為你收集整理的Java如何使用Listener的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。