From:http://www.iteye.com/topic/807224
- 每个servlet容器都有一个默认的servlet,一般都叫做default,在容器的默认配置中式这样写的(以tomcat为例):
- Xml代码
- <servlet>
- <servlet-name>default</servlet-name>
- <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
- <init-param>
- <param-name>debug</param-name>
- <param-value>0</param-value>
- </init-param>
- <init-param>
- <param-name>listings</param-name>
- <param-value>false</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- Xml代码
- <servlet-mapping>
- <servlet-name>default</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
- 配置文件时在${tomcat_home}/conf/web.xml中,如果想隐藏静态文件,或者希望静态文件(如图片,HTML文件等)不用其他servelt来解析,浪费资源呀,可以这样设置:
- Xml代码
- <!--
- - Map static resources to the default servlet
- - examples:
- - http://localhost:8080/static/p_w_picpaths/pets.png
- - http://localhost:8080/static/styles/petclinic.css
- -->
- <servlet-mapping>
- <servlet-name>default</servlet-name>
- <url-pattern>/static/*</url-pattern>
- </servlet-mapping>
- 如此配置后,所有在/html、/p_w_picpaths下的静态文件都必须使用/static/html和/static/p_w_picpaths来访问,并且不会再被其他的servlet解析,也减轻了服务器的压力。这个方法可以说是apache的替代品,非常好用,但是……
- Default servlet并不是servlet api的规范,也就是说它只是一个行规,大部分的serlvet容器都有一个default servlet,但是没有标准,这才是要老命的问题。拿jetty的default servle来做比较,先看tomcat的配置说明:
- Xml代码
- <!-- The default servlet for all web applications, that serves static -->
- <!-- resources. It processes all requests that are not mapped to other -->
- <!-- servlets with servlet mappings (defined either here or in your own -->
- <!-- web.xml file. This servlet supports the following initialization -->
- <!-- parameters (default values are in square brackets): -->
- <!-- -->
- <!-- debug Debugging detail level for messages logged -->
- <!-- by this servlet. [0] -->
- <!-- -->
- <!-- fileEncoding Encoding to be used to read static resources -->
- <!-- [platform default] -->
- <!-- -->
- <!-- input Input buffer size (in bytes) when reading -->
- <!-- resources to be served. [2048] -->
- <!-- -->
- <!-- listings Should directory listings be produced if there -->
- <!-- is no welcome file in this directory? [false] -->
- <!-- WARNING: Listings for directories with many -->
- <!-- entries can be slow and may consume -->
- <!-- significant proportions of server resources. -->
- <!-- -->
- <!-- output Output buffer size (in bytes) when writing -->
- <!-- resources to be served. [2048] -->
- <!-- -->
- <!-- readonly Is this context "read only", so HTTP -->
- <!-- commands like PUT and DELETE are -->
- <!-- rejected? [true] -->
- <!-- -->
- <!-- readmeFile File name to display with the directory -->
- <!-- contents. [null] -->
- <!-- -->
- <!-- sendfileSize If the connector used supports sendfile, this -->
- <!-- represents the minimal file size in KB for -->
- <!-- which sendfile will be used. Use a negative -->
- <!-- value to always disable sendfile. [48] -->
- <!-- -->
- <!-- useAcceptRanges Should the Accept-Ranges header be included -->
- <!-- in responses where appropriate? [true] -->
- <!-- -->
- <!-- For directory listing customization. Checks localXsltFile, then -->
- <!-- globalXsltFile, then defaults to original behavior. -->
- <!-- -->
- <!-- localXsltFile Make directory listings an XML doc and -->
- <!-- pass the result to this style sheet residing -->
- <!-- in that directory. This overrides -->
- <!-- globalXsltFile[null] -->
- <!-- -->
- <!-- globalXsltFile Site wide configuration version of -->
- <!-- localXsltFile This argument is expected -->
- <!-- to be a physical file. [null] -->
- <!-- -->
- <!--
- 再看看jetty的default servlet:
- Xml代码
- /** The default servlet.
- * This servlet, normally mapped to /, provides the handling for static
- * content, OPTION and TRACE methods for the context.
- * The following initParameters are supported, these can be set either
- * on the servlet itself or as ServletContext initParameters with a prefix
- * of org.mortbay.jetty.servlet.Default. :
- * <PRE>
- * acceptRanges If true, range requests and responses are
- * supported
- *
- * dirAllowed If true, directory listings are returned if no
- * welcome file is found. Else 403 Forbidden.
- *
- * welcomeServlets If true, attempt to dispatch to welcome files
- * that are servlets, but only after no matching static
- * resources could be found.
- *
- * This must be false if you want directory listings,
- * but have index.jsp in your welcome file list.
- *
- * redirectWelcome If true, welcome files are redirected rather than
- * forwarded to.
- *
- * gzip If set to true, then static content will be served as
- * gzip content encoded if a matching resource is
- * found ending with ".gz"
- *
- * resourceBase Set to replace the context resource base
- *
- * relativeResourceBase
- * Set with a pathname relative to the base of the
- * servlet context root. Useful for only serving static content out
- * of only specific subdirectories.
- *
- * aliases If True, aliases of resources are allowed (eg. symbolic
- * links and caps variations). May bypass security constraints.
- *
- * maxCacheSize The maximum total size of the cache or 0 for no cache.
- * maxCachedFileSize The maximum size of a file to cache
- * maxCachedFiles The maximum number of files to cache
- * cacheType Set to "bio", "nio" or "both" to determine the type resource cache.
- * A bio cached buffer may be used by nio but is not as efficient as an
- * nio buffer. An nio cached buffer may not be used by bio.
- *
- * useFileMappedBuffer
- * If set to true, it will use mapped file buffer to serve static content
- * when using NIO connector. Setting this value to false means that
- * a direct buffer will be used instead of a mapped file buffer.
- * By default, this is set to true.
- *
- * cacheControl If set, all static content will have this value set as the cache-control
- * header.
- *
- *
- 两个的差别很大,甚至是默认的设置都不一样,同样是设置:
- Xml代码
- <servlet-mapping>
- <servlet-name>default</servlet-name>
- <url-pattern>/static/*</url-pattern>
- </servlet-mapping>
- 在jetty就run不起来,很正常,jetty必须要求一个resourceBase或者relativeResourceBase,它需要映射到一个目录,或者相对目录,这点比tomcat要好很多。
- 结论就是: 如果要在Servlet容器中使用default Servlet,一定要考虑到容器迁移的事项。
转载于:https://blog.51cto.com/tianya23/1034651