Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Management in a web applications

 
Pravin Shirke
Ranch Hand
Posts: 152
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to implement Exception Handling in a web-application. like if i get a nullpointer exception it should display a user friendly message instead of a stack trace of the error for the same. how to implement that?? please help.
 
rajanikanth bhagavan kanth
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You need to implement the userdefined exception handling
like create one exception class called XYZException which extends Exception.

Let me know if im not clear

Regards
Rajani B.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64972
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can easily define handlers for any type of exception in the deployment descriptor (web.xml).

You do not need to define any custom exceptions.
[ November 17, 2008: Message edited by: Bear Bibeault ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, you might want to define a custom exception if there are special conditions or data values that will help you debug the cause. Then, in the web.xml define a handler that can extract these values and log them while sending an appropriate explanation to the user.

Bill
 
Pravin Shirke
Ranch Hand
Posts: 152
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually i wanted my application to handle that without using web.xml through code. so i thought of using filters but exception is not accessible there. how do i implement that. so if i say i got java.sql.SQLException it should print. a custom message instead of a stack trace. what should i use so that each and every page in my application has access to this feature?? please help..
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what should i use so that each and every page in my application has access to this feature??


The whole idea of the deployment descriptor (web.xml) is that information therein applies to the whole application. ;)

Bill
 
Pravin Shirke
Ranch Hand
Posts: 152
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for the information. but can you tell me what to add in web.xml.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64972
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Servlet Specification has all the information you need.
 
Ravi Kotha
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pravin,

In web.xml you can configure an error-page to show up when there is an exception. You can configure a different page to show up for different type of exception.

web.xml file:



In this case error handling page is "bummer.jsp". To make a JSP error handling page, you need to set the page directive for it.

Like this...

bummer.jsp


Hope its clear !!!

-Ravi
 
Arun Christopher
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to what Ravi said, you can also use

<error-page>
<error-code>404</error-code>
<location>/forwards/404.htm</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/forwards/500.htm</location>
</error-page>

in your web.xml
 
Pravin Shirke
Ranch Hand
Posts: 152
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you guys i am trying what ever you all have suggested get back to you after i complete this. thank you once again for the help.
 
Pravin Shirke
Ranch Hand
Posts: 152
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more thing guys

code:

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

will this be applicable for the entire application or do i need to define errorPage in every jsp in my application.
 
Srikanth Nutigattu
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

will this be applicable for the entire application or do i need to define errorPage in every jsp in my application.


If you declare the above in the DD, it applies to the entire application. ie, if "any" exception is encountered in "any" page then it is redirected to the "ErrorPage.jsp".

Note: you should declare <%@ page isErrorPage="true" %> in the ErrorPage.jsp if you want access to the default "session" object

For any specific JSP page, if you want to configure a "different" error page instead of the default ErrorPage.jsp, then you can specify it by using <%@page errorPage="relativeURL"%>. This declaration will override the default declaration in the DD..

HTH.
Cheers!!
[ November 19, 2008: Message edited by: Srikanth Nutigattu ]
 
Pravin Shirke
Ranch Hand
Posts: 152
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for helping me sorting out this issue its being resolved now. now in my ErrorPage.jsp i am checking for specific exception and displaying appropriate message.

code :
if(exception instanceOf SQLException)
{
message="Database Error.";
}
System.out.println("Error is this : "+message);
Thank you to all of you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic