HttpServlet class is declared abstract?

HttpServlet class is declared abstract?

The HttpServlet class is declared abstract in Java because it serves as a blueprint for creating HTTP servlets. The purpose of HttpServlet is to provide a basic framework for handling HTTP requests (GET, POST, PUT, DELETE, etc.) and responses, but it doesn’t provide concrete implementations for these methods.

HttpServlet class

Key reasons for declaring HttpServlet as abstract include

1.  Partial Implementation

   HttpServlet provides a partial implementation for the Servlet interface. It implements some methods but leaves others to be implemented by subclasses.

2.  Template for Subclasses

   By declaring the class abstract, it enforces that developers must subclass HttpServlet and provide specific implementations for methods such as doGet(), doPost(), etc. This ensures that each servlet handles requests appropriately for its specific context.

3.  Avoid Instantiation

   Abstract classes cannot be instantiated directly. This prevents the creation of generic HttpServlet objects that lack the necessary implementations for handling specific types of requests.

Custom Servlet Extending HttpServlet

Here is an example of a custom servlet that extends HttpServlet and provides implementations for the doGet() and doPost() methods.

Custom Servlet:

java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/customServlet")
public class CustomServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        response.getWriter().println("<html><body>");
        response.getWriter().println("<h2>Hello from doGet method</h2>");
        response.getWriter().println("</body></html>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        response.getWriter().println("<html><body>");
        response.getWriter().println("<h2>Hello from doPost method</h2>");
        response.getWriter().println("</body></html>");
    }
}

Explanation of the HttpServlet class

  • Custom Servlet:
  • The CustomServlet class extends HttpServlet and provides implementations for the doGet() and doPost() methods.
  • When an HTTP GET request is received, the doGet() method generates a simple HTML response.
  • When an HTTP POST request is received, the doPost() method generates a similar HTML response.

Summary

  • HttpServlet Class:
  • Declared abstract to enforce subclassing.
  • Provides a framework for handling HTTP requests.
  • Requires concrete implementations of request-handling methods (e.g., doGet(), doPost()).

Homepage

Readmore