Can you stop a webapp from deploying inside a servlet init()

I have a situation where certain exceptions:

java.security.NoSuchAlgorithmException

java.io.UnsupportedEncodingException

can occur if the JDK is not configured as expected.

I'd like to catch these and halt my webapp's deployment during the initialization of my webapp.

Is there a 'correct' way to do this? Perhaps inside of a servlet init() method?

[389 byte] By [Dave.Bickforda] at [2007-11-26 14:26:47]
# 1
If you have exception throw in init() during servlet initialization, your webapp won't start and therefore not deployable automattically. Are you sure you ask the right question?
thebugslayera at 2007-7-8 2:20:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

This doesn't seem to be the case under Tomcat 5.5.20. With the following init

public void init(ServletConfig config) throws ServletException {

super.init(config);

throw new Error("This error ought to take down the webapp");

}

it just logs that Error as SEVERE exception and continues deploying the rest of my web-app.

Dave.Bickforda at 2007-7-8 2:20:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> If you have exception throw in init() during servlet

> initialization, your webapp won't start and therefore

> not deployable automattically. Are you sure you ask

> the right question?

Actually, if you throw an UnavailableException in your servlet's init() method then the container will mark the servlet as unavailable. You could probably do this from the doGet() and doPost() method as well, I haven't bothered to look that up in the Servlet specification. But you'd have to do that in every servlet to make the entire application unavailable -- unless you were using the Front Controller pattern and all requests were funnelled through a single servlet.

DrClapa at 2007-7-8 2:20:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
If you can figure out what this guy is doing you'll be all set: http://forum.java.sun.com/thread.jspa?threadID=5122772&tstart=0
tolmanka at 2007-7-8 2:20:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

I've solved this problem, though I am not 100% sure that I'm using the best practice.

I had been misusing a servlet's init method to perform initializations that belonged in a ServletContextListener. Simply moving that code into a ServletcontextListener's contextInitialized method resolved the issue.

Firing an Error during contextInitialized() triggers a webapp to be undeployed with relative grace. (At least it does in Tomcat 5.5.20)

import javax.servlet.ServletContextEvent;

public class TestServletContextListener implements javax.servlet.ServletContextListener {

public TestServletContextListener() {

}

public void contextInitialized(ServletContextEvent servletContextEvent) {

try {

// Do my configuration checking things here

} catch(Exception e) {

throw new Error("Webapp initialization failure due to felonious configuration issue.");

}

}

public void contextDestroyed(ServletContextEvent servletContextEvent) {

}

}

Thanks for the comments

Dave.Bickforda at 2007-7-8 2:20:10 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...