Servlet Interface

In this tutorial you will learn about the Servlet Interface and its application with practical example.

Servlet Interface

A Servlet is small program which runs within a web server and public interface Servlet defines methods which is must implemented by all Servlet. For initialization a Servlet, For service requests and for removing a Servlet from the server this interface defines some methods. These methods are called life-cycle methods. Also it provides 2 non-life-cycle methods.

Table Of Contents

Servlet Interface Methods

These are 5 life-cycle and non-life-cycle methods in Servlet Interface.

public void init(ServletConfig config) :– This method is called by the Servlet container which indicates that Servlet is being placed into service. The Servlet container calls the init method only once post instantiating the Servlet. This is life-cycle method of Servlet.

public void service(ServletRequest request,ServletResponse response) :-

This method is called by the Servlet container which is used to allow the Servlet to respond to a request. This method is called post the init() method of Servlet.

public ServletConfig getServletConfig() :-

This method returns a ServletConfig object, which contains initialization and startup parameter for this Servlet.

public String getServletInfo() :– This method returns information about the Servlet, like author, version, and copyright. The string that this methods returns must be plain text.

public void destroy():- If the Servlet taken out from the service so this method is being called by the Servlet container. After calling method by Servlet container, it will not call the service method again on this Servlet.

Example:-

Let’s understand these all methods and Servlet Interface By below example.

Output:-

Output of this class will be like below image.

image003

In this tutorial we have learn about the Servlet Interface and its application with practical example. I hope you will like this tutorial.