先說一下什么是監(jiān)聽器,監(jiān)聽器也叫Listener,是Servlet的監(jiān)聽器,它可以監(jiān)聽客戶端的請求、服務(wù)端的操作等。通過監(jiān)聽器,可
以自動(dòng)激發(fā)一些操作,比如監(jiān)聽在線的用戶的數(shù)量。當(dāng)增加一個(gè)HttpSession時(shí),就激發(fā)sessionCreated(HttpSessionEvent?? se)方法,這樣
就可以給在線人數(shù)加1。常用的監(jiān)聽接口有以下幾個(gè):
ServletContextAttributeListener監(jiān)聽對(duì)ServletContext屬性的操作,比如增加、刪除、修改屬性。??
ServletContextListener監(jiān)聽ServletContext。當(dāng)創(chuàng)建ServletContext時(shí),激發(fā)contextInitialized(ServletContextEvent?? sce)方法;當(dāng)銷
毀ServletContext時(shí),激發(fā)contextDestroyed(ServletContextEvent?? sce)方法。??
HttpSessionListener監(jiān)聽HttpSession的操作。當(dāng)創(chuàng)建一個(gè)Session時(shí),激發(fā)session?? Created(HttpSessionEvent?? se)方法;當(dāng)銷毀一個(gè)
Session時(shí),激發(fā)sessionDestroyed?? (HttpSessionEvent?? se)方法。??
HttpSessionAttributeListener監(jiān)聽HttpSession中的屬性的操作。當(dāng)在Session增加一個(gè)屬性時(shí),激發(fā)attributeAdded
(HttpSessionBindingEvent?? se)?? 方法;當(dāng)在Session刪除一個(gè)屬性時(shí),激發(fā)attributeRemoved(HttpSessionBindingEvent?? se)方法;當(dāng)
在Session屬性被重新設(shè)置時(shí),激發(fā)attributeReplaced(HttpSessionBindingEvent?? se)?? 方法。?
下面帖上一個(gè)簡單的配置
- < listener > ??
- ?????? < listener-class > listener.MySessionListener </ listener-class > ??
- ?? </ listener > ??
?把它放到<web-app>里
再建一個(gè)監(jiān)聽類
- package ?listener;??
- ??
- import ?javax.servlet.http.HttpSessionEvent;??
- import ?javax.servlet.http.HttpSessionListener;??
- ??
- public ? class ?MySessionListener? implements ?HttpSessionListener?{??
- ? public ? void ?sessionCreated(HttpSessionEvent?se)?{??
- ?? //?當(dāng)session建立時(shí)觸發(fā) ??
- ??System.out.println( "當(dāng)session建立時(shí)觸發(fā)" );??
- ?}??
- ??
- ? public ? void ?sessionDestroyed(HttpSessionEvent?se)?{??
- ?? //?當(dāng)session銷毀時(shí)觸發(fā) ??
- ??System.out.println( "當(dāng)session銷毀時(shí)觸發(fā)" );??
- ?}??
?}
這樣就配置成功了,打開頁面時(shí)就會(huì)輸出當(dāng)session建立時(shí)觸發(fā),退出時(shí)輸出當(dāng)session銷毀時(shí)觸發(fā)!
下面我們開發(fā)一個(gè)
具體的例子
,這個(gè)監(jiān)聽器能夠統(tǒng)計(jì)在線的人數(shù)。在ServletContext初始化和銷毀時(shí),在服務(wù)器控制臺(tái)打印對(duì)應(yīng)的信息。當(dāng)ServletContext里的屬性增加、改變、刪除時(shí),在服務(wù)器控制臺(tái)打印對(duì)應(yīng)的信息。??
????????? 要獲得以上的功能,監(jiān)聽器必須實(shí)現(xiàn)以下3個(gè)接口:???
??????????????
? HttpSessionListener???
??????????????
? ServletContextListener???
??????????????
? ServletContextAttributeListener?????
????
????????? 我們看具體的代碼,見示例14-9。?????
????
????????? 【程序源代碼】?????
???
- ? //???====================???Program???Discription???=====================??? ??
- ? //???程序名稱:示例14-9???:???EncodingFilter???.java??? ??
- ? //???程序目的:學(xué)習(xí)使用監(jiān)聽器??? ??
- //???==============================================================??? ??
- ? import ???javax.servlet.http.*;?????
- ? import ???javax.servlet.*;?????
- ?????
- ? public ??? class ???OnLineCountListener??? implements ???HttpSessionListener,?????
- ServletContextListener,ServletContextAttributeListener?????
- ?{?????
- ? private ??? int ???count;?????
- ? private ???ServletContext???context???=??? null ;?????
- ?????
- ? public ???OnLineCountListener()?????
- ?{?????
- ?count= 0 ;?????
- ? //setContext();??? ??
- ?}?????
- ? //創(chuàng)建一個(gè)session時(shí)激發(fā)??? ??
- public ??? void ???sessionCreated(HttpSessionEvent???se)???????
- ?{?????
- ?count++;?????
- ?setContext(se);?????
- ?????
- ?}?????
- ? //當(dāng)一個(gè)session失效時(shí)激發(fā)??? ??
- public ??? void ???sessionDestroyed(HttpSessionEvent???se)???????
- ?{?????
- ?count--;?????
- ?setContext(se);?????
- ?}?????
- ? //設(shè)置context的屬性,它將激發(fā)attributeReplaced或attributeAdded方法??? ??
- public ??? void ???setContext(HttpSessionEvent???se)?????
- ?{?????
- ?se.getSession().getServletContext().?????
- etAttribute( "onLine" , new ???Integer(count));?????
- ?}?????
- ??? //增加一個(gè)新的屬性時(shí)激發(fā)??? ??
- public ??? void ???attributeAdded(ServletContextAttributeEvent???event)???{?????
- ?????
- ?log( "attributeAdded('" ???+???event.getName()???+??? "',???'" ???+?????
- ?????????event.getValue()???+??? "')" );?????
- ?????
- ?????????}?????
- ?????????????
- ??????? //刪除一個(gè)新的屬性時(shí)激發(fā)??? ??
- ???????? public ??? void ???attributeRemoved(ServletContextAttributeEvent???event)???{?????
- ?????
- ?log( "attributeRemoved('" ???+???event.getName()???+??? "',???'" ???+?????
- ?????????event.getValue()???+??? "')" );?????
- ?????
- ?????????}?????
- ?????
- ? //屬性被替代時(shí)激發(fā)??? ??
- ???????? public ??? void ???attributeReplaced(ServletContextAttributeEvent???event)???{?????
- ?????
- ?log( "attributeReplaced('" ???+???event.getName()???+??? "',???'" ???+?????
- ?????????event.getValue()???+??? "')" );?????
- ?????????}?????
- ????????? //context刪除時(shí)激發(fā)??? ??
- ?????????? public ??? void ???contextDestroyed(ServletContextEvent???event)???{?????
- ?????
- ?log( "contextDestroyed()" );?????
- ? this .context???=??? null ;?????
- ?????
- ?????????}?????
- ?????
- ????????? //context初始化時(shí)激發(fā)??? ??
- ???????? public ??? void ???contextInitialized(ServletContextEvent???event)???{?????
- ?????
- ? this .context???=???event.getServletContext();?????
- ?log( "contextInitialized()" );?????
- ?????
- ?????????}?????
- ????????? private ??? void ???log(String???message)???{?????
- ?????
- ?????????System.out.println( "ContextListener:???" ???+???message);?????
- ?????????}???????????
- ?}?????
?????
??????????
【程序注解】
???
????????? 在OnLineCountListener里,用count代表當(dāng)前在線的人數(shù),OnLineCountListener將在Web服務(wù)器啟動(dòng)時(shí)自動(dòng)執(zhí)行。當(dāng)
OnLineCountListener構(gòu)造好后,把count設(shè)置為0。每增加一個(gè)Session,OnLineCountListener會(huì)自動(dòng)調(diào)用sessionCreated(HttpSessionEvent?
?se)方法;每銷毀一個(gè)Session,OnLineCountListener會(huì)自動(dòng)調(diào)用sessionDestroyed(HttpSessionEvent?? se)方法。當(dāng)調(diào)用sessionCreated
(HttpSessionEvent?? se)方法時(shí),說明又有一個(gè)客戶在請求,此時(shí)使在線的人數(shù)(count)加1,并且把count寫到ServletContext中。
ServletContext的信息是所有客戶端共享的,這樣,每個(gè)客戶端都可以讀取到當(dāng)前在線的人數(shù)。?????
????
? 為了使監(jiān)聽器生效,需要在web.xml里進(jìn)行配置,如下所示:?????
????
?
- < listener > ?????
- ?????????????????? < listener-class > OnLineCountListener </ listener-class > ?????
- ?????????? </ listener > ?????
?????
? 測試程序:?????
????
?
- < %@???page??? contentType = "text/html;charset=gb2312" ???% > ?????
- ??????
- ??目前在線人數(shù):???????
- ??????
- ?? < font ??? color = red > < %=getServletContext().getAttribute("onLine")% > </ font > < br > ?????
- ??????
- ??退出會(huì)話:???????
- ??????
- ?? < form ??? action = "exit.jsp" ??? method = post > ?????
- ?? < input ??? type = submit ??? value = "exit" > ?????
- ?? </ form > ?????
- ??????
- ??getServletContext().getAttribute("onLine")獲得了count的具體值。客戶端調(diào)用???????
- ??????
- ?? < %session.invalidate()???;% > ?????
?
????
????????? 使Session失效,這樣監(jiān)聽器就會(huì)使count減1。?????
????
????????? 【運(yùn)行程序】???
????????? web.xml做好以上的配置,把OnLineCountListener放在WEB-INF/class目錄下,啟動(dòng)Web服務(wù)器,在瀏覽器里輸入以下URL(根據(jù)具體
情況不同):
http://127.0.0.1:8080/ch14/listener.jsp
?????
????
????????? 瀏覽器將會(huì)打印目前在線人數(shù)。在服務(wù)器端有以下輸出:?????
????
?
- …?????
- ??ContextListener:???contextInitialized()?????
- ??ContextListener:???attributeReplaced('org.apache.?????
- ??catalina.WELCOME_FILES ',???' [Ljava.lang.String; @1d98a ')?????
- ??…?????
- ??ContextListener:???attributeAdded( 'onLine' ,??? '1' )?????
- ??ContextListener:???attributeReplaced( 'onLine' ,??? '1' )?????
- ??ContextListener:???attributeReplaced( 'onLine' ,??? '0' )?????
- ??ContextListener:???attributeReplaced( 'onLine' ,??? '1' )?????
- ??ContextListener:???attributeReplaced( 'onLine' ,??? '2' ) ?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
