Hi,
You can't do it from a servlet's init method.  System.exit is an ugly
solution: what if there are other webapps on the container?

Two options come to mind:
- Move this init code to a ServletContextListener's contextInitialized
method.  If an error occurs, throw a RuntimeException (or any subclass
thereof).  The container will mark the context as unavailable and
requests will be rejected with a 500-level response.

- Have the init servlet set some sort of marker variable in a singleton,
making the app unavailable.  Have a filter mapped to /* check this
marker variable for every request, and reject the request in whatever
way you see fit if the app is marked as unavailable.

Whatever you do, remember to handle the case of a webapp reload
appropriately.  (This is one reason why the ContextListener approach is
good).

Yoav Shapira
Millennium Research Informatics


>-----Original Message-----
>From: Josh Rehman [mailto:[EMAIL PROTECTED]
>Sent: Monday, April 12, 2004 8:13 PM
>To: Tomcat Users List
>Subject: Prevent context startup on servlet init failure?
>
>Is there a way to prevent the context from starting up if a servlet
>init() call throws an exception or otherwise fails?
>
>Here's the situation: we have a utility servlet that loads first (using
><load-on-startup>) and accesses some external resources, checks
database
>connections, etc. Then our main application servlet loads. In the case
>where the external dependancy fails, the context still comes up and
>fails at a later point. It would be better practice to have the context
>load fail when our utility servlet init() method throws it's exception.
>
>I was thinking that there might be something in ServletContext that
>would allow the servlet to force the context to dump out. Or perhaps a
>call to System.exit(1) would be appropriate.
>
>I'd really appreciate some advice. Thanks.
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]




This e-mail, including any attachments, is a confidential business communication, and 
may contain information that is confidential, proprietary and/or privileged.  This 
e-mail is intended only for the individual(s) to whom it is addressed, and may not be 
saved, copied, printed, disclosed or used by anyone else.  If you are not the(an) 
intended recipient, please immediately delete this e-mail from your computer system 
and notify the sender.  Thank you.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to