当您的应用程序部署在tomcat网络服务器中并且您请求一个引用目录而不是文件的 URL 时,例如,http://host:port/helloWorldApp/
您可以将 Tomcat 配置为提供目录列表或欢迎文件,或者发出错误“404 Page Not Found” . 让我们看看如何在 tomcat 服务器中启用或禁用目录列表。
目录
为所有Web 应用
启用目录列表 为任何特定的 Web应用启用目录列表
为所有 Web 应用启用目录列表
要为所有 Web 应用程序启用目录列表,您可以通过将“ ” servlet 的“ ”从“ ”<CATALINA_HOME>\conf\web.xml
更改为“ ”来修改,如下所示:listings
false
true
default
<!-- 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. -->
< 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 >true</ param-value >
</ init-param >
< load-on-startup >1</ load-on-startup >
</ servlet >
<!-- The mapping for the default servlet -->
< servlet-mapping >
< servlet-name >default</ servlet-name >
< url-pattern >/</ url-pattern >
</ servlet-mapping >
<!-- ==================== Default Welcome File List ===================== -->
<!-- When a request URI refers to a directory, the default servlet looks -->
<!-- for a "welcome file" within that directory and, if present, -->
<!-- to the corresponding resource URI for display. If no welcome file -->
<!-- is present, the default servlet either serves a directory listing, -->
<!-- or returns a 404 status, depending on how it is configured. -->
< welcome-file-list >
< welcome-file >index.html</ welcome-file >
< welcome-file >index.htm</ welcome-file >
< welcome-file >index.jsp</ welcome-file >
</ welcome-file-list >
|
上述配置将 URL “\”(Web 上下文的根目录)映射到 Java 类DefaultServlet
。我们通过将 servlet 的初始化参数更改为 true 来启用目录列表。listings
为任何特定的 Webapp 启用目录列表
如果您希望仅允许特定 Web 应用程序的目录列表,您可以全局禁用“”中的目录列表,并在您的应用程序特定中<CATALINA_HOME>\conf\web.xml
定义以下内容,如下所示:<servlet>
<servlet-mapping>
WEB-INF\web.xml
< servlet >
< servlet-name >DirectoryListing</ servlet-name >
< servlet-class >com.package.MyServlet</ 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 >true</ param-value >
</ init-param >
</ servlet >
< servlet-mapping >
< servlet-name >DirectoryListing</ servlet-name >
< url-pattern >/</ url-pattern >
</ servlet-mapping >
|
请注意,启用目录列表对于测试服务器来说很方便,但对于生产服务器来说并不需要。