隨筆-124  評(píng)論-49  文章-56  trackbacks-0

          servlet監(jiān)聽(tīng)器
          在WEB應(yīng)用中響應(yīng)特定對(duì)象的特定事件
          好處:更方便的控制application,session,request對(duì)象的發(fā)生的特定事件
               可以集中處理特定的事件
          -------------------------------------------------------------------
          HttpSession監(jiān)聽(tīng)器接口
          1 HttpSessionListener(需要配web.xml)
          2 HttpSessionAttributeListener(需要配web.xml)
          3 HttpSessionBindingListener
          4 HttpSessionActivationListener(需要配web.xml)
          -------------------------------------------------------------------
          HttpSessionListener
          1 sessionCreated()//當(dāng)session對(duì)象產(chǎn)生的時(shí)候調(diào)用的方法
          2 sessionDestroyed()//當(dāng)session對(duì)象消滅的時(shí)候調(diào)用的方法
          web.xml配置
          <listener>
           <listener-class>webbook.chapter14.CounterListener(對(duì)應(yīng)的實(shí)現(xiàn)類(lèi))
           </listener-class>
          </listener>
          ---------------------------------------------------------------------
          HttpSessionAttributeListener
          1 attributeAdded()//添加時(shí)調(diào)用
          2 attributeReplaced()//替換時(shí)調(diào)用
          3 attributeRemoved()//刪除時(shí)調(diào)用
          產(chǎn)生事件的代碼
          session.setAttribute("username","wangwu");
          session.setAttribute("username","lisi");
          session.removeAttribute("username");
          -----------------------------------------------------------------
          HttpSessionBindingListener
          1 valueBound();
          2 vaoueUnbound();
          調(diào)用機(jī)制
          如果一個(gè)類(lèi)實(shí)現(xiàn)了HttpSessionBindingListener接口,則當(dāng)這個(gè)類(lèi)的對(duì)象通過(guò)session.setAttribute()被綁定到Session對(duì)象中時(shí),
          則該對(duì)象的valueBound()方法被自動(dòng)調(diào)用,當(dāng)這個(gè)對(duì)象從Session中刪除時(shí)(調(diào)用session.invalidate()和session.removeAtrribute()
          方法,或Session對(duì)象過(guò)期)valueUnbound()方法將被自動(dòng)調(diào)用.
          --------------------------------------------------------------------------------------------------------
          HttpSessionActivationListener
          1 sessionDidActivate();
          2 sessionWillPassivate();
          ---------------------------------------------------------------------------------
          例子:
          1 建立CounterListener處理類(lèi)
          import javax.servlet.http.HttpSessionEvent;
          import javax.servlet.http.HttpSessionListener;
          public class CounterListener implements HttpSessionListener {

           private static long onlineNumber=0;
           public void sessionCreated(HttpSessionEvent se) {
            onlineNumber++;
           }

           public void sessionDestroyed(HttpSessionEvent se) {
            onlineNumber--;
           }
           
           public static long getOnlineNumber(){
            return onlineNumber;
           }
          }
          2 配置web.xml
          <listener>
              <listener-class>com.tags.CounterListener</listener-class>
          </listener>
          3 JSP頁(yè)面
          <%@page import="com.tags.CounterListener;"%>
          <body>
              當(dāng)前的在線(xiàn)人數(shù):<%=CounterListener.getOnlineNumber() %> <br>
          </body>
          ----------------------------------------------------------------------------
          ServletContext監(jiān)聽(tīng)器接口
          ServletContextListener
          1 contextInitialized()
          2 contextDestroyed()
          調(diào)用機(jī)制
          當(dāng)在web應(yīng)用中部署了實(shí)現(xiàn)該接口的實(shí)現(xiàn)類(lèi)后,在WEB容器加載WEB應(yīng)用時(shí)(如:啟動(dòng)服務(wù)器)就會(huì)自動(dòng)調(diào)用contextInitialized()方法,
          而當(dāng)WEB容器銷(xiāo)毀WEB應(yīng)用時(shí)(如:關(guān)閉服務(wù)器),會(huì)自動(dòng)調(diào)用contextDestroyed()方法.
          -----------------------------------------------------------------------------------
          ServletContextAttributeListener
          1 attributeAdded()
          2 attributeReplaced()
          3 attributeRemoved()
          觸發(fā)事件的代碼
          context.setAttribute("counter",new Integer(0));
          context.setAttribute("counter",new Integer(100));
          context.removeAttribute("counter");
          -----------------------------------------------------------
          HttpServletRequest監(jiān)聽(tīng)器
          ServletRequestListener
          1 requestInitialized();
          2 requestDestroyed()
          調(diào)用機(jī)制
          與ServletContextListener接口類(lèi)似,在web應(yīng)用中部署實(shí)現(xiàn)該接口的實(shí)現(xiàn)類(lèi)扣,在HttpServletRequest對(duì)象(JSP中的request對(duì)象)建立
          或者被銷(xiāo)毀時(shí),由web容器自動(dòng)調(diào)用requestInitialized()或者requestDestroyed()方法.
          --------------------------------------------------------------------------------------
          ServletRequestAttributeListener
          1 attributeAdded();
          2 attributeReplaced();
          3 attributeRemoved();
          觸發(fā)事件的代碼
          request.setAttribute("username","lisi");
          request.setAttribute("username","wangwu");
          request.removeAttribute("username");

          ******************************************************************
          public class ListenerOnline implements HttpSessionLinstener,ServletContext{
            private  application;
            public void sessionCreated(HttpSessionEvent event){
              Integer n=(Integer)application.getAttrabute("z",1000);
              application.setAttrabute("z",n.IntValue()+1);
             
            }
             public void sessionDestroyed(HttpSessionEvent event){
            }
            public void contextInitialized(ServletContextEvent event){
              application=event.getServletContext();
              application.setAttrabute("z",1000);
            }
             public void contextDestroyed(ServletContextEvent event){
            }
          }

          <listener>
              <listener-class>com.tags.ListenerOnline</listener-class>
          </listener>

          <meta http-equiv="refresh" content="3"/>

          posted on 2009-11-29 22:47 junly 閱讀(336) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): jsp/servlet
          主站蜘蛛池模板: 育儿| 南汇区| 常宁市| 红原县| 华安县| 十堰市| 开江县| 镇平县| 金寨县| 青岛市| 岳阳县| 涟水县| 两当县| 思南县| 驻马店市| 高淳县| 芜湖市| 米脂县| 阳信县| 冀州市| 南康市| 华坪县| 苗栗市| 内乡县| 偃师市| 鄂伦春自治旗| 饶平县| 玉溪市| 简阳市| 永福县| 肇庆市| 南澳县| 塔河县| 航空| 英吉沙县| 正安县| 江西省| 柏乡县| 格尔木市| 屯昌县| 平塘县|