From:http://www.iteye.com/topic/807224

 
  
  1. 每个servlet容器都有一个默认的servlet,一般都叫做default,在容器的默认配置中式这样写的(以tomcat为例):  
  2. Xml代码   
  3. <servlet>   
  4.     <servlet-name>default</servlet-name>   
  5.     <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>   
  6.     <init-param>   
  7.         <param-name>debug</param-name>   
  8.         <param-value>0</param-value>   
  9.     </init-param>   
  10.     <init-param>   
  11.         <param-name>listings</param-name>   
  12.         <param-value>false</param-value>   
  13.     </init-param>   
  14.     <load-on-startup>1</load-on-startup>   
  15. </servlet>   
  16. Xml代码   
  17. <servlet-mapping>   
  18.     <servlet-name>default</servlet-name>   
  19.     <url-pattern>/</url-pattern>   
  20. </servlet-mapping>   
  21.  配置文件时在${tomcat_home}/conf/web.xml中,如果想隐藏静态文件,或者希望静态文件(如图片,HTML文件等)不用其他servelt来解析,浪费资源呀,可以这样设置: 
  22.   
  23. Xml代码   
  24. <!--   
  25.      - Map static resources to the default servlet   
  26.      - examples:   
  27.      -     http://localhost:8080/static/p_w_picpaths/pets.png   
  28.      -     http://localhost:8080/static/styles/petclinic.css   
  29.     -->     
  30.        
  31.     <servlet-mapping>   
  32.         <servlet-name>default</servlet-name>   
  33.         <url-pattern>/static/*</url-pattern>   
  34.     </servlet-mapping>   
  35.  如此配置后,所有在/html、/p_w_picpaths下的静态文件都必须使用/static/html和/static/p_w_picpaths来访问,并且不会再被其他的servlet解析,也减轻了服务器的压力。这个方法可以说是apache的替代品,非常好用,但是……  
  36. Default servlet并不是servlet api的规范,也就是说它只是一个行规,大部分的serlvet容器都有一个default servlet,但是没有标准,这才是要老命的问题。拿jetty的default servle来做比较,先看tomcat的配置说明:  
  37.   
  38. Xml代码   
  39. <!-- The default servlet for all web applications, that serves static     -->   
  40. <!-- resources.  It processes all requests that are not mapped to other   -->   
  41. <!-- servlets with servlet mappings (defined either here or in your own   -->   
  42. <!-- web.xml file.  This servlet supports the following initialization    -->   
  43. <!-- parameters (default values are in square brackets):                  -->   
  44. <!--                                                                      -->   
  45. <!--   debug               Debugging detail level for messages logged     -->   
  46. <!--                       by this servlet.  [0]                          -->   
  47. <!--                                                                      -->   
  48. <!--   fileEncoding        Encoding to be used to read static resources   -->   
  49. <!--                       [platform default]                             -->   
  50. <!--                                                                      -->   
  51. <!--   input               Input buffer size (in bytes) when reading      -->   
  52. <!--                       resources to be served.  [2048]                -->   
  53. <!--                                                                      -->   
  54. <!--   listings            Should directory listings be produced if there -->   
  55. <!--                       is no welcome file in this directory?  [false] -->   
  56. <!--                       WARNING: Listings for directories with many    -->   
  57. <!--                       entries can be slow and may consume            -->   
  58. <!--                       significant proportions of server resources.   -->   
  59. <!--                                                                      -->   
  60. <!--   output              Output buffer size (in bytes) when writing     -->   
  61. <!--                       resources to be served.  [2048]                -->   
  62. <!--                                                                      -->   
  63. <!--   readonly            Is this context "read only", so HTTP           -->   
  64. <!--                       commands like PUT and DELETE are               -->   
  65. <!--                       rejected?  [true]                              -->   
  66. <!--                                                                      -->   
  67. <!--   readmeFile          File name to display with the directory        -->   
  68. <!--                       contents. [null]                               -->   
  69. <!--                                                                      -->   
  70. <!--   sendfileSize        If the connector used supports sendfile, this  -->   
  71. <!--                       represents the minimal file size in KB for     -->   
  72. <!--                       which sendfile will be used. Use a negative    -->   
  73. <!--                       value to always disable sendfile.  [48]        -->   
  74. <!--                                                                      -->   
  75. <!--   useAcceptRanges     Should the Accept-Ranges header be included    -->   
  76. <!--                       in responses where appropriate? [true]         -->   
  77. <!--                                                                      -->   
  78. <!--  For directory listing customization. Checks localXsltFile, then     -->   
  79. <!--  globalXsltFile, then defaults to original behavior.                 -->   
  80. <!--                                                                      -->   
  81. <!--   localXsltFile       Make directory listings an XML doc and         -->   
  82. <!--                       pass the result to this style sheet residing   -->   
  83. <!--                       in that directory. This overrides              -->   
  84. <!--                        globalXsltFile[null]                          -->   
  85. <!--                                                                      -->   
  86. <!--   globalXsltFile      Site wide configuration version of             -->   
  87. <!--                       localXsltFile This argument is expected        -->   
  88. <!--                       to be a physical file. [null]                  -->   
  89. <!--                                                                      -->   
  90. <!--             
  91.   
  92. 再看看jetty的default servlet: 
  93.   
  94.   
  95. Xml代码   
  96. /** The default servlet.                                                    
  97.  * This servlet, normally mapped to /, provides the handling for static    
  98.  * content, OPTION and TRACE methods for the context.                      
  99.  * The following initParameters are supported, these can be set either   
  100.  * on the servlet itself or as ServletContext initParameters with a prefix   
  101.  * of org.mortbay.jetty.servlet.Default. :                             
  102.  * <PRE>                                                                         
  103.  *   acceptRanges     If true, range requests and responses are            
  104.  *                    supported                                            
  105.  *                                                                         
  106.  *   dirAllowed       If true, directory listings are returned if no       
  107.  *                    welcome file is found. Else 403 Forbidden.           
  108.  *   
  109.  *   welcomeServlets  If true, attempt to dispatch to welcome files    
  110.  *                    that are servlets, but only after no matching static    
  111.  *                    resources could be found.    
  112.  *                       
  113.  *                    This must be false if you want directory listings,   
  114.  *                    but have index.jsp in your welcome file list.   
  115.  *   
  116.  *   redirectWelcome  If true, welcome files are redirected rather than   
  117.  *                    forwarded to.   
  118.  *   
  119.  *   gzip             If set to true, then static content will be served as    
  120.  *                    gzip content encoded if a matching resource is    
  121.  *                    found ending with ".gz"   
  122.  *   
  123.  *  resourceBase      Set to replace the context resource base   
  124.  *   
  125.  *  relativeResourceBase       
  126.  *                    Set with a pathname relative to the base of the   
  127.  *                    servlet context root. Useful for only serving static content out   
  128.  *                    of only specific subdirectories.   
  129.  *    
  130.  *  aliases           If True, aliases of resources are allowed (eg. symbolic   
  131.  *                    links and caps variations). May bypass security constraints.   
  132.  *                       
  133.  *  maxCacheSize      The maximum total size of the cache or 0 for no cache.   
  134.  *  maxCachedFileSize The maximum size of a file to cache   
  135.  *  maxCachedFiles    The maximum number of files to cache   
  136.  *  cacheType         Set to "bio", "nio" or "both" to determine the type resource cache.    
  137.  *                    A bio cached buffer may be used by nio but is not as efficient as an   
  138.  *                    nio buffer.  An nio cached buffer may not be used by bio.       
  139.  *     
  140.  *  useFileMappedBuffer    
  141.  *                    If set to true, it will use mapped file buffer to serve static content   
  142.  *                    when using NIO connector. Setting this value to false means that   
  143.  *                    a direct buffer will be used instead of a mapped file buffer.    
  144.  *                    By default, this is set to true.   
  145.  *                       
  146.  *  cacheControl      If set, all static content will have this value set as the cache-control   
  147.  *                    header.   
  148.  *                       
  149.  *   
  150.  两个的差别很大,甚至是默认的设置都不一样,同样是设置: 
  151.   
  152. Xml代码   
  153. <servlet-mapping>   
  154.     <servlet-name>default</servlet-name>   
  155.     <url-pattern>/static/*</url-pattern>   
  156. </servlet-mapping>   
  157.   
  158. 在jetty就run不起来,很正常,jetty必须要求一个resourceBase或者relativeResourceBase,它需要映射到一个目录,或者相对目录,这点比tomcat要好很多。   
  159.   
  160. 结论就是: 如果要在Servlet容器中使用default Servlet,一定要考虑到容器迁移的事项。