滿店香的家

          學習+實踐=進步

          第四部分 DWRServlet類分析

              org.directwebremoting.servlet.DwrServlet主要分為三部分,也就是三個主要方法:init;doGet;doPost;
              1,init,初始化。    
              DWR對于Servelet容器內部設一個配置容器,該容器裝載dwr映射相關信息。

          public void init(ServletConfig servletConfig) throws ServletException
              
          {
                  
          super.init(servletConfig);
                  ServletContext servletContext 
          = servletConfig.getServletContext();

                  
          try
                  
          {
                      
          // setupLogging() only needed for servlet logging if commons-logging is unavailable
                      
          // logStartup() just outputs some version numbers
                      日志初始化開始
                      StartupUtil.logStartup(servletConfig);

                      
          // create and setup a DefaultContainer
                       DWR對于Servelet容器內部設一個配置容器,該容器裝載dwr隱射相關信息。
                      container 
          = ContainerUtil.createAndSetupDefaultContainer(servletConfig);

                      StartupUtil.initContainerBeans(servletConfig, servletContext, container);
                      webContextBuilder 
          = container.getBean(WebContextBuilder.class);

                      ContainerUtil.prepareForWebContextFilter(servletContext, servletConfig, container, webContextBuilder, 
          this);
                      ContainerUtil.publishContainer(container, servletConfig);
                      ContainerUtil.configureContainerFully(container, servletConfig);
                  }

                  
          catch (ExceptionInInitializerError ex)
                  
          {
                      log.fatal(
          "ExceptionInInitializerError. Nested exception:", ex.getException());
                      
          throw new ServletException(ex);
                  }

                  
          catch (Exception ex)
                  
          {
                      log.fatal(
          "DwrServlet.init() failed", ex);
                      
          throw new ServletException(ex);
                  }

                  
          finally
                  
          {
                      
          if (webContextBuilder != null)
                      
          {
                          webContextBuilder.unset();
                      }

                  }

              }



          比較細致的環節:
           init()調用
               createAndSetupDefaultContainer()
                  調用
                   setupDefaultContainer(defaultContainer, servletConfig)
                       調用如下步驟
                             1)setupDefaults(container);
                             包括:
                             容器相關管理工具配置集,也就是要實現的一些接口。這種方式蠻好,當容器需要具備很多接口功能時,把這些接口全部做成隱射,并設置成可配置的實現
                              container.addImplementation(AccessControl.class, DefaultAccessControl.class);
                              // Mapping handlers to URLs,路徑配置
                              createUrlMapping(container, "/index.html", IndexHandler.class, "indexHandlerUrl");
                              //讀取servlet配置屬性
                             2)setupFromServletConfig(container, servletConfig);
                               額外處理1)中多選類型
                             3)resolveMultipleImplementations(container, servletConfig);

                              里面使用了通用yui型壓縮器 
                             【使用 YUI Compressor 壓縮 Javascript 和 CSS 
                  
                              工具下載: http://www.julienlecomte.net/yuicompressor/ 
                              英文介紹: http://com3.devnet.re3.yahoo.com/yui/compressor/

                              YUI Compressor是使用Java編寫的工具,需要Java版本大于等于1.4。

                              工作原理簡介: 
                              主要是去掉冗余的空白,主要包括空格,換行符、制表符。 
                              對于 Javascript,還采用縮短變量名的方法壓縮文件,就是在保證代碼正確性的情況下將變量名用只有1個字符的字符串代替,或者2個、3個字符,總之盡量短。 
                              對于 CSS,還有采用優化0值屬性值的表示,優化顏色值的方法壓縮文件。 
                               CSS的優化比 CSSTidy 功能弱。CSSTidy還能支持一些相關屬性的合并。】
                               //工具配置集 加載,顯現,檢查
                             4)container.setupFinished();
                               容器內容bean初始化 ,包括系統默認???
                               initContainerBeans

              2,doGet
            實際調用doPost,看到這里最好溫習下servlet的doGet方法和doPost的區別
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
              
          {
                  doPost(req, resp);
              }

              3,doPost
             在工程中,有個樣例Dynamically Text,地址是http://localhost:5050/dwr/simpletext/index.html,當點擊按鈕時,實際上就是觸發了org.directwebremoting.servlet.DwrServlet的下面的方法。
          protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
              
          {
                  
          try
                  
          {
                      webContextBuilder.set(request, response, getServletConfig(), getServletContext(), container);

                      UrlProcessor processor 
          = container.getBean(UrlProcessor.class);
                      processor.handle(request, response);
                  }

                  
          finally
                  
          {
                      webContextBuilder.unset();
                  }

              }

          posted on 2008-03-28 01:41 滿店香 閱讀(1603) 評論(0)  編輯  收藏 所屬分類: dwr源碼分析

          主站蜘蛛池模板: 高唐县| 南投市| 武陟县| 庄浪县| 介休市| 西林县| 保亭| 民权县| 沅陵县| 玉林市| 宜君县| 祥云县| 美姑县| 金乡县| 桃江县| 仪征市| 鲜城| 眉山市| 寻乌县| 深州市| 楚雄市| 南陵县| 泾川县| 祁东县| 凉城县| 城固县| 常宁市| 磐石市| 古丈县| 西和县| 铜山县| 上林县| 佛教| 洮南市| 政和县| 内黄县| 登封市| 兴宁市| 蕲春县| 孝感市| 桐柏县|