亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Java Web 監(jiān)聽器

系統(tǒng) 2040 0

先說一下什么是監(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è)簡單的配置

Xml代碼?? 收藏代碼
  1. < listener > ??
  2. ?????? < listener-class > listener.MySessionListener </ listener-class > ??
  3. ?? </ listener > ??

?把它放到<web-app>里

再建一個(gè)監(jiān)聽類

Java代碼?? 收藏代碼
  1. package ?listener;??
  2. ??
  3. import ?javax.servlet.http.HttpSessionEvent;??
  4. import ?javax.servlet.http.HttpSessionListener;??
  5. ??
  6. public ? class ?MySessionListener? implements ?HttpSessionListener?{??
  7. ? public ? void ?sessionCreated(HttpSessionEvent?se)?{??
  8. ?? //?當(dāng)session建立時(shí)觸發(fā) ??
  9. ??System.out.println( "當(dāng)session建立時(shí)觸發(fā)" );??
  10. ?}??
  11. ??
  12. ? public ? void ?sessionDestroyed(HttpSessionEvent?se)?{??
  13. ?? //?當(dāng)session銷毀時(shí)觸發(fā) ??
  14. ??System.out.println( "當(dāng)session銷毀時(shí)觸發(fā)" );??
  15. ?}??

?}

這樣就配置成功了,打開頁面時(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。?????
????
????????? 【程序源代碼】?????
???

Java代碼?? 收藏代碼
  1. ? //???====================???Program???Discription???=====================??? ??
  2. ? //???程序名稱:示例14-9???:???EncodingFilter???.java??? ??
  3. ? //???程序目的:學(xué)習(xí)使用監(jiān)聽器??? ??
  4. //???==============================================================??? ??
  5. ? import ???javax.servlet.http.*;?????
  6. ? import ???javax.servlet.*;?????
  7. ?????
  8. ? public ??? class ???OnLineCountListener??? implements ???HttpSessionListener,?????
  9. ServletContextListener,ServletContextAttributeListener?????
  10. ?{?????
  11. ? private ??? int ???count;?????
  12. ? private ???ServletContext???context???=??? null ;?????
  13. ?????
  14. ? public ???OnLineCountListener()?????
  15. ?{?????
  16. ?count= 0 ;?????
  17. ? //setContext();??? ??
  18. ?}?????
  19. ? //創(chuàng)建一個(gè)session時(shí)激發(fā)??? ??
  20. public ??? void ???sessionCreated(HttpSessionEvent???se)???????
  21. ?{?????
  22. ?count++;?????
  23. ?setContext(se);?????
  24. ?????
  25. ?}?????
  26. ? //當(dāng)一個(gè)session失效時(shí)激發(fā)??? ??
  27. public ??? void ???sessionDestroyed(HttpSessionEvent???se)???????
  28. ?{?????
  29. ?count--;?????
  30. ?setContext(se);?????
  31. ?}?????
  32. ? //設(shè)置context的屬性,它將激發(fā)attributeReplaced或attributeAdded方法??? ??
  33. public ??? void ???setContext(HttpSessionEvent???se)?????
  34. ?{?????
  35. ?se.getSession().getServletContext().?????
  36. etAttribute( "onLine" , new ???Integer(count));?????
  37. ?}?????
  38. ??? //增加一個(gè)新的屬性時(shí)激發(fā)??? ??
  39. public ??? void ???attributeAdded(ServletContextAttributeEvent???event)???{?????
  40. ?????
  41. ?log( "attributeAdded('" ???+???event.getName()???+??? "',???'" ???+?????
  42. ?????????event.getValue()???+??? "')" );?????
  43. ?????
  44. ?????????}?????
  45. ?????????????
  46. ??????? //刪除一個(gè)新的屬性時(shí)激發(fā)??? ??
  47. ???????? public ??? void ???attributeRemoved(ServletContextAttributeEvent???event)???{?????
  48. ?????
  49. ?log( "attributeRemoved('" ???+???event.getName()???+??? "',???'" ???+?????
  50. ?????????event.getValue()???+??? "')" );?????
  51. ?????
  52. ?????????}?????
  53. ?????
  54. ? //屬性被替代時(shí)激發(fā)??? ??
  55. ???????? public ??? void ???attributeReplaced(ServletContextAttributeEvent???event)???{?????
  56. ?????
  57. ?log( "attributeReplaced('" ???+???event.getName()???+??? "',???'" ???+?????
  58. ?????????event.getValue()???+??? "')" );?????
  59. ?????????}?????
  60. ????????? //context刪除時(shí)激發(fā)??? ??
  61. ?????????? public ??? void ???contextDestroyed(ServletContextEvent???event)???{?????
  62. ?????
  63. ?log( "contextDestroyed()" );?????
  64. ? this .context???=??? null ;?????
  65. ?????
  66. ?????????}?????
  67. ?????
  68. ????????? //context初始化時(shí)激發(fā)??? ??
  69. ???????? public ??? void ???contextInitialized(ServletContextEvent???event)???{?????
  70. ?????
  71. ? this .context???=???event.getServletContext();?????
  72. ?log( "contextInitialized()" );?????
  73. ?????
  74. ?????????}?????
  75. ????????? private ??? void ???log(String???message)???{?????
  76. ?????
  77. ?????????System.out.println( "ContextListener:???" ???+???message);?????
  78. ?????????}???????????
  79. ?}?????

?????
?????????? 【程序注解】 ???
????????? 在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)行配置,如下所示:?????
????
?

