aspose file tools*
The moose likes Servlets and the fly likes handling servlet exception gracefully Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "handling servlet exception gracefully" Watch "handling servlet exception gracefully" New topic
Author

handling servlet exception gracefully

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
I have put a code in my servlet to handle the exception gracefully. Basically when the exception is thrown, the error.jsp should be displayed.
My servlet code has
below code.


The doService method calls one more method which throws exception.
The web.xml has below tags.

<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>

but When the exception is thrown instead of seeing the error.jsp , I am seeing the IE error page for error 500.

What am I doing wrong?

thanks
Trupti
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
There is no doService method.

You are implementing your servlet the wrong way. use doPost, doGet, etc.


JDBCSupport - An easy to use, light-weight JDBC framework -
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 688

Sebastian Janisch wrote:There is no doService method.

You are implementing your servlet the wrong way. use doPost, doGet, etc.


Yes, ther is no method like doService().

But if you want to override service method then simply use


However, this does make the Servlet less portable, since it is now dependent on preprocessed request data.

Since the routing takes place in service(), you generally do not override service() in an HTTP Servlet. Instead, override doGet() and/or doPost(), etc., depending on the type of request you expect.


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Hi Trupti,
This probably due to an IE bug/peculiar behaviour. If the message body size is less than a certain amount of byte (I believe 513 bytes) IE will try to be intelligent and show its own message.

The obvious work around is explained here.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

I guess trupti means the method(doService) calls inside service method, trupti override the service method right?..anyway the way you are handling the exception is OK . but if you are catch and throw the particular type of exception would be even better.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
In your browser under internet options, make sure "show Friendly HTTP Error messages" is not checked. That may be overriding the display of your error message.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Nishan Patel wrote:
Sebastian Janisch wrote:There is no doService method.

You are implementing your servlet the wrong way. use doPost, doGet, etc.


Yes, ther is no method like doService().

But if you want to override service method then simply use


However, this does make the Servlet less portable, since it is now dependent on preprocessed request data.

Since the routing takes place in service(), you generally do not override service() in an HTTP Servlet. Instead, override doGet() and/or doPost(), etc., depending on the type of request you expect.


Sorry for not mentioning other facts. My servlet extends the org.springframework.web.servlet.FrameworkServlet. Hence there is doService method and not the service.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
Are you really throwing an exception report to the user and not logging it?

How in the world do you expect to be able to debug your application?

It seems to me that a minimum treatment is:

1. log the stack trace
2. IF the exception shows that the app is now unusable, throw an UnavailableException so the container wont try another request
ELSE create and throw a ServletException with the message that will be some use to the client - such as try again later.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: handling servlet exception gracefully