I have this XML in my web.xml file:
<error-page>
<error-code>404</error-code>
<location>/error/html/404</location>
</error-page>
If I go to a page like /indexer (which doesn’t exist), then I get redirected properly to the URL /error/html/404. If I go to a page like /index/nope (which also doesn’t exist), then I a blank page and the URL reads /index/error/html/404. What can I put in the location tag to always get the correct URL, regardless of where I started?
This is not normal behaviour. It should definitely be relative to the context root. I cite from page 154 of the Servlet 3.0 specification:
Your problem is caused by a bug in the servletcontainer used. Report it and/or upgrade to latest version. Or, perhaps you’ve a bad
Filterintercepting onERRORdispatcher for an URL pattern of/*which is using theRequestDispatcher#forward()wrongly.Update as per the comments, you’re using Tomcat 7.0.16, which is a Servlet 3.0 compatible container. Although your
web.xmlis declared conform Servlet 2.5 instead of 3.0, I can’t reproduce your problem with bothweb.xmlversions on my local Tomcat 7.0.11, 7.0.19 nor 7.0.22. Also, the URL does not change here in browser address bar (and is not supposed to change). Just the original (wrong) URL remains in browser address bar. This makes me think that this is more likely caused by a bad filter or possibly a front controller servlet which is mapped on a too generic URL pattern of/*and is sending a redirect to the wrong URL. Check for such a filter or servlet in your webapp, debug and fix it accordingly.