Solved

Servlet Container Exception Handling

Posted on 2011-09-06
3
193 Views
Last Modified: 2012-05-12
Hello,

I have a J2EE application running on Tomcat 7 with log4j implemented.  I wanted to create a custom error page whenever my application encounters an Exception.  I created something similar to ex #1 below in my web.xml, but I was wondering If I can just catch all errors.  I don't think its possible since 500 is java.lang.Throwable and wont get caught if I try to use and exception-type condition (see #2 below).  

ex#1
<error-page>
    <error-code>404</error-code>
    <location>/errors/error.jsp</location>
<error-code>500</error-code>
    <location>/errors/error.jsp</location>
</error-page> 

Open in new window


ex#2
<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/errors/error.jsp</location>  
</error-page> 

Open in new window


0
Comment
Question by:cgray1223
  • 2
3 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36492523


perhpas this solution can help you:
http://www.coderanch.com/t/492765/Servlets/java/Exception-Handling-Servlets


      
posted Saturday, April 24, 2010 2:02:26 AM private message
Quote
If you wrap your MyException in a ServletException declare the ServletException in your web.xml file

Example:



    catch(MyException e)    
    {    
        throw new ServletException(e)  
    }  



web.xml file:


      <error-page>  
       <exception-type>javax.servlet.ServletException</exception-type>  
       <location>/error/error.jsp</location>  
      </error-page>  

also read some stuff below in that link
0
 

Author Comment

by:cgray1223
ID: 36497172
Thanks for the response.  That would work for all exceptions I'm expecting, but not for 404s or runtime exceptions that are not trapped.  I don't think I can trap every error that could ever occur...thoughts?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36497380
Yes, I think you are right - you cannot trap every error that could ever occurs - there will be alwyas something which you had not anticipated
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question