user1154644
user1154644

Reputation: 4609

Handling ServletExceptions that occur

I am building a simple web application. In my doPost() method, when I forward to a JSP, if a ServletException occurs, I throw a ServletException. In my web.xml file I have declared an error page to catch a 500 error. The application works fine on my GlassFish server, but when I deploy to a Tomcat server, I am being directed to my problem.jsp page. How can I get some details on the cause of the error?

Here is how my error page configured in web.xml:

<error-page>
    <error-code>500</error-code>
    <location>/problem.jsp</location>
</error-page>

Upvotes: 0

Views: 79

Answers (1)

Akheloes
Akheloes

Reputation: 1372

your problem.jsp can access this request attribute to give you a feed about the nature of the exception :

  • javax.servlet.error.exception

You can use these as follows :

    public void doPost(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
     {      
         Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
     }

You can afterwards spy on the type of the exception by doing :

PrintWriter out = response.getWriter();
out.println("Exception Type : " +  throwable.getClass( ).getName( ));

Resources

You can learn more on this web page. You'll find some other attributes you'll be wanting to check for extensive details about your error.

Upvotes: 1

Related Questions