javajspservletsservletconfigservlet-container

Programmatically get error-page from web.xml


I often have redirects like

r.sendRedirect("error.jsp"); 

Anyway the web.xml already defines error-pages.

<error-page>
    <exception-type>javax.servlet.ServletException</exception-type>
    <location>/error.jsp</location>
</error-page>

How can I programmatically get the configured error.jsp?


Solution

  • The API provides methods to do this without explicitly specifying the error page in your code. See the answer to this question for an example: How do I throw a 404 error from within a java servlet?

    The main benefit of the answer given is that the servlet container will ensure the correct HTTP error code is returned as part of the response.