• 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

Handling exceptions in init() and some opinions...

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
I have a big gap in my knowledge concerning exception handling and I was hoping someone would help me out. I'm working on a servlet that does some work in the init() and could potentially throw several exceptions. If this does happen, I'd like to show the user a good error page with contact info on it instead of a stacktrace. How can I do this without the request/response available to me? I must be missing something...
Also - just for kicks if you happen to read this - any opinions on using Singletons? I need to share an object between several servlets and I thought I'd just store it in the context but one of my co-workers thinks I should make it a Singleton.
Thanks so much!
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I recall, if the init method fails, the servlet engine will consider the servlet unavailable.
Therefore, you would have to catch the exception and set a flag in the servlet so that it could send the error message instead of performing the normal function.
Seems to me that storing a single object in the context amounts to a Singleton, assuming that you have an initial creation mechanism that won't make more than one.
Bill
 
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'm working on a servlet that does some work in the init() and could potentially throw several exceptions. If this does happen, I'd like to show the user a good error page with contact info on it instead of a stacktrace. How can I do this without the request/response available to me? I must be missing something...


Since there is no request/response there really isn't a page for you to display to the user, and what user would you show it to?
init() executes upon servlet load which can happen as early as when the servlet container is starting up, long before any users could potentially be hitting your app.
What kind of work are you performing in the inti() method that warrants user feedback? Would it perhaps be more approriate to perform this work in the context of a request? And if it truly is init()-type of work, who should see the failure? Your users, or the server admin?
hth,
bear
[ March 06, 2003: Message edited by: Bear Bibeault ]
 
You know it is dark times when the trees riot. I think this tiny ad is their leader:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic