The standard thing to do is have your Servlet's doXxx()
method (eg. doGet()
, doPost()
, etc.) throw a ServletException
and allow the container to catch and handle it. You can specify a custom error page to be shown in WEB-INF/web.xml
using the <error-page>
tag:
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
If you end up catching an Exception
you can't elegantly handle, just wrap it in a ServletException
like this:
try {
// code that throws an Exception
} catch (Exception e) {
throw new ServletException(e);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…