• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

exception Tag in web.xml

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !!!
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
reply
    Bookmark Topic Watch Topic
  • New Topic