<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監(jiān)聽器
    在WEB應(yīng)用中響應(yīng)特定對象的特定事件
    好處:更方便的控制application,session,request對象的發(fā)生的特定事件
         可以集中處理特定的事件
    -------------------------------------------------------------------
    HttpSession監(jiān)聽器接口
    1 HttpSessionListener(需要配web.xml)
    2 HttpSessionAttributeListener(需要配web.xml)
    3 HttpSessionBindingListener
    4 HttpSessionActivationListener(需要配web.xml)
    -------------------------------------------------------------------
    HttpSessionListener
    1 sessionCreated()//當(dāng)session對象產(chǎn)生的時候調(diào)用的方法
    2 sessionDestroyed()//當(dāng)session對象消滅的時候調(diào)用的方法
    web.xml配置
    <listener>
     <listener-class>webbook.chapter14.CounterListener(對應(yīng)的實現(xiàn)類)
     </listener-class>
    </listener>
    ---------------------------------------------------------------------
    HttpSessionAttributeListener
    1 attributeAdded()//添加時調(diào)用
    2 attributeReplaced()//替換時調(diào)用
    3 attributeRemoved()//刪除時調(diào)用
    產(chǎn)生事件的代碼
    session.setAttribute("username","wangwu");
    session.setAttribute("username","lisi");
    session.removeAttribute("username");
    -----------------------------------------------------------------
    HttpSessionBindingListener
    1 valueBound();
    2 vaoueUnbound();
    調(diào)用機(jī)制
    如果一個類實現(xiàn)了HttpSessionBindingListener接口,則當(dāng)這個類的對象通過session.setAttribute()被綁定到Session對象中時,
    則該對象的valueBound()方法被自動調(diào)用,當(dāng)這個對象從Session中刪除時(調(diào)用session.invalidate()和session.removeAtrribute()
    方法,或Session對象過期)valueUnbound()方法將被自動調(diào)用.
    --------------------------------------------------------------------------------------------------------
    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>
        當(dāng)前的在線人數(shù):<%=CounterListener.getOnlineNumber() %> <br>
    </body>
    ----------------------------------------------------------------------------
    ServletContext監(jiān)聽器接口
    ServletContextListener
    1 contextInitialized()
    2 contextDestroyed()
    調(diào)用機(jī)制
    當(dāng)在web應(yīng)用中部署了實現(xiàn)該接口的實現(xiàn)類后,在WEB容器加載WEB應(yīng)用時(如:啟動服務(wù)器)就會自動調(diào)用contextInitialized()方法,
    而當(dāng)WEB容器銷毀WEB應(yīng)用時(如:關(guān)閉服務(wù)器),會自動調(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)聽器
    ServletRequestListener
    1 requestInitialized();
    2 requestDestroyed()
    調(diào)用機(jī)制
    與ServletContextListener接口類似,在web應(yīng)用中部署實現(xiàn)該接口的實現(xiàn)類扣,在HttpServletRequest對象(JSP中的request對象)建立
    或者被銷毀時,由web容器自動調(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 閱讀(334) 評論(0)  編輯  收藏 所屬分類: jsp/servlet
    主站蜘蛛池模板: 亚洲香蕉在线观看| 精品在线视频免费| 国产精品无码一区二区三区免费| 亚洲一区二区观看播放| 亚洲日韩VA无码中文字幕| 一级毛片**不卡免费播| 亚洲av无码专区在线电影天堂| 亚洲五月综合缴情在线观看| 真人做人试看60分钟免费视频| 日韩一级片免费观看| 亚洲视频中文字幕在线| 亚洲国产成人精品91久久久| 黄网站色视频免费在线观看的a站最新| 亚洲成a人片在线看| 亚洲一区二区三区影院 | 亚洲人AV永久一区二区三区久久| 免费日本一区二区| 婷婷国产偷v国产偷v亚洲| 亚洲国产精品婷婷久久| 免费欧洲美女牲交视频| 最近中文字幕完整版免费高清| 免费观看亚洲人成网站| 亚洲午夜精品一区二区公牛电影院 | 亚洲无码一区二区三区| 亚洲成年轻人电影网站www| 国产国产成年年人免费看片| 1000部啪啪毛片免费看| 一级毛片不卡免费看老司机| 国产AV旡码专区亚洲AV苍井空| 亚洲国产精品乱码一区二区 | xvideos永久免费入口| 亚洲一卡一卡二新区无人区| 亚洲A∨无码无在线观看| 亚洲阿v天堂在线2017免费| 成人性生交大片免费看无遮挡| 日本视频在线观看永久免费| 日韩精品无码免费视频| 亚洲精品无码专区| 亚洲中文久久精品无码1| 亚洲国产天堂久久综合网站| 中文字幕久久亚洲一区|