Discussions

Web tier: servlets, JSP, Web frameworks: What to do in case of servlet initialization failure ?

  1. I have a web application in which I will want to successfully initialize all servlets before making it available to users. As such I will want all servlets which access a database to make their connections in their init() methods, and if these don't succeed I want to alert the administrator, log the errors, and disallow access to the servlets. Is there a standard way of doing this ?

    I have thought that I could load all servlets on startup which would call the init() methods in which the database connections are made. If any of these fail then I will want to disallow access to any of the servlets of the web application. Is there a way of disabling these servlets programmatically, or am I limited to paying attention to the console and error logs when I deploy the web application, and undeploy if any errors are encountered ?

    Thanks in advance for any feedback.


    -James
  2. If the servlet's init() method throws a ServletException, the servlet engine will disable that servlet, making it unavailable to end users. So, if your initialization logic fails, throw a ServletException.