Saturday, August 5, 2017

JSP: error page for uncaught exception


To create an error page to print out the details of an uncaught exception:

1. In WEB-INF/web.xml, add the error-page element:
    <error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/error.jsp</location>
    </error-page>


2. Create the error.jsp in the root directory with the following content:
    <%@ page isErrorPage="true" import="java.io.*" contentType="text/plain"%>

    Exception:
    <%
        PrintWriter printWriter = new PrintWriter(out);
        exception.printStackTrace(printWriter);
        printWriter.close();
    %>


However, for security reason, you don't want to leak the information of the stack trace of your exception to a client side browser, because it is a trace of your source code. The above error.jsp that prints out the stack trace should be used only in the development environment. In a production environment, an error.jsp may contain only an error message such as "An error has occurred. Please contact ..."

No comments:

 
Get This <