• 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

Display error message from Java Helper class

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67747
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
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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic