wood burning stoves 2.0*
The moose likes Servlets and the fly likes Handling exceptions in init() and some opinions... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Handling exceptions in init() and some opinions..." Watch "Handling exceptions in init() and some opinions..." New topic
Author

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

Stephanie Spike
Greenhorn

Joined: Jan 24, 2003
Posts: 11
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

Joined: Mar 22, 2000
Posts: 12806
    
    5
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

Joined: Jan 10, 2002
Posts: 61434
    
  67


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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Handling exceptions in init() and some opinions...