aspose file tools*
The moose likes JSP and the fly likes exception Tag in web.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "exception Tag in web.xml" Watch "exception Tag in web.xml" New topic
Author

exception Tag in web.xml

Sam Furtado
Ranch Hand

Joined: Jul 16, 2002
Posts: 45
Hi Guys !!!
I have created a Custom Exception "DataStoreException" which is being thrown by a method in a class and caught in the doGet()method of the Servlet class.
On the exception occuring i would like to automatically redirect to a specified error page as mentioned within the <error-page><exception-type></exception-type></error-page><location></location> tags in web.xml. However, how should i re-throw the caught exception in the servlet since the doGet() method would then have to contain "throws DataStoreException" in its throws clause which it does'nt support(does'nt allow to overide doGet()). In this case how should the exception be thrown from the Servlet and the appropriate error page be invoked as mentioned in the <error-page> tag.
Pls suggest.
Thanks Guys !!!


Sun Certified Java Programmer<br />Sun Certified Web Component Developer
Harpartap Singh
Greenhorn

Joined: Sep 14, 2002
Posts: 23
The doGet() method of Servlet interface can only throw the following three exceptions or their subclasses:
ServletException
IOException
RuntimeException
For custom, self-defined exceptions, wrap them in ServletException and rethrow the ServletException.
e.g:
catch( DataStoreException ex ) {
throw new ServletException( "Wrapped Excep", ex );
}
In the web.xml:

<error-page>
<exception-type>DataStoreException</exception-type>
<location>myErrorHandlingPage.jsp</location>
</error-page>
In your myErrorHandlingPage.jsp:

use the ServletException.getRootCause() to read the wrapped exception
NOTE: Even though you are throwing ServletException from doGet(), the conainer will call the ServletException.getRootCause() to find the wrapped exception and match it your <error-page> definition. BUT, remember not to have a generic <exception-type>ServletException</exception-type> in your web.xml, because then the container will just hand over your exception to this generic's corresponding location.
Hope this helps.
/hs.


<blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Lance Armstrong: "What am I on? I'm on my bike, busting my ass six hours a day. What are you on?"<hr></blockquote>
Sam Furtado
Ranch Hand

Joined: Jul 16, 2002
Posts: 45
Hi Harpartap !!!
Thanks for ur reply. I have tried out the same. However, when the exception does occur it prints a stack trace of "ServletException" and below it a ROOT CAUSE Stack Trace of "DataStoreException". However, it should rather redirect to the jsp page specified in the web.xml file. This application is currently being run on tomcat 3.2.1. What needs to be done ???
Pls suggest.
Thanks Harpartap
Asher Tarnopolski
Ranch Hand

Joined: Jul 28, 2001
Posts: 260
just in case...
the tags order you have is:
<error-page>
<exception-type></exception-type>
<location></location>
</error-page>
and not as appears in your post...


Asher Tarnopolski
SCJP,SCWCD
Kees van Oosterhout
Ranch Hand

Joined: Jul 08, 2002
Posts: 34
I seem to have the same problem. I am using Websphere Application Single Server 4.0. I have placed a IllegalArgumentException in the doPost method and configured an error page for it in web.xml. This works fine. But when i try:
<CODE>
AppException e = new AppException("Error", this.getClass().getName());
throw new ServletException("Servlet POST error.", e);
</CODE>
and:
<CODE>
<error-page id="ExceptionTypeErrorPage_1">
<exception-type>com.company.AppException</exception-type>
<location>/jsp/error.jsp</location>
</error-page>
</CODE>
I get a white screen with:
Servlet POST error.
Of course, this is not the content of error.jsp


"...you've got to ask yourself one question, Do I feel lucky?"
Kees van Oosterhout
Ranch Hand

Joined: Jul 08, 2002
Posts: 34
Ok. It works fine now. You should check if the location in web.xml. First try to redirect the exception to jsp page with only HTML tags. If this works set the jsp page as an error page with:
<%@ page isErrorPage="true" %>
And cast the exception object to an ServletException. The root cause of this exception is your DataStoreException. (on IBM Websphere)
Hope it helps....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: exception Tag in web.xml