Results 1 to 3 of 3

Thread: Exception handling of exceptions thrown in jsp's

  1. #1
    Join Date
    Dec 2004
    Posts
    10

    Default Exception handling of exceptions thrown in jsp's

    Currently I use SimpleMappingExceptionResolver to show the users an appropriate error page and log the exception to a log file when an exception is thrown in a controller.

    The SimpleMappingExceptionResolver only handles exceptions thrown in controllers and not exceptions thrown when rendering a jsp.

    Is there a similar mechanism for jsp's? Ie. a mechanism allowing me to catch exceptions thrown while rendering a jsp, show the user an error page and log the exception to a log file?

    Best regards,
    Jørgen

  2. #2
    Join Date
    Dec 2004
    Posts
    10

    Default

    OK, sorted it out myself. Had to add an isErrorPage="true" and after that I am able to access the exception:

    Code:
    <%@ page isErrorPage="true" import="org.apache.log4j.Logger"%>
    ...
    <%
       Logger.getLogger&#40;"exception.jsp"&#41;.error&#40;exception&#41;;
       Logger.getLogger&#40;"exception.jsp"&#41;.error&#40;exception.getCause&#40;&#41;&#41;;
    %>
    The problem now is that debugging shows me that a NullPointerException is thrown when rendering the jsp but the printout from the code above is org.apache.jasper.JasperException?!
    Is there a way to print out the root cause ie. the NullPointerException?

    Cheers from sunny Denmark,
    Jørgen

  3. #3
    Join Date
    Dec 2004
    Posts
    10

    Default

    Got the root cause by casting the Exception to a ServletException ie.:

    Code:
    <%
      if &#40;exception instanceof ServletException&#41; &#123;
        ServletException servletException = &#40;ServletException&#41; exception;
        Logger.getLogger&#40;"exception.jsp"&#41;.error&#40;servletException.getRootCause&#40;&#41;&#41;;
        Logger.getLogger&#40;"exception.jsp"&#41;.error&#40;servletException.getRootCause&#40;&#41;.getCause&#40;&#41;&#41;;
      &#125; else &#123;
        Logger.getLogger&#40;"exception.jsp"&#41;.error&#40;exception&#41;;
        Logger.getLogger&#40;"exception.jsp"&#41;.error&#40;exception.getCause&#40;&#41;&#41;;
      &#125;
    %>
    /Jørgen

Similar Threads

  1. Replies: 1
    Last Post: Oct 4th, 2005, 06:11 PM
  2. Context initialization failed
    By kanonmicke in forum Container
    Replies: 7
    Last Post: Sep 29th, 2005, 12:35 AM
  3. Handling exceptions thrown during initialization
    By tgullotta in forum Container
    Replies: 1
    Last Post: Mar 7th, 2005, 11:42 AM
  4. Replies: 2
    Last Post: Dec 20th, 2004, 04:35 PM
  5. Replies: 3
    Last Post: Nov 8th, 2004, 07:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •