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

Exception Handling in Servlets

jagan raja
Ranch Hand

Joined: Jan 29, 2010
Posts: 54
Hi All,

I have created a user defined exception say MyException.

I have got a exception class,business class and a servlet class. I throw exception from my business class to the servlet class.





In my web.xml i have



I also have a Exception.jsp also.

How to pass the user defined exception from servlet to my jsp page? I dont wish to use sendRedirect method!!!

Can any one please help me out??
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1734
    
  25

wrap it up in a ServletException like:

together with your DD <exception> element will work as you intended,

Regards,
Frits
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
If you wrap your MyException in a ServletException declare the ServletException in your web.xml file

Example:



web.xml file:


Bye,
Nicola
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1734
    
  25

You can declare a ServletException in your web.xml but if you want to differentiate between various business exceptions you have to put the business exception type in your web.xml. That way you can define an error page per exception-type. This is what the servlet specification 2.4 says about it:

SRV.9.9.2 Error Pages
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 heirarchy wins.
If no error-page declaration containing an exception-type fits using the
class-heirarchy match, and the exception thrown is a ServletException or
subclass thereof, the container extracts the wrapped exception, as defined by the
ServletException.getRootCause method. A second pass is made over the error
page declarations, again attempting the match against the error page declarations,
but using the wrapped exception instead.


Regards,
Frits
jagan raja
Ranch Hand

Joined: Jan 29, 2010
Posts: 54
Thanks all for your information........I am now able to throw my exception and display in my JSP.
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
Can anyone explain to me what it is jagan is not doing that he should be doing to handle the exception being raised in the business class? The web.xml looks fine to me in that it maps MyException to an error handler page, and so I do not quite grasp what it is he needed help with. I also don't understand the reason for the suggestion to wrap it in a ServletException.


Costs matter. Justice lies in processes not outcomes. Crime is caused by criminals.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61772
    
  67

The doXXX methods of HttpServlet only allows ServletException and IOException to be thrown. So in order for other checked exceptions to be propagated outward (as they should be), they are wrapped in a ServletException which is then thrown. That way, the original exception becomes the root cause of the ServletException and its information is not lost.

It's a common newbie mistake to catch exceptions in the servlets and not wrap/throw them again.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling in Servlets