Servlet Interface


Servlet interface offers common behavior to all of the servlets. The Servlet interface defines strategies that each one servlets needs to implement.

Servlet interface desires to be carried out for developing any servlet (both immediately or indirectly). Also, It offers three lifestyle cycle strategies which can be used to initiate the servlet, to carry the requests, and to wreck the servlet and a pair of non-lifestyles cycle strategies.

Methods of Servlet interface

There are five strategies in the Servlet interface. The init, carrier and wreck are the lifestyles cycle strategies of servlet. These are invoked with the aid of using the net field.

Method Description



public void init(ServletConfig config)

initializes the servlet. It is the lifestyle cycle technique of servlet and invoked with the aid of the web container only once.

public void carrier(ServletRequest request,ServletResponse response)

gives a response for the incoming request. It is invoked at each and every request by the web container.

public void destroy()

is invoked handiest once and shows that servlet is being destroyed.

public ServletConfig getServletConfig()

returns the item of ServletConfig.

public String getServletInfo()

returns records approximately servlet like writer, copyright, version etc.

Example of Servlet carry through Servlet Interface

Easy instance of servlet with the help of the servlet interface is given below:

File Name: First.java

import java.io.*;  
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>hello simple servlet</b>");  
public void destroy(){System.out.println("servlet is destroyed");}  
public ServletConfig getServletConfig(){return config;}  
public String getServletInfo(){return "copyright 2007-1010";}