• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to map a NullPointerException thrown by servlet to my html-page?

 
Stefan Elfvinge
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I'm trying to cope with this error-handling mechanism. I force my servlet to throw a NullPointerException at the end of doGet(), and I have mapped an error-page in the deployment descriptor. I'm using Resin 2.1.0.
web.xml:
<web-app>
...other stuff...
<error-page>
<exception-type>java.lang.NullPointerException</exception-type>
<location>/nullpointer.html</location>
</error-page>
</web-app>
The container recognizes the html-file, though just saying that it can't find it. The html-file is placed in dir myapplication/nullpointer.html
I read something about using the sendError() method, but isn't that only for HTTPserver-errors like 401, 404
Hoping...
 
Val Pecaoco
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stefan,
From what I could recall the container expects a JSP and not an HTML page, one that contains

Please tell if that works
Ex Animo Java!
-- Val
 
Stefan Elfvinge
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, a html should do. But I read some more and I've come to the conclusion that the container can only cope with exceptions that are derived from javax.servlet.ServletException(or the same).
You write your own exception-class thrown in the catch-blocks...like:
import javax.servlet.ServletException;
public class TryAgainException extends ServletException{
public TryAgainException(String message, Throwable cause){
super(message, cause);
}
}
Could my assumptions be right?
 
Val Pecaoco
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Elfvinge:
No, a html should do.

LOL maybe I didn't read that one! Thanks (but in Tomcat when I converted my HTML page to JSP it worked :roll: )
Originally posted by Stefan Elfvinge:
But I read some more and I've come to the conclusion that the container can only cope with exceptions that are derived from javax.servlet.ServletException(or the same).
You write your own exception-class thrown in the catch-blocks...like:
import javax.servlet.ServletException;
public class TryAgainException extends ServletException{
public TryAgainException(String message, Throwable cause){
super(message, cause);
}
}
Could my assumptions be right?

Wow! I hope I have the same luxury of time that you have in creating a custom exception class! But if you would just like to catch a standard exception class like NullPointerException, you can make do with the folowing:
<web-app>
...other stuff...
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/nullpointer.html</location>
</error-page>
</web-app>
Since every exception is subclassed from Throwable.
Ex Animo Java!
-- Val
 
Stefan Elfvinge
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, suddenly another java-rule sprung up in my bewildered head. A NullPointerException is a RuntimeException and should not be caught!!!
Anyway, it's now working(with a simple html), but I've only tried with ServletExceptions(and my own)
Thanks for the answers!!!
 
chanoch wiggers
Author
Ranch Hand
Posts: 245
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont think thats right, the spec says that the exception can be any valid exception - it seems to be genuinly unable to find the error page to me...
not sure why though
 
Val Pecaoco
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Elfvinge:
Well, suddenly another java-rule sprung up in my bewildered head. A NullPointerException is a RuntimeException and should not be caught!!!


But I've caught them maybe a thousand times at the least (with the above code), especially with IE 4, which has a penchant for it...
Ex Animo Java!
-- Val
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic