• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Exception Management in a web applications

 
Ranch Hand
Posts: 152
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for the information. but can you tell me what to add in web.xml.
 
Bear Bibeault
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Servlet Specification has all the information you need.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic