一、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è))
1
2
3
4
5
6
7
8
9
|
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è))
1
2
3
4
5
6
7
8
9
10
11
|
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)
1
2
3
4
5
6
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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中配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!-- 監(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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<%-- 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ì)加載兩次了。
以上這篇基于listener監(jiān)聽(tīng)器生命周期(詳解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/caijh/archive/2017/10/17/7683007.html