- It is the base interface for Servlet programming.
- Every Servlet has to implement this interface either directly or indirectly
- 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:
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
- init() method throws servlet exception
- If init() method doesn’t return within the time specified by the web server.
- 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.
public void service(ServletRequest req,ServletResponse resp) throws ServletException,IOException
- This method will be executed for every client request to provide response to the end user
- Entire service logic we have to define in this method only.
- service() method should be executed after completion of init() method.
public void destroy()
- destroy() method will be executed only once to perform clean up activities ,just before removing a servlet from service.
- 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.
- Whenever web container calls destroy() method ,it will wait until all threads in service() method completes their execution.
- While executing destroy() method if any exception raised,it is simply ignored by web container.
public ServletConfig getServletConfig()
- This method returns ServletConfig object which provides entire servlet configuration information.
public String getServletInfo()
- This method returns information about our servlet like author ,copyright info,description etc…