Servlet Interface

Servlet Interface provides the common methods which needs to be implemented by all the servlets. All the servlets must implement this interface directly or indirectly. To have the implementation of the Servlet Interface you can extend GenericServlet Class(javax.servlet.GenericServlet) or HttpServlet Class (javax.servlet.http.HttpServlet).  Read through Running Your First Servlet Application article for the basic understanding. 

Methods of Servlet Interface

  1. public void init(ServletConfig config) : This method initializes the servlet and will be called by the servlet container after instatiating the servlet. This method will be called only once.
  2. public void service(ServletRequest request,ServletResponse response) : This method process the request and provides the response back. This method will be called for every request from the web container.
  3. public void destroy() : This method indicates the end of the servlet lifecycle. This method will be called only once
  4. public ServletConfig getServletConfig() : This method returns back the ServletConfig object. It is used to get configuration information from web.xml file.
  5. public String getServletInfo() : Returns information about servlet such as writer, version etc.

Servlet Example Implementing Servlet Interface

Lets see the below Servlet Example which implements the Servlet Interface

import javax.servlet.*;

public class ServletInterfaceExample implements Servlet{
   ServletConfig config=null;
   public void init(ServletConfig config){
      System.out.println("Initialization the Servlet");

   public void service(ServletRequest req,ServletResponse res)
   throws IOException,ServletException{
       PrintWriter out=res.getWriter();
       out.print("<h2>Welcome to Servlet Interface Example!!!</h2>");
   public void destroy(){
       System.out.println("End of the Servlet lifecycle");
   public ServletConfig getServletConfig(){
       return config;
   public String getServletInfo(){
       return "Servlet Example Using Servlet Interface";


The deployment descriptor will be in XML format and called as web.xml, which sould be placed in the WEB-INF directory of the Servlet application.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">


To run our ServletInterfaceExample application hit on the below url


We will get the below output displayed in the browser

Welcome to Servlet Interface Example!!!

Leave a Reply

Your email address will not be published. Required fields are marked *