您好,登錄后才能下訂單哦!
小編給大家分享一下基于Listener監(jiān)聽(tīng)器生命周期的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
一、Listener生命周期
listener是web三大組件之一,是servlet監(jiān)聽(tīng)器,用來(lái)監(jiān)聽(tīng)請(qǐng)求,監(jiān)聽(tīng)服務(wù)端的操作。
listener分為:(都是接口類,必須實(shí)現(xiàn)相應(yīng)方法)
1.生命周期監(jiān)聽(tīng)器(3個(gè))
ServletContextListener requestDestroyed 在容器啟動(dòng)時(shí)被調(diào)用(在servlet被實(shí)例化前執(zhí)行) requestInitialized 在容器銷毀時(shí)調(diào)用(在servlet被銷毀后執(zhí)行) HttpSessionListener sessionCreated 在HttpSession創(chuàng)建后調(diào)用 sessionDestroyed 在HttpSession銷毀前調(diào)用(執(zhí)行session.invalidate();方法) ServletRequestListener requestDestroyed 在request對(duì)象創(chuàng)建后調(diào)用(發(fā)起請(qǐng)求) requestInitialized 在request對(duì)象銷毀前調(diào)用(請(qǐng)求結(jié)束)
2.屬性變化監(jiān)聽(tīng)器(3個(gè))
attributeAdded(ServletContextAttributeEvent event)向appliction中添加屬性時(shí)調(diào)用 attributeRemoved(ServletContextAttributeEvent event)從appliction中刪除屬性時(shí)調(diào)用 attributeReplaced(ServletContextAttributeEvent event)替換application中的屬性時(shí)調(diào)用 HttpSessionAttributeListener attributeAdded(HttpSessionBindingEvent event) attributeRemoved(HttpSessionBindingEvent event) attributeReplaced(HttpSessionBindingEvent event) ServletRequestAttributeListener attributeAdded(ServletRequestAttributeEvent event) attributeRemoved(ServletRequestAttributeEvent event) attributeReplaced(ServletRequestAttributeEvent event)
以上監(jiān)聽(tīng)器接口除了傳參不同,方法名都是一樣的。分別監(jiān)聽(tīng)application,session,request對(duì)象的屬性變化。
3.session中指定類屬性變化監(jiān)聽(tīng)器(2)
HttpSessionBindingListener valueBound(HttpSessionBindingEvent event) 當(dāng)該類實(shí)例設(shè)置進(jìn)session域中時(shí)調(diào)用 valueUnbound(HttpSessionBindingEvent event) 當(dāng)該類的實(shí)例從session域中移除時(shí)調(diào)用 HttpSessionActivationListener sessionWillPassivate(HttpSessionEvent se) sessionDidActivate(HttpSessionEvent se)
二、測(cè)試范例
1.生命周期監(jiān)聽(tīng):
ServletContentAttribute_Listener.java
public class ServletContentAttribute_Listener implements ServletContextListener { /** * ServletContextListener實(shí)現(xiàn)方法 * @param sce */ public void contextInitialized(ServletContextEvent sce) { System.out.println("ServletContextListener初始化"); } public void contextDestroyed(ServletContextEvent sce) { System.out.println("ServletContextListener銷毀"); } }
其他兩個(gè)監(jiān)聽(tīng)器類似,不在重復(fù)貼出。
在web.xml中配置
<!-- 監(jiān)聽(tīng)器 --> <!-- servlet監(jiān)聽(tīng)器 --> <listener> <listener-class>study.myListener.ServletContentAttribute_Listener</listener-class> </listener> <!-- session監(jiān)聽(tīng)器 --> <listener> <listener-class>study.myListener.HttpSessionAttribute_Listener</listener-class> </listener> <!-- request監(jiān)聽(tīng)器--> <listener> <listener-class>study.myListener.ServletRequestAttribute_Listener</listener-class> </listener>
運(yùn)行結(jié)果:
2.屬性監(jiān)聽(tīng):
ServletContentAttribute_Listener.java
public class ServletContentAttribute_Listener implements ServletContextAttributeListener{ /** * ServletContextAttributeListener實(shí)現(xiàn)方法 * @param event */ public void attributeAdded(ServletContextAttributeEvent event) { String meg = MessageFormat.format("ServletContent添加屬性:{0},屬性值:{1}",event.getName(),event.getValue()); System.out.println(meg); } public void attributeRemoved(ServletContextAttributeEvent event) { String meg = MessageFormat.format("ServletContent刪除屬性:{0},屬性值:{1}",event.getName(),event.getValue()); System.out.println(meg); } public void attributeReplaced(ServletContextAttributeEvent event) { String meg = MessageFormat.format("ServletContent替換屬性:{0},屬性值:{1}",event.getName(),event.getValue()); System.out.println(meg); } }
另外兩個(gè)監(jiān)聽(tīng)器類似,不在贅訴。接下來(lái)用jsp頁(yè)面測(cè)試
listenerDemo.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2017/10/17 Time: 15:28 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>監(jiān)聽(tīng)器設(shè)置</title> </head> <body> <% /** * servlet監(jiān)聽(tīng) */ application.setAttribute("name","changxiang"); application.setAttribute("name","小Cai先森"); application.removeAttribute("name"); /** * session監(jiān)聽(tīng) */ session.setAttribute("sessionName","changxiang"); session.setAttribute("sessionName","小Cai先森"); session.removeAttribute("sessionName"); session.invalidate(); /** * request監(jiān)聽(tīng) */ request.setAttribute("requestName","changxiang"); request.setAttribute("requestName","小Cai先森"); request.removeAttribute("requestName"); %> </body> </html>
執(zhí)行結(jié)果如下:
注意:其中遇到一個(gè)問(wèn)題:就是在啟動(dòng)tomcat的時(shí)候servletcontextListener監(jiān)聽(tīng)執(zhí)行了兩次,最后刪除掉server.xml中 Context 的手動(dòng)配置,這樣就不會(huì)加載兩次了。
看完了這篇文章,相信你對(duì)“基于Listener監(jiān)聽(tīng)器生命周期的示例分析”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。