How to prevent Jetty from showing context related information

14,063

Solution 1

That page is provided by the Jetty "DefaultHandler"

To stop that page from displaying, you should remove that handler from your server (it's configured in your jetty.xml file) You don't really need (or want) the default handler on a production (internet facing) server, so it's a good idea to remove it.

Note, that it also provides the regular 404 page, and the favicon.ico so if you're relying on those behaviours then you'll need to put in a replacement.

Solution 2

When configuring Jetty XML you can set showContexts to false on the DefaultHandler.

If you are using older Jetty versions replace org.eclipse.jetty on my example with the old org.mortbay.jetty package structure.

  <Configure id="Server" class="org.eclipse.jetty.server.Server">

  <!-- =========================================================== -->
  <!-- Set handler Collection Structure -->
  <!-- =========================================================== -->
  <Set name="handler">
    <!-- the collection of handlers that will handle the request -->
    <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
      <Set name="handlers">
        <Array type="org.eclipse.jetty.server.Handler">
          <!-- primarily handles the request and maps the request to a ContextHandler -->
          <Item>
            <New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
          </Item>

          <!-- The default handler ... handles the request if not yet handled -->
          <Item>
            <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
          </Item>

          <!-- The handler for your request logs -->
          <Item>
            <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler"/>
          </Item>
        </Array>
      </Set>
    </New>
  </Set>

  <!-- ===================== -->
  <!-- DefaultHandler config -->
  <!-- ===================== -->

  <Ref id="DefaultHandler">
    <Set name="showContexts">false</Set>
  </Ref>

</Configure>

Maybe you'll also be wanting to prevent directory browsing configuring the DefaultServlet of your web.xml,

  <servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class>
    <init-param>
      <param-name>dirAllowed</param-name>
      <param-value>false</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>

Solution 3

Jetty 9.X version has come up with showContexts property in org.eclipse.jetty.servlet.DefaultServlet class. Hence we can set the showContexts to flase, if you do not want to show the contexts list.

<Item>
 <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler">
     <Set name="showContexts">false</Set>
 </New>
</Item>
Share:
14,063
a5hk
Author by

a5hk

I am a software developer who has recently turned towards technology writing.

Updated on June 05, 2022

Comments

  • a5hk
    a5hk about 2 years

    I am using Jetty to deploy a production website. Let's assume my website is foo.com When I point my browser to a context which does not exist (say foo.com/notavailable), Jetty shows an error page with information of all the contexts which are deployed on it.

    It looks something like this:

    No context on this server matched or handled this request. Contexts known to this server are:

    • /test ---> org.mortbay.jetty.webapp.WebAppContext@6910fe28{/test,/root/webserver/jetty-6.1.4/webapps/test}

    I want to prevent Jetty from showing this message because it contains the full path to the context on the server.

    Is there a way to do this?

  • Sohan
    Sohan over 8 years
    It is better to explain or put the answe here, once link is invalid no pint in carrying the answer. Current link has become invalid.
  • SimplyInk
    SimplyInk over 7 years
    provided link is broken