小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Listener是Servlet的監(jiān)聽器

 小朋 2006-11-08
Listener是Servlet的監(jiān)聽器,它可以監(jiān)聽客戶端的請求、服務端的操作等。通過監(jiān)聽器,可以自動激發(fā)一些操作,比如監(jiān)聽在線的用戶的數(shù)量。當 增加一個HttpSession時,就激發(fā)sessionCreated(HttpSessionEvent se)方法,這樣就可以給在線人數(shù)加1。常用的監(jiān)聽接口有以下幾個:
    
  • ServletContextAttributeListener監(jiān)聽對ServletContext屬性的操作,比如增加、刪除、修改屬性。
        
  • ServletContextListener 監(jiān)聽ServletContext。當創(chuàng)建ServletContext時,激發(fā)contextInitialized (ServletContextEvent sce)方法;當銷毀ServletContext時,激發(fā)contextDestroyed(ServletContextEvent sce)方法。
        
  • HttpSessionListener 監(jiān)聽HttpSession的操作。當創(chuàng)建一個Session時,激發(fā)session Created(HttpSessionEvent se)方法;當銷毀一個Session時,激發(fā)sessionDestroyed (HttpSessionEvent se)方法。
        
  • HttpSessionAttributeListener 監(jiān)聽HttpSession中的屬性的操作。當在Session增加一個屬性時,激發(fā)attributeAdded (HttpSessionBindingEvent se) 方法;當在Session刪除一個屬性時,激發(fā)attributeRemoved(HttpSessionBindingEvent se)方法;當在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(HttpSessionEvent se)方法;每銷毀一個Session,OnLineCountListener會自動調(diào)用sessionDestroyed (HttpSessionEvent se)方法。當調(diào)用sessionCreated(HttpSessionEvent se)方法時,說明又有一個客戶在請求,此時使在線的人數(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‘)
    • 本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
      轉(zhuǎn)藏 分享 獻花(0

      0條評論

      發(fā)表

      請遵守用戶 評論公約

      類似文章 更多