Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can some please explain this?

 
A Harry
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just testing the effects of throwing a ServletException from my doPost method to make sure my error.jsp page handle it -

public class UploadServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException
{
throw new ServletException("test exception");
}
}

this works & my error page is called BUT the exception stack trace is output to the console regardless - surely if I'm handling the exception this shouldn't happen? - how can I suppress it's output? or am I doing something wrong!

thanks

harry
 
Mahmoud Hadad
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have you defined an erroe-page header in your deployment descriptor?
 
A Harry
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mahmoud, I have this in my web.xml file

<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this works & my error page is called BUT the exception stack trace is output to the console regardless

So it is output to the console, is this a problem?
You're not actaully managing the Exception, you're allowing the container to manage it, and this is what it is doing. If you don't want it in the console, catch the exception and redirect to the error page instead.

Dave
 
A Harry
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that David, just one question though, "redirect to the error page instead", I assume you mean using response.sendRedirect("error.jsp")?, won't that lose the exception object from which I want to tell what sort of exception occured? (sorry for being a bit thick!)

thanks

harry
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it would. If you want to preserve the request environment you would forward rather than redirect.

But, what's your issue with letting the container do it's own thing? Why do you not want the errors to appear on the console? That's a very desirable thing in my view.
 
A Harry
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The situation is this -

I have a struts app which I have to provide a file upload facility. Because I have my Form using enctype="multipart/form-data" the Struts framework generates this warning -

"One of the getParameter family of methods called after reading from the
ServletInputStream. Not merging post parameters"

This is because I beleive Struts use's getParameter() in it's standard framework. This however has no adverse effect in my app but obviously I would prefer it not to happen. So hence trying to do the upload using a Servlet cutting out Struts. This works fine except for the error handling!

From my session facade I throw an Exception when there is a problem with the uploaded file i.e InvalidZIPFileException extends Exception. It's this InvalidZIPFileException that I want to pass to my error page & display a custom error message.

I hope this makes some sense?

harry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic