WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JSP exception handling


When writing JSP programs, programmers may miss some bugs, and these bugs may appear anywhere in the program. JSP code usually has the following exceptions:

  • Checked exception: A checked exception is a typical user error or an error that a programmer cannot foresee. For example, if a file is about to be opened but the file cannot be found, an exception is thrown. These exceptions cannot simply be ignored at compile time.
  • Runtime exceptions: A runtime exception may have been avoided by the programmer, and such exceptions will be ignored at compile time.
  • Error: An error is not an exception, but the problem is that it is beyond the control of the user or programmer. Errors are usually ignored in your code, and you can hardly get at it. For example, a stack overflow error. These errors are ignored at compile time.
  • This section will give a few simple and elegant ways to deal with runtime exceptions and errors.


    Using Exception Objects

    The exception object is an instance of the Throwable subclass and is only available on error pages. The following table lists some important methods in the Throwable class:

    Serial number method&Description
    1 public String getMessage ()

    Returns exception information. This information is initialized in the Throwable constructor
    2 public ThrowablegetCause ()

    Returns the cause of the exception, type Throwable object
    3 public String toString ()

    Return class name
    4 public void printStackTrace ()

    Output the exception stack trace to System.err
    5 public StackTraceElement [] getStackTrace ()

    Returns the exception stack trace as an array of stack trace elements
    6 public ThrowablefillInStackTrace ()

    Fill the Throwable object with the current stack trace

    JSP provides the option to specify an error page for each JSP page. Whenever an exception is thrown by the page, the JSP container will automatically call the error page.

    The following example specifies an error page for main.jsp. use <%@page errorPage="XXXXX"%>The directive specifies an error page.

    <%@ page errorPage="ShowError.jsp" %>
    
    <html>
    <head>
       <title>Error Handling Example</title>
    </head>
    <body>
    <%
       // Throw an exception to invoke the error page
       int x = 1;
       if (x == 1)
       {
          throw new RuntimeException("Error condition!!!");
       }
    %>
    </body>
    </html>

    Now, write the ShowError.jsp file as follows:

    <%@ page isErrorPage="true" %>
    <html>
    <head>
    <title>Show Error Page</title>
    </head>
    <body>
    <h1>Opps...</h1>
    <p>Sorry, an error occurred.</p>
    <p>Here is the exception stack trace: </p>
    <pre>
    <% exception.printStackTrace(response.getWriter()); %>

    Noticed´╝îShowError.jsp file used<%@page isErrorPage="true"%>Directive, this directive tells the JSP compiler to generate an exception instance variable.

    Now try to visit the main.jsp page, it will produce the following result:

    java.lang.RuntimeException: Error condition!!!
    ......
    
    Opps...
    Sorry, an error occurred.
    
    Here is the exception stack trace:

    Using JSTL tags in error pages

    You can use JSTL tags to write the error page ShowError.jsp. The code in this example is almost the same as the code in the previous example, but the code in this example has a better structure and can provide more information:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@page isErrorPage="true" %>
    <html>
    <head>
    <title>Show Error Page</title>
    </head>
    <body>
    <h1>Opps...</h1>
    <table width="100%" border="1">
    <tr valign="top">
    <td width="40%"><b>Error:</b></td>
    <td>${pageContext.exception}</td>
    </tr>
    <tr valign="top">
    <td><b>URI:</b></td>
    <td>${pageContext.errorData.requestURI}</td>
    </tr>
    <tr valign="top">
    <td><b>Status code:</b></td>
    <td>${pageContext.errorData.statusCode}</td>
    </tr>
    <tr valign="top">
    <td><b>Stack trace:</b></td>
    <td>
    <c:forEach var="trace" 
             items="${pageContext.exception.stackTrace}">
    <p>${trace}</p>
    </c:forEach>
    </td>
    </tr>
    </table>
    </body>
    </html>

    The results are as follows: