The GenericServlet Vs HttpServlet section will help you to know the basic and most important difference between them. But Interviewers usually ask these types of questions in interviews for Java Developers.
By using both GenericServlet and HttpServlet we create servlets but the main difference between GenericServlet and HttpServlet is that GenericServlet is protocol-independent while HttpServlet is protocol-dependent.
GenericServlet Vs HttpServlet
The primary difference between HttpServlet and GenericServlet is that HttpServlet is protocol dependent and used only with HTTP protocol whereas GenericServlet is protocol independent which can be used with any protocol.
GenericServlet belongs to javax.servlet package but HttpServlet belongs to javax.servlet.http package.
HttpServlet is the most commonly used servlet by Java programmers whereas Servlet is not commonly used.
GenericServlet allows all types of protocols and has only one abstract method which is service() method and other four methods are concrete method whereas HttpServlet is a class which is specific and allows only http protocol and other protocol like FTP doesn’t allow SMTP.
|1.||GenericServlet defines a generic and protocol-independent servlet.||But HttpServlet defines a HTTP protocol specific servlet.|
|2.||In GenericServlet, the service() method is abstract.||But service() method in HttpServlet is non-abstract.|
|3.||GenericServlet extends Object class and implements Servlet, ServletConfig, and Serializable interfaces.||But HttpServlet extends GenericServlet and implements a Serializable interface.|
|4.||GenericServlet supports only service() method which does not contain doGet() and doPost() methods.||HttpServlet also supports doGet(), doPost(), doHead(), doPut(), doOptions(), doDelete(), doTrace() methods.|
|5.||GenericServlet can process multiple clients request from a single form hence programmer doesn’t prefer this servlet.||HttpServlet can process multiple clients requesting from multiple HTML forms hence programmer prefer this servlet.|
|6.||GenericServlet is Stateless and is not commonly used servlet.||But HttpServlet is Stateful and it is most popular servlet because it is commonly used by the developer.|
After learning GenericServlet Vs HttpServlet, finally you are able to decide which servlet you should prefer for writing servlet program.