carlspring
carlspring

Reputation: 32629

How to programmatically enable directory browsing for a particular path in Jetty 9.x?

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

Upvotes: 3

Views: 1720

Answers (2)

ironchefpython
ironchefpython

Reputation: 3409

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

Upvotes: 3

ironchefpython
ironchefpython

Reputation: 3409

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.

Upvotes: 3

Related Questions