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

前端 未结 10 1687
抹茶落季
抹茶落季 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:39

    -->GenericServlet can process multiple clients request from a single form. Whereas, HttpServlet can process multiple clients requesting from multiple HTML forms.

    --> GenericServlet is Stateless and HttpServlet is Stateful.

    0 讨论(0)
  • 2020-12-23 15:42

    "Exact difference" meaning what? The API lists the exact differences.

    Servlet is an interface defining what a servlet must implement.

    GenericServlet is just that, a generic, protocol-independent servlet.

    HttpServlet is a servlet tied specifically to the HTTP protocol.

    Are you asking when you'd use any of those?

    In general, you'd extend HttpServlet to implement an application's web layer.

    You might implement Servlet if you're writing your own container or handling everything yourself. You might extend GenericServlet to handle a different protocol, but you might not.

    0 讨论(0)
  • 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
    0 讨论(0)
  • 2020-12-23 15:43

    All classes, interfaces, and methods present in the javax.servlet package are protocol independent (generic to all protocols).

    In contrast, all classes, interfaces, and methods present in the javax.servlet.http package are protocol dependent (specific to the HTTP protocol)

    0 讨论(0)
提交回复
热议问题