Java中監(jiān)聽器Listener詳解
1、Listener
- 是由Java編寫的WEB組件,主要完成對內(nèi)置對象狀態(tài)的變化 (創(chuàng)建、銷毀)和屬性的變化 進(jìn)行監(jiān)聽,做進(jìn)一步的處理
- 作用:主要對session和application內(nèi)置對象監(jiān)聽。
2、對application監(jiān)聽
package cn.mldn.lxh.listener ; import javax.servlet.* ; public class ServletContextDemo implements ServletContextListener,ServletContextAttributeListener { private ServletContext application = null ; // 實(shí)現(xiàn)方法 public void contextInitialized(ServletContextEvent sce) { this.application = sce.getServletContext() ; System.out.println("** 上下文初始化 ...") ; System.out.println("** 當(dāng)前虛擬目錄的絕對路徑:"+this.application.getRealPath("/")) ; } public void contextDestroyed(ServletContextEvent sce) { System.out.println("** 上下文銷毀 ...") ; } public void attributeAdded(ServletContextAttributeEvent scab) { System.out.println("** 增加屬性:"+scab.getName()+" --> "+scab.getValue()) ; } public void attributeRemoved(ServletContextAttributeEvent scab) { System.out.println("** 刪除屬性:"+scab.getName()+" --> "+scab.getValue()) ; } public void attributeReplaced(ServletContextAttributeEvent scab) { System.out.println("** 替換屬性:"+scab.getName()+" --> "+scab.getValue()) ; } }; /* <listener> <listener-class>cn.mldn.lxh.listener.ServletContextDemo</listener-class> </listener> */
1、語法 :
- 是WEB組件,一定要有包聲明 package cn.mldn.lxh.listener ;
- 包導(dǎo)入 javax.servlet
- 實(shí)現(xiàn)接口 ServletContextListener ,ServletContextAttributeListener
- 實(shí)現(xiàn)接口的方法
2、實(shí)現(xiàn)方法
實(shí)現(xiàn)的兩個接口,一個是為了實(shí)現(xiàn)對application狀態(tài)監(jiān)聽的方法,一個是為了實(shí)現(xiàn)對applciation屬性的變化監(jiān)聽的方法。如下:
- 對applicaiton的狀態(tài)做監(jiān)聽 implements ServletContextListener ,實(shí)現(xiàn)兩個方法:
public void contextInitialized(ServletContextEvent sce);
tomcat一啟動,先創(chuàng)建出監(jiān)聽器,對application的創(chuàng)建和銷毀做監(jiān)聽。當(dāng)application一創(chuàng)建,被監(jiān)聽器監(jiān)聽到,就調(diào)用contextInitialized()方法;
public void contextDestroyed(ServletContextEvent sce);
當(dāng)application一銷毀,監(jiān)聽器就會監(jiān)聽到applicaiton銷毀,調(diào)用contextDestoryed();
對applicaiton里屬性的變化做監(jiān)聽 , implements ServletContextAttributeListener
public void attributeAdded(ServletContextAttributeEvent scab) public void attributeRemoved(ServletContextAttributeEvent scab) public void attributeReplaced(ServletContextAttributeEvent scab)
通過實(shí)現(xiàn)ServletContextAttributeListener 的三個方法,對applcation的屬性變化做監(jiān)聽。注意:監(jiān)聽到的都是已經(jīng)完成的變化 。比如往application里設(shè)值屬性、刪除屬性、替換屬性值等,這些都會被監(jiān)聽器監(jiān)聽到??梢酝ㄟ^方法獲得增加的屬性名和屬性值,獲得刪除的屬性名和屬性值,獲得替換的屬性名和替換前的屬性值(是被替換的,而不是替換后的值)
3、Tomcat一啟動,創(chuàng)建了哪些內(nèi)置對象?
- tomcat一啟動,首先會創(chuàng)建監(jiān)聽器,去監(jiān)聽內(nèi)置對象的狀態(tài)和屬性變化。
- 然后會創(chuàng)建Config對象,來收集配置文件中的初始參數(shù),一共有會創(chuàng)建兩個Config對象,分別是封裝Servlet初始參數(shù)的ServletConfig,封裝Filter初始參數(shù)的FilterConfig。
- 然后會創(chuàng)建applicaiton對象
- 然后創(chuàng)建Filter
- 如果配了loadonstartup,就再創(chuàng)建servlet。
3、對session做監(jiān)聽
package cn.mldn.lxh.listener ; import javax.servlet.http.* ; public class HttpSessionDemo implements HttpSessionListener,HttpSessionAttributeListener { private HttpSession session ; // 實(shí)現(xiàn)方法 public void sessionCreated(HttpSessionEvent se) { this.session = se.getSession() ; System.out.println("** Session 創(chuàng)建 ....") ; System.out.println("** SessionID --> "+this.session.getId()) ; } public void sessionDestroyed(HttpSessionEvent se) { System.out.println("** Session 銷毀 ....") ; } public void attributeAdded(HttpSessionBindingEvent se) { System.out.println("** Session 增加屬性:"+se.getName()+" --> "+se.getValue()) ; System.out.println("** 獲得Session "+se.getSession().getId()) ; } public void attributeRemoved(HttpSessionBindingEvent se) { System.out.println("** Session 刪除屬性:"+se.getName()+" --> "+se.getValue()) ; } public void attributeReplaced(HttpSessionBindingEvent se) { System.out.println("** Session 替換屬性:"+se.getName()+" --> "+se.getValue()) ; } }; /* <listener> <listener-class>cn.mldn.lxh.listener.HttpSessionDemo</listener-class> </listener> */
包聲明包導(dǎo)入類聲明,實(shí)現(xiàn) HttpSessionListener接口,實(shí)現(xiàn)對狀態(tài)監(jiān)聽的方法實(shí)現(xiàn)HttpSessionAttributeListener接口,實(shí)現(xiàn)對屬性變化監(jiān)聽的方法
什么時候創(chuàng)建session?
我們可以通過public void sessionCreated(HttpSessionEvent se) 來監(jiān)聽session的創(chuàng)建
- 訪問服務(wù)器不會
- 訪問靜態(tài)組件不會(html)
- 只有訪問動態(tài)組件(jsp),才會創(chuàng)建請求—第一次發(fā)送請求
到此這篇關(guān)于Java中監(jiān)聽器Listener詳解的文章就介紹到這了,更多相關(guān)java 監(jiān)聽器Listener內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中的通用權(quán)限管理設(shè)計(jì)(推薦)
下面小編就為大家推薦一篇java中的通用權(quán)限管理設(shè)計(jì),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12Java MyBatis返回兩個字段作為Map的key和value問題
使用MyBatis查詢兩個字段并返回Map時,需要注意數(shù)據(jù)量和值的類型,直接返回Map會導(dǎo)致報(bào)錯,使用@MapKey注解可以生成Map,但值是對象而不是直接值,為了解決這個問題,可以自定義一個Map結(jié)果處理器MapResultHandler2024-12-12詳解Java中的reactive stream協(xié)議
Stream大家應(yīng)該都很熟悉了,java8中為所有的集合類都引入了Stream的概念。優(yōu)雅的鏈?zhǔn)讲僮?,流式處理邏輯,相信用過的人都會愛不釋手。本文將詳細(xì)介紹Java中的reactive stream協(xié)議。2021-06-06SpringBoot利用Junit動態(tài)代理實(shí)現(xiàn)Mock方法
說到Spring Boot 單元測試主要有兩個主流集成分別是Mockito,Junit,這個各有特點(diǎn),在實(shí)際開發(fā)中,我想要的測試框架應(yīng)該是這個框架集成者,本文給大家介紹了SpringBoot利用Junit動態(tài)代理實(shí)現(xiàn)Mock方法,需要的朋友可以參考下2024-04-04SpringBoot日程管理Quartz與定時任務(wù)Task實(shí)現(xiàn)詳解
定時任務(wù)是企業(yè)級開發(fā)中必不可少的組成部分,諸如長周期業(yè)務(wù)數(shù)據(jù)的計(jì)算,例如年度報(bào)表,諸如系統(tǒng)臟數(shù)據(jù)的處理,再比如系統(tǒng)性能監(jiān)控報(bào)告,還有搶購類活動的商品上架,這些都離不開定時任務(wù)。本節(jié)將介紹兩種不同的定時任務(wù)技術(shù)2022-09-09Java實(shí)現(xiàn)大文件的切割與合并操作示例
這篇文章主要介紹了Java實(shí)現(xiàn)大文件的切割與合并操作,結(jié)合實(shí)例形式分析了java基于io及util操作大文件按指定個數(shù)分割與合并相關(guān)操作技巧,需要的朋友可以參考下2018-07-07SpringBoot集成ip2region實(shí)現(xiàn)ip白名單的代碼示例
ip2region v2.0 - 是一個離線IP地址定位庫和IP定位數(shù)據(jù)管理框架,10微秒級別的查詢效率,提供了眾多主流編程語言的 xdb 數(shù)據(jù)生成和查詢客戶端實(shí)現(xiàn),本文介紹了SpringBoot集成ip2region實(shí)現(xiàn)ip白名單的代碼工程,需要的朋友可以參考下2024-08-08