当您的应用程序部署在tomcat网络服务器中并且您请求一个引用目录而不是文件的 URL 时,例如,http://host:port/helloWorldApp/您可以将 Tomcat 配置为提供目录列表或欢迎文件,或者发出错误“404 Page Not Found” . 让我们看看如何在 tomcat 服务器中启用或禁用目录列表。
目录
为所有Web 应用
启用目录列表 为任何特定的 Web应用启用目录列表
为所有 Web 应用启用目录列表
要为所有 Web 应用程序启用目录列表,您可以通过将“ ” servlet 的“ ”从“ ”<CATALINA_HOME>\conf\web.xml更改为“ ”来修改,如下所示:listingsfalsetruedefault
<!-- 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>
|
请注意,启用目录列表对于测试服务器来说很方便,但对于生产服务器来说并不需要。
本文介绍了如何在Tomcat服务器中管理目录列表的显示。默认情况下,当请求一个目录而非文件时,Tomcat会返回404错误。要全局启用目录列表,你需要在`<CATALINA_HOME>confweb.xml`中修改`default` servlet的`listings`参数为`true`。这将允许所有Web应用显示目录列表。若只想对特定Web应用启用目录列表,可以在应用程序的`WEB-INFweb.xml`中进行配置,创建一个新的servlet并设置`listings`为`true`。尽管这对于测试是有用的,但在生产环境中通常不建议启用目录列表。
1480

被折叠的 条评论
为什么被折叠?



