aspose file tools*
The moose likes Servlets and the fly likes error-page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "error-page" Watch "error-page" New topic
Author

error-page

Yuqing Zhu
Greenhorn

Joined: Jan 17, 2002
Posts: 8
Hello everyone, I'm new here and have a question on error handling in servlet, hope you can help me.
My environment are winme, tomcat4.01, j2ee 1.3
see the following code fragement:
in servlet ...
//1 throw new UnavailableException ("unava", 2);
//2 throw new ServletException ();
//3 throw new RuntimeException();
throw new FileNotFoundException();
in web.xml ...
<error-page>
<exception-type>javax.servlet.UnavailableException</exception-type>
<location>/errorpage/unavailable.html</location>
</error-page>
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/errorpage/servlet.html</location>
</error-page>
<error-page>
<exception-type>java.io.FileNotFoundException</exception-type>
<location>/errorpage/io.html</location>
</error-page>
<error-page>
<exception-type>java.lang.RuntimeException</exception-type>
<location>/errorpage/runtime.html</location>
</error-page>
these codes compile. But only line 2 and 3 in servlet code work(the respective error page is displayed). For line 1, tomcat default error page is shown. For line 4, the only content shown are those output by servlet before exception thrown point.
From the previous posts in this forum, someone had the same problem with UnavailableException, but worked fine with java.net.MalformedURLException (subclass of IOException). Does anyone have any idea on this?
Thank for your help.
Regards,
Yuqing
[ January 29, 2002: Message edited by: Yuqing Zhu ]
Yuqing Zhu
Greenhorn

Joined: Jan 17, 2002
Posts: 8
Another thing should be mentioned for my pre post:
I changed line 4 to
throw new ServletException(new FileNotFoundException())
and comment out page-error for ServletException, the io.html is shown.
Thanks,
Yuqing
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
As I understand the API, the UnavailableException has a special meaning to the servlet container so it is probably caught elsewhere.
Bill
Yuqing Zhu
Greenhorn

Joined: Jan 17, 2002
Posts: 8
Thanks William.
I agree UnavailableException is handled in some special way by container. But when I add a <error-code>503</error-code> in web.xml, all UnavailableException is redirect to my desired page. I believe that's <error-code> doing its job. So I think this maybe a work-around.
But how about IOException? As someone had success experience on it, so there must be something wrong in my codes, would anyone has some success codes pasted here, thanks.
Also funny enough, if I wrap UnavailableException in a ServletException, get rid of error-page for ServletException in web.xml, it works fine. So it seems only ServletException and RuntimeException work with <error-page> handling in my experience.
Regards,
Yuqing
[ January 29, 2002: Message edited by: Yuqing Zhu ]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

What if you wrapped those other exceptions in a ServletException?

I have never tried this, but you seem to think ServletExceptions and its subclasses are the ones that are appropriately handled by the container (which is probably correct).

[ January 29, 2002: Message edited by: Mike Curwen ]
Yuqing Zhu
Greenhorn

Joined: Jan 17, 2002
Posts: 8
Thanks Mike. It did work if I wrap IOException in ServletException.
But as service() method declares throwing IOException, the container should handle it directly with error-page mechanism instead of leaving it out, shouldn't it!
Regards
Yuqing
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

The service method throws IO and Servlet Exceptions, but who says the container has to handle those the same? In fact, I think you've proven in an indirect way, that they don't.

It seems that only ServletException or a subclass of this exception are handled by the 'error page' mechanism of the container.

Kinda sucky.

And perhaps I'm wrong, and there is something else going on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: error-page