• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP/FilterServlet/ControllerServlet Exception Handling

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having some problems figuring out how to handle Exceptions when working with JSP / FilterServlet / ControllerServlet

I want all exceptions, no matter if they happen in JSP page/FilterServlet/ControllerServlet/Java Class to redirect the user to "error.jsp" where it shows the error name and a stacktrace of it.

This is a typical action on the site:
1. user requests "index.jsp"
2. FilterServlet checks session and gets data to display on "index.jsp"
3. when FilterServlet is done it shows "index.jsp"

1. user clicks update button
2. jquery ajax makes a post to ControllerServlet
3. ControllerServlet ask a Java Class to open a connection to mysql and make the update
4. ControllerServlet gets a boolean back from the class wheather it was a success or not
5. ControllerServlet makes a printwriter where it prints the result
6. jquery ajax reads the result and displays it to the user in a nice message under the update form.

All of the above works like it should except for when an exception occours.
right now when an error occours i just make a try catch and do nothing in the catch block.
i have tried almost all of the tutorials and examples i could find on how to handle this but none of them seams to work.

The problem might be that everything happens through jQuery AJAX, but i am not sure.

in a sidenote i am also using URLRewriteFilter, but i dont think that gives any problems when it comes to exceptions

I would be very grateful if someone here can figure this out.

Regards Frederik
 
Sheriff
Posts: 67746
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
I always just declare a central error handler in the deployment descriptor and never catch an exception (unless I have something special to do with it). Just let it propagate out to the container to handle it according to the deployment descriptor.
 
Frederik Nielsen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right now i only have this in web.xml



What do you do about exceptions that has to be caught like sql exceptions ?

Are you able to go from anywhere in your project when an exception happens and then get to an error page where it displays the error ? give a little more info/code about how.
 
Bear Bibeault
Sheriff
Posts: 67746
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
What you've got will only trap 404 (not found) responses. You want to catch 500 errors. You can also specify specific exception types.

The details can be found in the servlet specification.

(Not sure why you have the servlet and servlet-mapping declarations. They're not used by your error-page declaration.)
 
Frederik Nielsen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my Servlet controller and filter has

in their catch block now and in my web.xml do i have


It does absolutely nothing.

here is the exception i get in the console in netbeans
 
Frederik Nielsen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have just figured out why nothing was happening.

It was because of the jQuery AJAX.

The problem is that i have a form that has a jQuery post method like this:



When the servlet or filter throws an exception it will be ajax that receives it and then throws it in the toilet.

Any idea of a solution for this? (the solution cant be to remove all the jQuery, because that would just kill everything on the site)
 
Frederik Nielsen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think i am getting close to having solved this.

I have


below the success in the AJAX i have posted above.

It shows
<h1>javax.servlet.ServletException: exception</h1>
in the alert (and the rest of the error.jsp page including a full stacktrace.)

but when it redirects it shows
<h1>org.apache.jasper.JasperException: java.lang.NullPointerException</h1>

So it has lost the original exception cause, and lost most of the stacktrace

Any ideas to how i can preserve it ?

 
Bear Bibeault
Sheriff
Posts: 67746
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
Well. yeah. When you redirect to the error page on your own, it creates a new request and no error information is available. The result of the error processing is sent to you as the Ajax response which you are alerting.
 
Frederik Nielsen
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks mate for all your replies. I would probably have given up if it wasnt for you :)

I figured it out.
I just needed to change the code on the error.jsp page to the following



So now it can finally handle exceptions :)
 
reply
    Bookmark Topic Watch Topic
  • New Topic