Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

exception Tag in web.xml

 
Sam Furtado
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!
 
Harpartap Singh
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sam Furtado
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 260
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Kees van Oosterhout
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kees van Oosterhout
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic