Tomcat – 启用/禁用目录列表

本文介绍了如何在Tomcat服务器中管理目录列表的显示。默认情况下,当请求一个目录而非文件时,Tomcat会返回404错误。要全局启用目录列表,你需要在`<CATALINA_HOME>confweb.xml`中修改`default` servlet的`listings`参数为`true`。这将允许所有Web应用显示目录列表。若只想对特定Web应用启用目录列表,可以在应用程序的`WEB-INFweb.xml`中进行配置,创建一个新的servlet并设置`listings`为`true`。尽管这对于测试是有用的,但在生产环境中通常不建议启用目录列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当您的应用程序部署在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

如果用户请求目录,并且启用了目录列表并且它包含 <welcome-file> 列表中的文件之一,则将提供欢迎文件;否则,将提供目录列表。另一方面,如果接收到目录请求并且目录列表未启用,则服务器返回错误“404 Page Not Found”。

为任何特定的 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>

请注意,启用目录列表对于测试服务器来说很方便,但对于生产服务器来说并不需要。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值