Tomcat directory listing feature allows the users to see a list of files and directories when they navigate to a directory without a default file (like index.html
). By default, the directory listing is enabled in Tomcat. However, for security reasons or user experience, we may want to disable this feature.
Let’s see how you can enable or disable directory listing in the Tomcat server.
1. Enabling Directory Listing for ALL Webapps
To enable directory listing for all the web applications, you could modify the <CATALINA_HOME>\conf\web.xml
, by changing “listings
” from “false
” to “true
” for the “default
” servlet, as follows:
The above configuration maps URL “\” (root directory of the web context) to the Java class DefaultServlet
. We enable directory listing by changing the servlet’s initialization parameter listings
to true.
If a user requests for a directory, and the directory listing is enabled and it contains one of the files in the <welcome-file> list, the welcome file will be served; otherwise, the directory listing will be served. On the other hand, if a directory request is received and the directory listing is not enabled, the server returns an error “404 Page Not Found”.
2. Enabling Directory Listing for a Particular Webapp
If you wish to allow directory listing of a particular web application only, you could disable the directory listing in “<CATALINA_HOME>\conf\web.xml
” globally, and define the following <servlet>
and <servlet-mapping>
in your application-specific WEB-INF\web.xml
, as follows:
Please note that enabling directory listing is handy for the test server but NOT desired for the production server.
Also, restarting Tomcat is necessary to apply the configuration changes.
Happy Learning !!
Comments