• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Stephanie Spike
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic