1、監(jiān)聽器的啟動(dòng)優(yōu)先級(jí)順序:監(jiān)聽器 > 過(guò)濾器 > servlet
2、根據(jù)監(jiān)聽域劃分監(jiān)聽器:
(1)監(jiān)聽ServletContext,實(shí)現(xiàn)ServletContextListener接口。
兩個(gè)事件處理方法:contextInitialized、contextDestroyed。
主要用途:定時(shí)器、全局屬性對(duì)象
(2)監(jiān)聽HttpSession,實(shí)現(xiàn)HttpSessionListener接口。
兩個(gè)事件處理方法:sessionCreated、sessionDestroyed。
主要用途:統(tǒng)計(jì)在線人數(shù)、記錄訪問(wèn)日志
(3)監(jiān)聽ServletRequest,實(shí)現(xiàn)ServletRequestListener接口。
兩個(gè)事件處理方法:requestInitialized、requestDestroyed。
主要用途:讀取參數(shù)、記錄訪問(wèn)歷史
3、監(jiān)聽域?qū)ο笾械膶傩缘脑黾雍蛣h除的事件監(jiān)聽器:
(1)監(jiān)聽ServletContext屬性變化,實(shí)現(xiàn) ServletContextAttributeListener接口。
3個(gè)事件處理方法:attributeAdded、attributeRemoved、 attributeReplaced。
(2)監(jiān)聽HttpSession屬性變化,實(shí)現(xiàn)HttpSessionAttrubuteListener 接口。
3個(gè)事件處理方法:attributeAdded、attributeRemoved、 attributeReplaced。
(3)監(jiān)聽ServletRequest屬性變化,實(shí)現(xiàn) ServletRequestAttributeListener接口。
3個(gè)事件處理方法:attributeAdded、attributeRemoved、 attributeReplaced。
4、監(jiān)聽器在Web.xml中的配置:

5、綁定到HttpSession域中的某個(gè)對(duì)象狀態(tài)的監(jiān)聽:
實(shí)現(xiàn)HttpSessionBindingListener、HttpSessionActivationListener、 Serialized接口。
主要處理方法:valueBound、valueUnbound
Session正常存儲(chǔ)在服務(wù)器內(nèi)存中。Session鈍化機(jī)制本質(zhì)就在于把服務(wù)器中不經(jīng)常使用的Session對(duì)象暫時(shí)序列化到系統(tǒng)文件或數(shù)據(jù)庫(kù)系統(tǒng)中,當(dāng)被使用時(shí),反序列化到內(nèi)存中,整個(gè)過(guò)程由服務(wù)器自動(dòng)完成。
鈍化:將Session對(duì)象持久到系統(tǒng)文件上。
活化:將Session對(duì)象從系統(tǒng)文件恢復(fù)到內(nèi)存中。
服務(wù)器關(guān)閉后,把當(dāng)前Session中的監(jiān)聽對(duì)象鈍化 到 tomcat的work目錄下,下次啟動(dòng)后可以活化該文件。
6、案例:監(jiān)聽在線用戶的數(shù)量、ip等信息
(1)監(jiān)聽HttpSession:

(2)監(jiān)聽ServletRequest:
[圖片上傳中...(image-24b5ed-1525684433032-4)]
(3)web.xml配置:

(4)用戶信息類(略去get、set方法)

(5)SessionUtil類:

(6)index.jsp展示用戶信息:
[圖片上傳中...(image-8d7378-1525684433032-0)]