aspose file tools*
The moose likes JSP and the fly likes JSP/FilterServlet/ControllerServlet Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP/FilterServlet/ControllerServlet Exception Handling" Watch "JSP/FilterServlet/ControllerServlet Exception Handling" New topic
Author

JSP/FilterServlet/ControllerServlet Exception Handling

Frederik Nielsen
Greenhorn

Joined: Aug 25, 2013
Posts: 14
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Frederik Nielsen
Greenhorn

Joined: Aug 25, 2013
Posts: 14
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

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

Joined: Aug 25, 2013
Posts: 14
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

Joined: Aug 25, 2013
Posts: 14
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

Joined: Aug 25, 2013
Posts: 14
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60077
    
  65

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

Joined: Aug 25, 2013
Posts: 14
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 :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP/FilterServlet/ControllerServlet Exception Handling
 
Similar Threads
[U]Problem retrieving values from a database in a seating arrangement web application
Struts 1.1 forward mapping problem
Tests for the Data class/locking mechanism
Jquery Event Sequence
How to Populate TextField result in another Textfield on same page..using struts2 jquery AJAX