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

ServletInterfaceExample.java

Lets see the below Servlet Example which implements the Servlet Interface

import java.io.*;
import javax.servlet.*;

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

   public void service(ServletRequest req,ServletResponse res)
   throws IOException,ServletException{
       res.setContentType("text/html");
       PrintWriter out=res.getWriter();
       out.print("<html>");
       out.print("<body>");
       out.print("<h2>Welcome to Servlet Interface Example!!!</h2>");
       out.print("</body>");
       out.print("</html>");
   }
   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";
   }
}

web.xml

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="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
     <display-name>ServletsInterfaceExample</display-name>
     <servlet>
         <servlet-name>ServletInterfaceExample</servlet-name>
         <servlet-class>com.javainterviewpoint.ServletInterfaceExample</servlet-class>
     </servlet>
     <servlet-mapping>
         <servlet-name>ServletInterfaceExample</servlet-name>
         <url-pattern>/ServletInterfaceExample</url-pattern>
     </servlet-mapping>
</web-app>

Output

To run our ServletInterfaceExample application hit on the below url

http://localhost:8080/ServletTutorial/ServletInterfaceExample

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 *