• Post Reply Bookmark Topic Watch Topic
  • New Topic

Display error message from Java Helper class

 
Joseph Smithern
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Servlet that is working with a database where the connection is called from a Java class file and it works great.
The only issue is if there is anything wrong with connection or any other methods in the Java class file it prints the error message to my Tomcat 4.1.27 console and doesnt give an output on the web page.

I tried using:
context.getRequestDispatcher("/otherpage.jsp").forward(req, res);
and
response.sendRedirect("/otherpage.jsp");
and they both dont work because the Java Helper class wont compile due to type issue where it seems to be missing Response object or something which is normally in a Servlet but this Java helper class is not a servlet.

Please advise how to get catch statements in my Java helper class to output exception messages to a web browser?? Or if that is not possible what are my options so I can let the users know if there is any issues with my Database connection and other methods in the Java helper class file?

Java helper class:



Servlet where I call constructor and a Java helper class.
The sendRedirect works in Servlet but why wont it work in my above Java class file??

[ April 16, 2007: Message edited by: Joseph Smithern ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you need the request/response objects to redirect to another page.
I think you'd better make a custom application exception and throw it from your Builder class when an error occurs. In the servlet, catch that exception and redirect accordingly.

What you could also do is to define some error pages in your deployment descriptor. Use the error-page tag and the exception-type tag inside.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your question is very confusing as you talk about this as if it's a run-time problem, but then you say

they both dont work because the Java Helper class wont compile


Your classes should all be compiled and deployed before you even think about starting the application.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you try passing the HTTP request and HTTP response objects to the helper class then this might work.But the problem would be that your helper class would be tightly tied with the servelt environment.

I think if the making of a connection in the helper class fails , then it should store the exception message in some place and return null.In the caller server class you can check for whether the connection returned in null.In case its null then it should try to find the error message in the place where you have stored it.Retrieve it from that place an put it as parameter to the request and forward this to a error jsp , which would display the error message after retrieving it from the request to the user.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!