How do I prevent Tomcat from displaying the directory contents?

Serge Knystautas

In Tomcat 3.1, there is no setting to change this. However, you can modify org.apache.tomcat.servlets.DefaultServlet pretty easily to prevent directory listings. At line 208, the doGet method calls serveDir() which creates the file listing. Instead of calling that method, comment that out and change it to something like:

Compile this and replace the original servlet with your new version, and now instead of a directory listing, users will see a 404 File Not Found. This will not interfere with welcome files such as index.jsp or index.html (or whatever you specified in web.xml).