Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the servlet's init() method used for?

Tags:

java

servlets

When I decompile the GenericServlet and check the init() , I see the following code .

public void init(ServletConfig servletconfig)
    throws ServletException
{
    config = servletconfig;
    init();
}

public void init()
    throws ServletException
{
}

What is the init method actually doing here ? Am I missing something ?

like image 527
Vinoth Kumar C M Avatar asked Feb 28 '11 09:02

Vinoth Kumar C M


4 Answers

Yes, it does nothing. It could have been abstract, but then each servlet would be forced to implement it. This way, by default, nothing happens on init(), and each servlet can override this behaviour. For example, you have two servlets:

public PropertiesServlet extends HttpServlet {

   private Properties properties;

   @Override
   public void init() {
       // load properties from disk, do be used by subsequent doGet() calls
   }
}

and

public AnotherServlet extends HttpServlet {

   // you don't need any initialization here, 
   // so you don't override the init method.
}
like image 101
Bozho Avatar answered Nov 02 '22 07:11

Bozho


From the javadoc:

/**
 *
 * A convenience method which can be overridden so that there's no need
 * to call <code>super.init(config)</code>.
 *
 * <p>Instead of overriding {@link #init(ServletConfig)}, simply override
 * this method and it will be called by
 * <code>GenericServlet.init(ServletConfig config)</code>.
 * The <code>ServletConfig</code> object can still be retrieved via {@link
 * #getServletConfig}. 
 *
 * @exception ServletException  if an exception occurs that
 *                  interrupts the servlet's
 *                  normal operation
 *
 */

So it does nothing and is just a convenience.

like image 22
bert Avatar answered Nov 02 '22 09:11

bert


Constructor might not have access to ServletConfig since container have not called init(ServletConfig config) method.

init() method is inherited from GenericServlet which has a ServletConfig as its property. thats how HttpServletand what ever custom servlet you write by extending HttpServlet gets ServletConfig.

and GenericServlet implements ServletConfig which has getServletContext method. so your custom servlets init method will have access to both of those.

like image 43
Nusky Azhar Avatar answered Nov 02 '22 08:11

Nusky Azhar


Servlet container calls servlet init() method before handling client requests. It is called just one times after servlet is created. By default it does nothing. You can override this method and it is also good for performing one-time activities. Such as database connection or reading configuration data etc.

public void init(ServletConfig config) throws ServletException {
     super.init(config);

     // You can define your initial parameter in web.xml file.
     String initialParameter = config.getInitParameter("initialParameter");
     // Do some stuff with initial parameters

}

What happens if init() method throw an exception?

The servlet destroy() will not be called because it is unsuccesfull initialization. Servlet container may try to instantiate and initialize a new instance of this failed servlet later.

like image 37
Harun ERGUL Avatar answered Nov 02 '22 09:11

Harun ERGUL