Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Exception Handling in Servlets

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Creator of Enthuware JWS+ V6
Posts: 3348
303
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wrap it up in a ServletException like:

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

Regards,
Frits
 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you wrap your MyException in a ServletException declare the ServletException in your web.xml file

Example:



web.xml file:
 
Frits Walraven
Creator of Enthuware JWS+ V6
Posts: 3348
303
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all for your information........I am now able to throw my exception and display in my JSP.
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 67425
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic