Servlet Interface Example

The Servlet interface is the central abstraction of the Java Servlet API. All servlets implement this interface either directly, or more commonly, by extending a class that implements the interface. The two classes in the Java Servlet API that implement the Servlet interface are GenericServlet and HttpServlet. For most purposes, Developers will extend HttpServlet to implement their servlets.
This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:
  1. The servlet is constructed, then initialized with the init method.
  2.  Requests from the Client are handled by the service method.
  3. The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.
Below diagram shows the life-cycle methods of Servlet:

Servlet interface class diagram

The below diagram shows the two classes in the Java Servlet API that implement the Servlet interface are GenericServlet and HttpServlet.
Note that HttpServlet indirectly (indirectly implementation means extending those classes that implement Servlet interface, These classes are GenericServlet and HttpServlet) implements the Servlet interface. HttpServlet class extends GenericServlet and provides an implementation of life cycle methods to handle HTTP requests.

Servlet interface methods

Below class diagram shows a list of Servlet interface methods:

From Servlet 4.0.1, the Servlet interface provides below list of methods:
  • void destroy() - Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
  • ServletConfig getServletConfig() - Returns a ServletConfig object, which contains initialization and startup parameters for this servlet.
  • String getServletInfo() - Returns information about the servlet, such as author, version, and copyright.
  • void init(ServletConfig config) - Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
  • void service(ServletRequest req, ServletResponse res) - Called by the servlet container to allow the servlet to respond to a request.

The init( ), service( ), and destroy( ) methods are the life cycle methods of the servlet. These are invoked by the server. The getServletConfig( ) method is called by the servlet to obtain initialization parameters. A servlet developer overrides the getServletInfo( ) method to provide a string with useful
information (for example, the version number). This method is also invoked by the server.

Servlet interface example

Let's add servlet 4.0.1 dependency to pom.xml:
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
If your project is not a maven project then you can download the jar file and keep in the classpath.
In this example, we create ServletImpl which implements Servlet interface and it's all methods. Let's use @WebServlet annotation to define Servlet component so that web.xml is not required.
package net.javaguides.servlet.tutorial;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;

@WebServlet("/ServletInterface")
public class ServletImpl implements Servlet {

    private ServletConfig config = null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        this.config = config;
        System.out.println("iniside init method : " + config.getServletName());

    }

    @Override
    public ServletConfig getServletConfig() {
        return this.config;
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        System.out.println("inside service method");
        res.setContentType("text/html");

        PrintWriter out = res.getWriter();
        out.print("<html><body>");
        out.print("<b> Servlet Interface Example</b>");
        out.print("</body></html>");
    }

    @Override
    public String getServletInfo() {
        return "Demonstrated implementaton of Servlet interface";
    }

    @Override
    public void destroy() {
        System.out.println("inside destroy method");
    }
}
Output: Let's deploy above servlet in a web container like tomcat 8 and hit http://localhost:8080/java-servlet-tutorial/ServletInterface link in a browser will result below web page:

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments