Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling in Servlet

 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added an exception handling servlet and configured it in web.xml using error-page tag. But when an exception occurs, it is not calling that servlet. Please suggest.

Example


MyExceptionServlet.java


AppExceptionHandler.java


web.xml


index.html
 
Ron McLeod
Bartender
Pie
Posts: 1049
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have any suggestions for your issue, but I am wondering who/what you see being the receipient of this information - a regular user with browser, someone technical debugging an issue with a website, a webservice endpoint?
 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:I don't have any suggestions for your issue, but I am wondering who/what you see being the receipient of this information - a regular user with browser, someone technical debugging an issue with a website, a webservice endpoint?


webservice endpoint
 
Ron McLeod
Bartender
Pie
Posts: 1049
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is for an endpoint, then put the data in a form which the endpoint can use - not in a form for humans. Rather than providing the details wrapped in HTML, provide information in a machine-friendly format such as JSON or XML, or add Warning headers (or custom X- headers) to the response providing data needed by the developer to understand the problem. You may also want to log the data on the server-side as well.

Also, in your example you are throwing an exception because of a non-supported method. I'm not sure if this was just a way to generate an exception for testing, or if it is an example of what you actually intend to do, but using exceptions to report back something like a non-supported method seems like a bad idea. A proper way would be to set the status code to 405 (Method Not Supported), and add an Allow header specifying which methods are supported for the URL. This is probably already done automatically by the webserver/framework if you don't implement a handler for a particular method.

If you are using a JAX-RS framework for your webservice server, you may want to catch and report the directly in the resource class - for example:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic