Solved

Servlet Container Exception Handling

Posted on 2011-09-06
3
189 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Loop 6 49
windows explorer path to command prompt 5 33
how to add new optional parameter to JSP 1 29
arguments to jar 5 15
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now