Forums Register Login

Business Exception error page shown when ServletException thrown

+Pie Number of slices to send: Send

Within a servlet I throw a Business Exception, catch it and then throw a ServletException. In the deployment descriptors I define an error page for the Business Exception. When I call the servlet that throws the ServletException I am redirected to the error page defined for the Business Exception. Why and how does it do this? I have also discovered that if I define a error page for the ServletException I am shown its error page instead. I am sure that this is desired behaviour. I wondered if someone with more experience could tell me something about this. I have not found other questions on this forum like this nor did I find any clues in the servlet specs. Thank you in advance for anything you can say regarding this.

Please see code snippets below.

The doGet() method of the servlet:



The XML snippet from the deployment descriptors.

+Pie Number of slices to send: Send
Alex

If you let the exception go, rather than catching it, what happens?
+Pie Number of slices to send: Send
The Business Exception is a checked exception which means that it must be either caught or propagated. It cannot be propagated as it does not form one of a subset of exceptions that are thrown by the doGet() method that it overrides, so it must be caught otherwise an unhandled exception error occurs. I am seeking an explanation as to why and how the error page associated with the business exception (defined in the deployment descriptor) is shown when the catch clause of the try that caught the business exception throws a ServletException. I believe that this behaviour is correct and desirable, just not what I expected. I am hoping that someone with knowledge of this can shed some light on it.
1
+Pie Number of slices to send: Send
Alex

As a test, create another exception which is a runtime exception:
Amend your deployment descriptor to include a different re-direct for this exception:

Now test these scenarios:
1) In your doGet method, throw a MyRuntimeException. Which page does the user get re-directed to?
2) In your doGet method, throw a ServletException. Which page does the user get re-directed to?
+Pie Number of slices to send: Send
I haven't tried your suggestion but I think that I have an answer.

I wanted to know why the error page defined for the business exception was shown when the exception thrown was ServletException. The reason is that the business exception is passed as a parameter to the ServletException as the root cause of the exception and I am assuming that the container attempts to match this root cause exception to an exception mapped to an error page in the deployment descriptor and that if there is an error page defined for the ServletException that this page is chosen over the other.

Thanks for attempting to help me with this issue. As is often the case a good nights sleep results in the solution being found.
+Pie Number of slices to send: Send
Alex

I think ultimately the container will look to match the "closest" exception in the class hierarchy. I have read a little of the Java Servlet Specification, section 10.9.2:

The Web application may have declared error pages using the exception-type
element. In this case the container matches the exception type by comparing the
exception thrown with the list of error-page definitions that use the exception-type
element. A match results in the container returning the resource indicated in the
location entry. The closest match in the class hierarchy wins.


This question on SO may also lead you to more information:
http://stackoverflow.com/questions/2583605/web-xml-exceptiontype
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1391 times.
Similar Threads
Exception Handling in Servlets
Q: Appropriate Exception Handling in a servlet
Servlet Exception handling not working
HttpServletResponse.sendError
exception Tag in web.xml
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 05:59:41.