File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to map a NullPointerException thrown by servlet to my html-page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to map a NullPointerException thrown by servlet to my html-page?" Watch "How to map a NullPointerException thrown by servlet to my html-page?" New topic
Author

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

Stefan Elfvinge
Ranch Hand

Joined: Oct 29, 2001
Posts: 52
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...


Steffe
Val Pecaoco
Ranch Hand

Joined: Dec 05, 2001
Posts: 156
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


"Knowledge is power, but enthusiasm is the key." -- Lavern Barn
Stefan Elfvinge
Ranch Hand

Joined: Oct 29, 2001
Posts: 52
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

Joined: Dec 05, 2001
Posts: 156
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

Joined: Oct 29, 2001
Posts: 52
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

Joined: May 24, 2001
Posts: 245
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


chanoch<p><a href="http://www.amazon.com/exec/obidos/ASIN/1861007736/" target="_blank" rel="nofollow">Author of Professional Apache Tomcat</a></p>
Val Pecaoco
Ranch Hand

Joined: Dec 05, 2001
Posts: 156
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
 
subject: How to map a NullPointerException thrown by servlet to my html-page?