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

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

    -> One common feature is, both these Classes are Abstract Classes.

    -> GenericServlet is a super class of HttpServlet class.

    -> The main difference is that, HttpServlet is a protocol dependent whereas GenericServlet is protocol independent. So GenericServlet can handle all types of protocols, but HttpServlet handle only HTTP specific protocols.

    -> GenericServlet belongs to javax.servlet package. HttpServlet belongs to javax.servlet.http package

    -> GenericServlet is an abstract class which extends Object and implements Servlet, ServletConfig and java.io.Serializable interfaces. HttpServlet is an abstract class which extends GenericServlet and implements java.io.Serializable interface.

    -> GenericServlet supports only service() method does not contain doGet() and doPost() methods. HttpServlet support also doGet(), doPost(), doHead() methods (HTTP 1.0) plus doPut(), doOptions(), doDelete(), doTrace() methods (HTTP 1.1).

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

    javax.servlet.Servlet is interface, it defines methods for all the implementations - that's what interfaces usually do.

    javax.servlet.GenericServlet is protocol independent. It is abstract, so it is not to be directly instantiated. It is usable class to extend if you some day have to write servlet for protocol other than HTTP.

    javax.servlet.http.HttpServlet is abstract class to be extended if you want to communicate over HTTP protocol. Most likely you only have to care about this one.

    More exact information you can find behind the links.

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

    HttpServlet is specific to the HTTP protocol and hence it supplies methods for the HTTP verbs: doGet, doPost, etc, and a version of the generic service method that takes HTTP-specific request and response objects. It is a special type of Servlet which is actually a pretty minimal interface.

    GenericServlet is the basic, protocol-neutral implementation of the Servlet interface. Often you'll find similar basic implementations of interfaces in an API; in this case GenericServlet adds a bit of functionality to the Servlet API: getServletName, getServletInfo, and pass-through methods for the servlet init parameters. HttpServlet benefits from these additions by extending GenericServlet.

    Generally everyone coding against this API is using HttpServlet for implementing HTTP web services, but one can also extend or use GenericServlet for implementing server/service functionality using a custom protocol, or another extant protocol, for example, FTP.

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

    Servlet is an interface which contains five abstract methods in order use servlet we have to provide an implementation for all these five methods, which is a headache. In order to avoid this complexity, we have GenericServlet and HttpServlet for next level.

    GenericServlet is protocol independent, it means it can accept any protocol request. GenericServlet can forward and include a request but we can not redirect the request. Session Management with cookies and HttpSession is not possible in GenericServlet. In GenericServlet it is not possible to define separate logic for get and post request.

    HttpServlet is protocol dependent. it means, it accepts only HTTP protocol request. HttpServlet can forward and include and redirect a request. Session Management with cookies and HttpSession is possible in HttpServlet. In HttpServlet it is possible to define separate logic for get and post request.

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

    javax.servlet

    Servlet is a server-side web technology. As the name implies, it serves a client request and receives a response from the server. You have to implement javax.Servlet (Interface) to handle a servlet work.

    javax.servlet.GenericServlet

    Signature:

    public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable
    
    1. GenericServlet defines a generic, protocol-independent servlet.
    2. GenericServlet gives a blueprint and makes writing servlet easier.
    3. GenericServlet provides simple versions of the life-cycle methods init and destroy and of the methods in the ServletConfig interface.
    4. GenericServlet implements the log method, declared in the ServletContext interface.
    5. To write a generic servlet, it is sufficient to override the abstract service() method.

    javax.servlet.http.HttpServlet

    Signature:

    public abstract class HttpServlet extends GenericServlet implements java.io.Serializable
    
    1. HttpServlet defines a HTTP protocol specific servlet.
    2. HttpServlet gives a blueprint for Http servlet and makes writing them easier.
    3. HttpServlet extends the GenericServlet and hence inherits the properties GenericServlet.
    0 讨论(0)
  • 2020-12-23 15:37

    Servlet:-

    1. The Servlets runs as a thread in a web-container instead of in a seperate OS process.
    2. Only one object is created first time when first request comes, other request share the same object.
    3. Servlet is platform independent.
    4. Servlet is fast.

    GenericServlet:-

    1. General for all protocol.
    2. Implements Servlet Interface.
    3. Use Service method.

    HttpServlet:-

    1. Only for HTTP Protocol.
    2. Inherit GenericServlet class.
    3. Use doPost, doGet method instead of service method.
    0 讨论(0)
提交回复
热议问题