javatomcatdirectory-listing

How can I list all the files in folder on tomcat?


I have got a folder with many Excel documents in it on Tomcat and I want those files to be available when I go to that folder's url in the browser (eg http://localhost:8080/myfolder).

At the moment when I try to access a folder i get a 404 error. If I try to access a file that is in that folder, it works.


Solution

  • The DefaultServlet of Tomcat is by default configured to not show directory listings. You need to open Tomcat's own /conf/web.xml file (look in Tomcat installation folder), search the <servlet> entry of the DefaultServlet and then change its listings initialization parameter from

    <init-param>
        <param-name>listings</param-name>
        <param-value>false</param-value>
    </init-param>
    

    to

    <init-param>
        <param-name>listings</param-name>
        <param-value>true</param-value>
    </init-param>
    

    Keep in mind that this affects all folders of your webapp. If you want to enable this for only an individual folder, you've got to write some Servlet code yourself which does the job with help of the java.io.File API in servlet side to collect the files and some bunch of HTML/CSS in JSP side to present it in a neat fashion.