1. It is the base interface for Servlet programming.
  2. Every Servlet has to implement this interface either directly or indirectly
  3. Servlet interface defines the most common methods including life cycle methods, which can be applicable for any servlet obj.

The following are the methods available in Servlet interface:

  1. init()
  2. service()
  3. destroy()
  4. getServletConfig()
  5. getServletInfo()

init()

public void init(servletConfig conf) throws ServletException. 

This method will be executed only once,to perform initialization activities immediately after instantiation. servlet container can’t place servlet into service if

  1. init() method throws servlet exception
  2. If init() method doesn’t return within the time specified by the web server.
  3. If initialization fails, web container makes that servlet object immediately eligible for garbage collection without calling any other life cycle method.

Before processing any client request, init() method should be completed.We can override init() method in our servlet class,if we have any initialization code.

service()

public void service(ServletRequest req,ServletResponse resp)
throws ServletException,IOException
  1. This method will be executed for every client request to provide response to the end user
  2. Entire service logic we have to define in this method only.
  3. service() method should be executed after completion of init() method.

destroy()

public void destroy()
  1. destroy() method will be executed only once to perform clean up activities ,just before removing a servlet from service.
  2. This is usually happens, when ever web container needs some free memory or at the time of application un deployment or at the time of server shutdown.
  3. Whenever web container calls destroy() method ,it will wait until all threads in service() method completes their execution.
  4. While executing destroy() method if any exception raised,it is simply ignored by web container.

getServletConfig()

public ServletConfig getServletConfig()
  1. This method returns ServletConfig object which provides entire servlet configuration information.

getServletInfo()

public String getServletInfo()
  1. This method returns information about our servlet like author ,copyright info,description etc…