---
title: "DWR  之  DWRServlet类分析 "
author: "Arno"
date: "2009-03-28T10:35:32Z"
tags: []
slug: "dwr-dwrservlet"
draft: false
lastmod: "2009-03-28T11:00:51Z"
id: 356559
url: "https://arno.iteye.com/blog/356559"
---

org.directwebremoting.servlet.DwrServlet主要分为三部分，也就是三个主要方法:init;doGet;doPost

1,init,初始化。    
    DWR对于Servelet容器内部设一个配置容器，该容器装载dwr映射相关信息。

<span style="color:green">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();
            }
        }
    }</span>



比较细致的环节：
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 
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的区别
<span style="color:red">protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
    {
        doPost(req, resp);
    }</span>


**    3，doPost**
   在工程中，有个样例Dynamically Text，地址是http://localhost:5050/dwr/simpletext/index.html，当点击按钮时，实际上就是触发了org.directwebremoting.servlet.DwrServlet的下面的方法。
<span style="color:red">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();
        }
    }</span>