GeeCON Prague 2014*
The moose likes JSP and the fly likes Exception Management in a web applications Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Exception Management in a web applications" Watch "Exception Management in a web applications" New topic
Author

Exception Management in a web applications

Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

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.


[Vipassana] - It is seeing the reality as it is, And not as you want it to be.!!!
SCJP1.5.
rajanikanth bhagavan kanth
Ranch Hand

Joined: May 20, 2008
Posts: 78
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

Joined: Jan 10, 2002
Posts: 61310
    
  66

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
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

Joined: Apr 05, 2008
Posts: 150

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

Joined: Mar 22, 2000
Posts: 12791
    
    5
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

Joined: Apr 05, 2008
Posts: 150

thank you for the information. but can you tell me what to add in web.xml.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

The Servlet Specification has all the information you need.
Ravi Kotha
Ranch Hand

Joined: Mar 02, 2006
Posts: 53
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

Joined: Nov 18, 2008
Posts: 19
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

Joined: Apr 05, 2008
Posts: 150

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

Joined: Apr 05, 2008
Posts: 150

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

Joined: Oct 30, 2007
Posts: 114

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

Joined: Apr 05, 2008
Posts: 150

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.
 
GeeCON Prague 2014
 
subject: Exception Management in a web applications