When your application is deployed in tomcat webserver and you request a URL which refers to a directory instead of a file, e.g., http://host:port/helloWorldApp/
, you can configure Tomcat to serve the directory listing, or a welcome file, or issue error “404 Page Not Found”. Let’s see how you can enable or disable directory listing in tomcat server.
Table of Contents
Enabling Directory Listing for ALL Webapps
Enabling Directory Listing for any particular Webapp
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:
< 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 >
< servlet-mapping >
< servlet-name >default</ servlet-name >
< url-pattern >/</ url-pattern >
</ servlet-mapping >
< welcome-file-list >
< welcome-file >index.html</ welcome-file >
< welcome-file >index.htm</ welcome-file >
< welcome-file >index.jsp</ welcome-file >
</ welcome-file-list >
|
The above configuration maps URL “\” (root directory of the web context) to 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”.
Enabling Directory Listing for any 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:
< 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 >
|
Please note that enabling directory listing is handy for test server but NOT desire for production server.