What is the difference between GenericServlet, HttpServlet and a Servlet?

前端 未结 10 1733
抹茶落季
抹茶落季 2020-12-23 15:16

I was searching for exact difference between javax.servlet.http.HttpServlet , javax.servlet.GenericServlet and javax.Servlet unable to

10条回答
  •  别那么骄傲
    2020-12-23 15:42

    1. Servlet interface
      Its the super interface for GenericServlet and HttpServlet. It contains 5 abstract methods and all inherited by GenericServlet and HttpServlet.
      • Problem
        If you want to create servlet by implementing Servlet interface all the 5 abstract methods of the interface Servlet should be overridden eventhough Programmer is not interested
      • Usage
        Only implement Servlet interface if you like to develop your own container.
    2. GenericServlet
      Its the immediate subclass of Servlet interface and super class of HttpServlet. In this class, one abstract method exist: service(). Other 4 abstract methods are implemented in this class.
      • Problem
        All methods are concrete except service() method, So you have to implement it as a callback method.
      • Usage
        It is protocol independent so Can be used with any protocol like FTP, SMTP, HTTP etc.
    3. HttpServlet
      Its subclass of both GenericServlet and Servlet interface. Immediate super class of HttpServlet is GenericServlet. HttpServlet does not contain any abstract method. Eventhough the HttpServlet does not contain any abstract methods, it is declared as abstract class by the Designers to not to allow the anyone to create an object directly because a Servlet object is created by the Servlet Container.
      • Problem
        HttpServlet is protocol dependent and used specific to HTTP protocol only.
      • Usage
        service() method need not be overridden. It can be replaced by doGet() or doPost() methods with the same parameters. Its most used method to create servlet.


        hierarchy
        Reference: way2java.com

提交回复
热议问题