Many of runtime errors can be caught and handled inside JSP using Java's built-in exception handling.In addition, JSP provides some great resources to help the JSP author manage error conditions.

JSP pages allow specify the errorPage page directive attribute. This allows the JSP author to deal with any uncaught exceptions and present a nice looking front-end page to the client. Using an errorPage will also allow the JSP author to build additional functions that should run to help deal with errors appropriately. It is even possible to forward the resulting problem to another location based on the specific form of problem that occurred.

Below is an example designed to simply print information about error caught:

<%@ page isErrorPage="true" %>
<%@ page import="*" %>

////////////// extract error message and stack-trace to print in log:

  String showError (HttpServletRequest req, Exception ex
                           Throws IOException {
    StringBuffer buffer = new StringBuffer();

    buffer.append("Runtime error\n");
    buffer.append(" URI: ");

    buffer.append("Extra Path: ");
    buffer.append(req.getPathInfo()).append("\n"); cw = new; pw = new,true);

    buffer.append("Stack trace:\t");

    return err.toString();

<% response.setStatus (response.SC_INTERNAL_SERVER_ERROR); %>


////////////// print excpetion information into server log:
    String error = showError(request, exception);
    application.log (error);

  <h3>500 -- Internal Server Error</hr>

////////////// print error to the user:

  <p>Error in: <%= request.getRequestURI() %></p>
  <p>Reason: <%= exception.getMessage() %></p>