<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-124  評論-49  文章-56  trackbacks-0

    servlet監聽器
    在WEB應用中響應特定對象的特定事件
    好處:更方便的控制application,session,request對象的發生的特定事件
         可以集中處理特定的事件
    -------------------------------------------------------------------
    HttpSession監聽器接口
    1 HttpSessionListener(需要配web.xml)
    2 HttpSessionAttributeListener(需要配web.xml)
    3 HttpSessionBindingListener
    4 HttpSessionActivationListener(需要配web.xml)
    -------------------------------------------------------------------
    HttpSessionListener
    1 sessionCreated()//當session對象產生的時候調用的方法
    2 sessionDestroyed()//當session對象消滅的時候調用的方法
    web.xml配置
    <listener>
     <listener-class>webbook.chapter14.CounterListener(對應的實現類)
     </listener-class>
    </listener>
    ---------------------------------------------------------------------
    HttpSessionAttributeListener
    1 attributeAdded()//添加時調用
    2 attributeReplaced()//替換時調用
    3 attributeRemoved()//刪除時調用
    產生事件的代碼
    session.setAttribute("username","wangwu");
    session.setAttribute("username","lisi");
    session.removeAttribute("username");
    -----------------------------------------------------------------
    HttpSessionBindingListener
    1 valueBound();
    2 vaoueUnbound();
    調用機制
    如果一個類實現了HttpSessionBindingListener接口,則當這個類的對象通過session.setAttribute()被綁定到Session對象中時,
    則該對象的valueBound()方法被自動調用,當這個對象從Session中刪除時(調用session.invalidate()和session.removeAtrribute()
    方法,或Session對象過期)valueUnbound()方法將被自動調用.
    --------------------------------------------------------------------------------------------------------
    HttpSessionActivationListener
    1 sessionDidActivate();
    2 sessionWillPassivate();
    ---------------------------------------------------------------------------------
    例子:
    1 建立CounterListener處理類
    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頁面
    <%@page import="com.tags.CounterListener;"%>
    <body>
        當前的在線人數:<%=CounterListener.getOnlineNumber() %> <br>
    </body>
    ----------------------------------------------------------------------------
    ServletContext監聽器接口
    ServletContextListener
    1 contextInitialized()
    2 contextDestroyed()
    調用機制
    當在web應用中部署了實現該接口的實現類后,在WEB容器加載WEB應用時(如:啟動服務器)就會自動調用contextInitialized()方法,
    而當WEB容器銷毀WEB應用時(如:關閉服務器),會自動調用contextDestroyed()方法.
    -----------------------------------------------------------------------------------
    ServletContextAttributeListener
    1 attributeAdded()
    2 attributeReplaced()
    3 attributeRemoved()
    觸發事件的代碼
    context.setAttribute("counter",new Integer(0));
    context.setAttribute("counter",new Integer(100));
    context.removeAttribute("counter");
    -----------------------------------------------------------
    HttpServletRequest監聽器
    ServletRequestListener
    1 requestInitialized();
    2 requestDestroyed()
    調用機制
    與ServletContextListener接口類似,在web應用中部署實現該接口的實現類扣,在HttpServletRequest對象(JSP中的request對象)建立
    或者被銷毀時,由web容器自動調用requestInitialized()或者requestDestroyed()方法.
    --------------------------------------------------------------------------------------
    ServletRequestAttributeListener
    1 attributeAdded();
    2 attributeReplaced();
    3 attributeRemoved();
    觸發事件的代碼
    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 閱讀(329) 評論(0)  編輯  收藏 所屬分類: jsp/servlet
    主站蜘蛛池模板: 国产免费爽爽视频在线观看 | 亚洲成AV人片在线观看无| 美女羞羞喷液视频免费| 国产一区二区三区免费视频| 国产午夜亚洲精品不卡电影| 免费很黄很色裸乳在线观看| 国产成人亚洲精品蜜芽影院| 亚洲AV无码之日韩精品| 国产免费黄色无码视频| 久久精品国产亚洲沈樵| 99精品视频在线观看免费专区| 亚洲视频2020| 精品免费久久久久久久| 亚洲人成人网毛片在线播放| 日韩一级在线播放免费观看| 免费视频成人国产精品网站| 国产综合亚洲专区在线| 免费精品一区二区三区第35| 亚洲国产韩国一区二区| 免费被黄网站在观看| 一级毛片a女人刺激视频免费| 久久久久久久尹人综合网亚洲| 99久久久国产精品免费牛牛四川| 亚洲乱码卡一卡二卡三| 日本媚薬痉挛在线观看免费| 91av免费在线视频| 亚洲黄色中文字幕| 国产一区在线观看免费| 国产一精品一av一免费爽爽| 亚洲人成综合网站7777香蕉| 久久久久亚洲爆乳少妇无| 95免费观看体验区视频| 亚洲国产欧美一区二区三区| 国产成人综合亚洲AV第一页 | 亚洲天天做日日做天天欢毛片| 亚洲人成网站免费播放| 春意影院午夜爽爽爽免费| 亚洲影院在线观看| 国产精品高清全国免费观看| 暖暖免费在线中文日本| 亚洲精品无码久久久久APP|