How do I programmatically enable directory browsing for a specific path in Jetty 9.x?

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

+3


source to share


2 answers


Programmatically creating a Jetty instance with directory access enabled can be accomplished by creating a ResourceHandler for static content and setting setDirectoriesListed to true, or by explicitly creating and configuring DefaultServlet

. Below is an example of creation and configuration 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);

      

+3


source


If you want to customize directory browsing through configuration (not programmatically) of the web application deployment descriptor ( web.xml

), you need to customize DefaultServlet

. Here's 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 .

+3


source







All Articles