Search code examples
javaservletsantjetty

On a Jetty server how do you disable directory listing, if your servlet only maps to a particular url pattern?


I have a .war application that is deployed by my Jetty server. When I access it I'm able to view the directory structure by navigating to say localhost/MyServlet/folder. I have tried to include the following in the web.xml file:

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.something.path.my.class.Servlet</servlet-class>
    <init-param>
        <param-name>dirAllowed</param-name>
        <param-value>false</param-value>
    </init-param>
</servlet>

When I rebuild my .war and run it I can still access the directory structure in my browser. This is what I would expect because I also have the following defined in the web.xml:

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

So I know that it's not the servlet that's causing this, it's Jetty that is handling and returning these directory structures. I can't find any way of turning this off within Jetty however. Does anyone know a way around this?


Solution

  • I eventually discovered that I could set this on the code base of my WebApp itself. When the web application context is setup in my java code, I can simply set it as an initiation parameter:

    WebAppContext webApp = new WebAppContext();
    webApp.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false");