I have a servlet that handles requests that match a pattern (*.sth). Now I want to throw a 404, if an URL was called, that matches the pattern but was not supposed to be called. I do this by setting the status on HttpServletResponse.
In the web.xml is a custom error page for 404s configured. This error page is opened, if the tomcat itself throws a 404, but not with my servlet. Of course I can call the page directly, but I’m interested in a more generic way because the servlet is used in other web apps, too. Is there a way to get the configured error pages to be able to redirect to them or tell the web container to use them?
web.xml snippet:
<servlet>
<display-name>Controller</display-name>
<servlet-name>Controller</servlet-name>
<servlet-class>com.example.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern >*.do</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>
/error.do
</location>
</error-page>
setting status:
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
‘home’ gave the right clue. Using
sendError()instead ofsetStatus()does what I want. Thanks!