Xml代碼?? 收藏代碼
  1. < listener > ?????
  2. ?????????????????? < listener-class > OnLineCountListener </ listener-class > ?????
  3. ?????????? </ listener > ?????

?????
? 測試程序:?????
????
?

Html代碼?? 收藏代碼
  1. < %@???page??? contentType = "text/html;charset=gb2312" ???% > ?????
  2. ??????
  3. ??目前在線人數(shù):???????
  4. ??????
  5. ?? < font ??? color = red > < %=getServletContext().getAttribute("onLine")% > </ font > < br > ?????
  6. ??????
  7. ??退出會(huì)話:???????
  8. ??????
  9. ?? < form ??? action = "exit.jsp" ??? method = post > ?????
  10. ?? < input ??? type = submit ??? value = "exit" > ?????
  11. ?? </ form > ?????
  12. ??????
  13. ??getServletContext().getAttribute("onLine")獲得了count的具體值。客戶端調(diào)用???????
  14. ??????
  15. ?? < %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ù)器端有以下輸出:?????
????
?

Java代碼?? 收藏代碼
  1. …?????
  2. ??ContextListener:???contextInitialized()?????
  3. ??ContextListener:???attributeReplaced('org.apache.?????
  4. ??catalina.WELCOME_FILES ',???' [Ljava.lang.String; @1d98a ')?????
  5. ??…?????
  6. ??ContextListener:???attributeAdded( 'onLine' ,??? '1' )?????
  7. ??ContextListener:???attributeReplaced( 'onLine' ,??? '1' )?????
  8. ??ContextListener:???attributeReplaced( 'onLine' ,??? '0' )?????
  9. ??ContextListener:???attributeReplaced( 'onLine' ,??? '1' )?????
  10. ??ContextListener:???attributeReplaced( 'onLine' ,??? '2' ) ?

Java Web 監(jiān)聽器


更多文章、技術(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ì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 超激情碰碰碰啪在线视频 | 亚洲天堂一区在线 | 久久精品国产一区二区三区不卡 | 日本国产一区 | 欧美肥婆videos另类 | 老师邪恶影院a啦啦啦影院 老师在办公室被躁到白浆 老湿机午夜影院 | www.久久精品 | 99久久精品国产交换 | 五月四房 | 中文欧美日韩 | 中文字幕日本一区久久 | 99精品网| 日韩精品中文字幕一区三区 | 98色花堂国产精品首页 | 99热在线获取最新地址 | 日本一区中文字幕 | 国产精品夜色一区二区三区 | 久久精品国内偷自一区 | 国产欧美日韩中文久久 | 99热久久国产精品免费观看 | 在线视频综合视频免费观看 | 96精彩视频在线观看 | 国产美女午夜精品福利视频 | 国产精品日韩欧美 | 国产成人精品影院狼色在线 | 黄色毛片免费看 | 一级成人毛片免费观看欧美 | 四虎影院最新网址 | 大ji吧快给我别停受不了视频 | 久久99久久99精品观看 | 一区视频在线播放 | 国产成人欧美视频在线 | 国产日产欧美a级毛片 | 欧美性猛交xxx嘿人猛交 | 青草青草久热精品视频在线观看 | 久久国产一区二区三区 | 七月婷婷在线视频综合 | 品色视频| 97视频免费看 | 国产精品露脸张开双腿 | 亚洲va欧美 |