What is the Difference Between Generic Servlet and HTTP Servlet? with Proper Definition and Brief Explanation

The main difference between Generic Servlet and HTTP Servlet is that the Generic Servlet is protocol independent and can be used with any protocol like HTTP, SMTP, FTP and CGI whereas HTTP Servlet is protocol dependent and is only used with HTTP protocol.

Servlet is a Java technology that helps develop robust and scalable web applications. It is a platform independent server side component. Furthermore, it is possible to write a Servlet using three methods: by implementing the Servlet interface, by extending the Generic Servlet abstract class, or by extending the Http Servlet abstract class. The Servlet interface is the super interface for both the Generic Servlet and the Http Servlet. It has five abstract methods, and Generic Servlet and Http Servlet inherit these methods.

Key Areas Covered

1. What is Generic Servlet?
     – Definition, Functionality
2. What is Http Servlet?
     – Definition, Functionality
3. What is the difference between Generic Servlet and Http Servlet?
     – Key Differences Comparison

Key terms

Generic ServletHTTP Servlet

What is Generic Servlet

Generic Servlet is the immediate subclass of the Servlet interface. Namely; a method inherited from the Servlet interface called service() is an abstract method on Generic Servlet. The other four methods inherited from the Servlet interface have implementations in Generic Servlet. A programmer extending the Generic Servlet class should override the service() method and write the implementation for it.

Also, Generic Servlet is used with protocols like SMTP, CGI, FTP, HTTP, etc. Therefore, it is protocol independent. In other words, it can be used when the web was not standardized for the HTTP protocol.

What is Http Servelt?

Today most web applications use the HTTP protocol. Http Servlet is designed to support the HTTP protocol. It is also an abstract class. Also, the immediate superclass of HttpServlet is GenericServlet. HttpServlet overrides the service method on GenericServlet. It is possible to override the service (method using doGet() or doPost() with the same parameters of the service method).

Because HttpServlet is the subclass of GenericServlet, it inherits the properties and methods of GenericServlet. Therefore, when the programmer extends the HttpServlet, he can use the functionality of both classes.

Difference between GenericServlet and HttpServlet

Definition

GenericServlet is a class that implements the Servlet, ServletConfig and Serializable interfaces which provide the implementation of all the methods of these interfaces except the service method. HttpServlet is a class that extends the GenericServlet class and implements the Serializable interface that provides HTTP-specific methods. Thus, this indicates the fundamental difference between GenericServlet and HttpServlet.

Dependency Protocol

A fundamental difference between GenericServlet and HttpServlet is that GenericServlet is protocol independent while HttpServlet is protocol dependent.

Service Method

Also, in GenericServlet, the service method is abstract. However, in HttpServlet, the service method is not abstract. So, this is another important difference between GenericServlet and HttpServlet.

Signature

Additionally, the public abstract class GenericServlet extends java.lang.Object and implements Servlet, ServletConfig, and java.io.Serializable. However, the public abstract class HttpServlet is extended, and GenericServlet implements java.io.Serializable.

Associated Subclass

GenericServlet is the immediate subclass of the Servlet interface. HttpServlet, on the other hand, is the immediate subclass of GenericServlet.

Defined Package

The javax.servlet package defines the GenericServlet while the javax.servlet.http package defines the HttpServlet. This is another difference between GenericServlet and HttpServlet.

Extension / Implementation

Additionally, GenericServlet extends the object class and implements the Servlet, ServletConfig, and Serializable interfaces. HttpServlet extends GenericServelt and implements a Serializable interface.

Usability

Another difference between GenericServlet and HttpServlet is that the GenericServlet is not commonly used while the HttpServlet is commonly used.

Conclusion

The main difference between Generic Servlet and HttpServlet is that GenericServlet is a separate protocol that can be used with any protocol like HTTP, SMTP, FTP, CGI, etc.

Reference:

1. “GenericServlet Class in Servlet – Javatpoint.” www.javatpoint.com, available here.
2. “HttpServlet Class in Servlet – Javatpoint.” www.javatpoint.com, available here.

Courtesy image:

1. “Servlet” By Frederik Wahl – Own work (Public Domain) via Commons Wikimedia

See More:

Leave a Reply

Your email address will not be published.

CAPTCHA


Back to top button