3

Is it possible to programmatically enable directory browsing for a particular path in Jetty 9.x (and if "yes" -- how)?

carlspring
  • 31,231
  • 29
  • 115
  • 197

2 Answers2

3

Programmatically creating a Jetty instance with directory browsing enabled can be done by creating a ResourceHandler for static content and setting setDirectoriesListed to true , or by explicitly creating a and configuring a DefaultServlet. Below is an example for creating and configuring a ResourceHandler.

ResourceHandler staticResource = new ResourceHandler();
staticResource.setDirectoriesListed(true);
staticResource.setWelcomeFiles(new String[] { "index.html" });
staticResource.setResourceBase("/path/to/your/files");

ContextHandler staticContextHandler = new ContextHandler();
staticContextHandler.setContextPath("/*");
staticContextHandler.setHandler(staticResource);

Server server = new Server(8080);
server.setHandler(staticContextHandler);
ironchefpython
  • 3,409
  • 1
  • 19
  • 32
  • Hi, thanks for the example! What exactly do I need to do with this `ResourceHandler`, if I'm running the code from within the container? – carlspring Nov 10 '14 at 18:35
  • You put the `ResourceHandler` in a `ContextHandler`, and put the `ContextHandler` in the `Server` (or add it to a `ContextHandlerCollection`). – ironchefpython Nov 10 '14 at 18:44
  • I think I might not have been clear enough -- the code is running within a standalone Jetty, which I am not starting programmatically. Is there a way to do it like this? – carlspring Nov 10 '14 at 18:51
  • You either need to be able to edit the web.xml, or programmatically change how the standalone Jetty is started. – ironchefpython Nov 11 '14 at 16:05
  • Right -- so how would I go about doing it, if I am able to edit the `web.xml`? – carlspring Nov 11 '14 at 16:11
  • I've added a new answer for handling your use case through configuration rather than programmatically. – ironchefpython Nov 12 '14 at 18:06
  • you can also aggregate multiple directories together if you like under one context like this (just mentioning since it is kinda neat): http://www.eclipse.org/jetty/documentation/current/embedded-examples.html#embedded-split-file-server – jesse mcconnell Nov 17 '14 at 19:08
3

If you want to configure directory browsing through configuration (not programmatically) of the Web Application Deployment Descriptor (web.xml), you will need to configure a DefaultServlet. Here is an example:

<servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class>
    <init-param>
        <param-name>resourceBase</param-name>
        <param-value>/path/to/your/static/files</param-value>
    </init-param>
    <init-param>
        <param-name>dirAllowed</param-name>
        <param-value>true</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/path/to/serve/content/on/*</url-pattern>
</servlet-mapping>

See http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/servlet/DefaultServlet.html for details and additional configuration options.

ironchefpython
  • 3,409
  • 1
  • 19
  • 32