Servlets - SPLessons

Servlet Interface

Chapter 16

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Servlet Interface

Servlet Interface

shape Description

Servlet Interface, When the Servlet is deployed in the server the Servlet container creates life cycle of the Servlet. The central abstraction in the Servlet API is the Servlet interface, all the Servlets have to implement this interface either directly or by extending a class such as GenericServlet, HttpServlet. Following are the different methods of servlet interface.

The following steps show the different methods used in Servlet Life Cycle.

shape Conceptual

shape Methods

Loading the servlet class: When the web container gets the request from the browser for servlet then servlet class will be loaded.

Creating servlet instance: The web container creates an object for the servlet when servlet class is loaded, here the important point is an object will be created in life cycle only once.

init method: This method will be called by the web container after an object was created to the servlet. The functionality of the init() method is to initialize the servlet, following is the regular syntax to declare the init() method.

public void init(ServletConfig config) throws ServletException  

service method: The service() method will be called by web container when servlet will have the request, in case servlet is not initialized then above methods will be utilized. In the life cycle of servlet this method will be utilized only once. Following is a syntax to declare the service() method.

public void service(ServletRequest request, ServletResponse response)   
  throws ServletException, IOException  

destroy method: Web container is the responsibility to call this method utilized while vanishing an object of the Servlet from the service, it provides the chance for servlet to clean up the memory or junk data. Following is the Syntax for the destroy method.

public void destroy()

Following is the code structure of life cycle methods.

shape Example

package servletinterface;

import javax.servlet.*;

public class First implements Servlet{
ServletConfig config=null;

public void init(ServletConfig config){
System.out.println("servlet is initialized");

public void service(ServletRequest req,ServletResponse res)
throws IOException,ServletException{


PrintWriter out=res.getWriter();
out.print("<b>Hey man, welcome to SPLesons.</b>");

public void destroy(){System.out.println("servlet is destroyed");}
public ServletConfig getServletConfig(){return config;}
public String getServletInfo(){return "copyright 2007-1010";}


Sample rule of WEB.XML file as follows.





<center><a href="./hello">Invoke Generic Servlet</a></center>


By compiling the program the follolwing output get generated.

By clicking on the link following output will be generated.


shape Key Points

  • Servlet Interface – The init() will be called by web container only once.
  • The destroy() cleans the junk files to free up the memory.
  • The destroy() will be called only if the servlet is unloaded.