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

78,587

Solution 1

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.

Solution 2

You can also enable it starting from a given url pattern. Just add the servlet and servlet-mapping to you app web.xml

<servlet>
    <!-- List files in /ws-definitions -->
    <servlet-name>ListWsDefinitions</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>100</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>ListWsDefinitions</servlet-name>
    <url-pattern>/ws-definitions/*</url-pattern>
</servlet-mapping>

In this example directories below "/ws-definitions/" will be listen.

Solution 3

Here's some documentation explaining how to do this.

http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html

The basic idea is to change the value of listings parameter to true in the main web.xml of tomcat.

<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>false</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

But the above will expose all directories. In order to have fine control, follow the steps explained here:

http://tomcat.apache.org/tomcat-7.0-doc/default-servlet.html#dir

Solution 4

If you are using Tomcat 6 (which implements Servlet 2.5 specification) or a newer version, you don't have to change the web.xml in the CATALINA_HOME/conf/ directory to display the directory listings. Instead you should change the web application's own web.xml file under WEB-INF.

As Adarshr mentioned, this is what you need to add to the web.xml

<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>

You also need to add the following

<servlet-mapping>
   <servlet-name>default</servlet-name>
   <url-pattern>/</url-pattern>
</servlet-mapping>

Solution 5

Here's a simple servlet that might be a start for a completely custom approach.

Share:
78,587
124697
Author by

124697

Updated on February 09, 2020

Comments

  • 124697
    124697 over 4 years

    I have got a folder with many excel documents in it on tomcat and i want those files to be available when i got go the 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. by if i try to access a file that is in that folder, it works.

  • BalusC
    BalusC almost 13 years
    I did not downvote, but your initial answer only told how to customize the default directory listing layout, not how to enable it.
  • 124697
    124697 almost 13 years
    I have added the web-inf file to the original post. can you tell me where abouts those paramaters should go. thanks
  • Ricardo
    Ricardo almost 10 years
    I tried this to access the files list using tomcat and it worked well. Thank you!
  • BalusC
    BalusC over 8 years
    @Solver: the other answer is not portable (i.e. webapp will crash when deployed to a server of a different make and possibly even different version). You see, it's not part of standard Servlet API.
  • Christopher Schultz
    Christopher Schultz over 7 years
    This can be done on a per-webapp basis. You just have to modify the application's WEB-INF/web.xml instead of Tomcat's site-wide conf/web.xml. You will need to copy the whole DefaultServlet declaration, setup, and mapping into your own WEB-INF/web.xml but once you do that, you can enable directory-listings for a single web application instead of all web applications deployed on that Tomcat instance.
  • BalusC
    BalusC over 7 years
    @Christopher: it's not portable, see previous comment.
  • Christopher Schultz
    Christopher Schultz over 7 years
    @BalusC Sure it's not portable, but neither are directory listings themselves. The servlet spec does not guarantee that requests for directories with no welcome-file present will instead show a directory listing. So discussing directory-listings provided by the container are already out of spec, and thus not portable.
  • BalusC
    BalusC over 7 years
    @Christopher: The main concern is that the webapp will crash when deployed to other server, not that directory listings are not configurable via Servlet API. Just configure them in the individual server itself. This way you don't need to change and rebuild the webapp for every different server the world is aware of.
  • BalusC
    BalusC over 7 years
    Nope, such a webapp would crash when deployed to a different server make/version. In other words, such a webapp is not portable. Rather configure it in server end instead of webapp end. Or, homegrow your own reusable directory listings servlet.
  • zookastos
    zookastos over 4 years
    A little late but, this is a very bad idea. We should be reading the files using Java IO and then sending the content over API, possibly paginated.