免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Listener是Servlet的監(jiān)聽器
Listener是Servlet的監(jiān)聽器,它可以監(jiān)聽客戶端的請求、服務端的操作等。通過監(jiān)聽器,可以自動激發(fā)一些操作,比如監(jiān)聽在線的用戶的數(shù)量。當增加一個HttpSession時,就激發(fā)sessionCreated(HttpSessionEventse)方法,這樣就可以給在線人數(shù)加1。常用的監(jiān)聽接口有以下幾個:
ServletContextAttributeListener監(jiān)聽對ServletContext屬性的操作,比如增加、刪除、修改屬性。
ServletContextListener監(jiān)聽ServletContext。當創(chuàng)建ServletContext時,激發(fā)contextInitialized(ServletContextEventsce)方法;當銷毀ServletContext時,激發(fā)contextDestroyed(ServletContextEvent sce)方法。
HttpSessionListener監(jiān)聽HttpSession的操作。當創(chuàng)建一個Session時,激發(fā)session Created(HttpSessionEventse)方法;當銷毀一個Session時,激發(fā)sessionDestroyed (HttpSessionEvent se)方法。
HttpSessionAttributeListener監(jiān)聽HttpSession中的屬性的操作。當在Session增加一個屬性時,激發(fā)attributeAdded(HttpSessionBindingEvent se)方法;當在Session刪除一個屬性時,激發(fā)attributeRemoved(HttpSessionBindingEventse)方法;當在Session屬性被重新設置時,激發(fā)attributeReplaced(HttpSessionBindingEvent se)方法。
下面我們開發(fā)一個具體的例子,這個監(jiān)聽器能夠統(tǒng)計在線的人數(shù)。在ServletContext初始化和銷毀時,在服務器控制臺打印對應的信息。當ServletContext里的屬性增加、改變、刪除時,在服務器控制臺打印對應的信息。
要獲得以上的功能,監(jiān)聽器必須實現(xiàn)以下3個接口:
HttpSessionListener
ServletContextListener
ServletContextAttributeListener
我們看具體的代碼,見示例14-9。
【程序源代碼】
1 // ==================== Program Discription =====================2 // 程序名稱:示例14-9 : EncodingFilter .java3 // 程序目的:學習使用監(jiān)聽器4 // ==============================================================5 import javax.servlet.http.*;6 import javax.servlet.*;78 public class OnLineCountListener implements HttpSessionListener,ServletContextListener,ServletContextAttributeListener9 {10 private int count;11 private ServletContext context = null;12 13 public OnLineCountListener()14 {15 count=0;16 //setContext();17 }18 //創(chuàng)建一個session時激發(fā)19 public void sessionCreated(HttpSessionEvent se) 20 {21 count++;22 setContext(se);23 24 }25 //當一個session失效時激發(fā)26 public void sessionDestroyed(HttpSessionEvent se) 27 {28 count--;29 setContext(se);30 }31 //設置context的屬性,它將激發(fā)attributeReplaced或attributeAdded方法32 public void setContext(HttpSessionEvent se)33 {34 se.getSession().getServletContext().setAttribute("onLine",new Integer(count));35 }36 //增加一個新的屬性時激發(fā)37 public void attributeAdded(ServletContextAttributeEvent event) {38 39 log("attributeAdded(‘" + event.getName() + "‘, ‘" +40 event.getValue() + "‘)");41 42 }43 44 //刪除一個新的屬性時激發(fā)45 public void attributeRemoved(ServletContextAttributeEvent event) {4647 log("attributeRemoved(‘" + event.getName() + "‘, ‘" +48 event.getValue() + "‘)");49 50 }5152 //屬性被替代時激發(fā)53 public void attributeReplaced(ServletContextAttributeEvent event) {54 55 log("attributeReplaced(‘" + event.getName() + "‘, ‘" +56 event.getValue() + "‘)");57 }58 //context刪除時激發(fā)59 public void contextDestroyed(ServletContextEvent event) {60 61 log("contextDestroyed()");62 this.context = null;63 64 }65 66 //context初始化時激發(fā)67 public void contextInitialized(ServletContextEvent event) {68 69 this.context = event.getServletContext();70 log("contextInitialized()");71 72 }73 private void log(String message) {74 75 System.out.println("ContextListener: " + message);76 } 77 }
【程序注解】
在OnLineCountListener里,用count代表當前在線的人數(shù),OnLineCountListener將在Web服務器啟動時自動執(zhí)行。當OnLineCountListener構(gòu)造好后,把count設置為0。每增加一個Session,OnLineCountListener會自動調(diào)用sessionCreated(HttpSessionEventse)方法;每銷毀一個Session,OnLineCountListener會自動調(diào)用sessionDestroyed(HttpSessionEvent se)方法。當調(diào)用sessionCreated(HttpSessionEventse)方法時,說明又有一個客戶在請求,此時使在線的人數(shù)(count)加1,并且把count寫到ServletContext中。ServletContext的信息是所有客戶端共享的,這樣,每個客戶端都可以讀取到當前在線的人數(shù)。
為了使監(jiān)聽器生效,需要在web.xml里進行配置,如下所示:
<listener> <listener-class>OnLineCountListener</listener-class> </listener>
測試程序:
<%@ page contentType="text/html;charset=gb2312" %>
目前在線人數(shù):
<font color=red><%=getServletContext().getAttribute("onLine")%></font><br>
退出會話:
<form action="exit.jsp" method=post><input type=submit value="exit"></form>
getServletContext().getAttribute("onLine")獲得了count的具體值。客戶端調(diào)用
<%session.invalidate() ;%>
使Session失效,這樣監(jiān)聽器就會使count減1。
【運行程序】
web.xml做好以上的配置,把OnLineCountListener放在WEB-INF/class目錄下,啟動Web服務器,在瀏覽器里輸入以下URL(根據(jù)具體情況不同):http://127.0.0.1:8080/ch14/listener.jsp
瀏覽器將會打印目前在線人數(shù)。在服務器端有以下輸出:
…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‘)
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
JavaWEB開發(fā)-Servlet事件監(jiān)聽器
J2EE筆記(二) — Java web
Java中的Listener 監(jiān)聽器
Servlet過濾器和監(jiān)聽器知識總結(jié)
web.xml中l(wèi)istener的作用及使用(轉(zhuǎn))
關于JSP.Servlet啟動次序的說明
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服