1

I have a particular JSP which I would like to serve at the root page of my website (the URL "/"). All other requests should be served statically. So naturally I configured my web.xml like so:

<servlet>
  <servlet-name>index</servlet-name>
  <jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
  <servlet-name>index</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

<servlet>
  <servlet-name>default</servlet-name>
  <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>default</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

Unfortunately it seems that <url-pattern>/</url-pattern> does not do what I need it to do. Instead of only handling the "/" URL, it is special-cased, and functions as the "default mapping", handling all requests not captured by other URL patterns.

In this particular case, the default servlet's <url-pattern>/*</url-pattern> handles all URLs. A request to "/" comes up as a 404, and the index servlet never gets invoked no matter what request is made.

Is there a way to explicitly map the "/" URL, and only that URL, to a particular servlet?

2
  • No, that yields a web site which returns a 404 error no matter what I request.
    – qntm
    Aug 3, 2015 at 15:19
  • Nope. Cleared the whole web.xml. Anyway it's solved, see below.
    – qntm
    Aug 3, 2015 at 16:33

1 Answer 1

5

Use an empty pattern string to match the context root:

<servlet-mapping>
    <servlet-name>index</servlet-name>
    <url-pattern></url-pattern>
</servlet-mapping>
1
  • Thank you! I had a horrible time locating hard documentation about this.
    – qntm
    Aug 3, 2015 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.