What is the Difference Between GenericServlet and HttpServlet

The main difference between GenericServlet and HttpServlet is that the GenericServlet is protocol independent and can be used with any protocol such as HTTP, SMTP, FTP, and, CGI while HttpServlet is protocol dependent and only used with HTTP protocol.

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

Key Areas Covered

1. What is GenericServlet
     – Definition, Functionality
2. What is HttpServlet
     – Definition, Functionality
3. What is the Difference Between GenericServlet and HttpServlet
     – Comparison of Key Differences

Key Terms

GenericServlet, HttpServlet

Difference Between GenericServlet and HttpServlet - Comparison Summary

What is GenericServlet

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

Also, GenericServlet is used with protocols such as SMTP, CGI, FTP, HTTP, etc. Therefore, it is protocol independent. In other words, it can be used when the web was not standardized to HTTP protocol.

What is HttpServelt

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

Difference Between GenericServlet and HttpServlet

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

Difference Between GenericServlet and HttpServlet

Definition

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

Protocol dependency

A prime difference between GenericServlet and HttpServlet is that the GenericServlet is protocol-independent while the HttpServlet is protocol-dependent.

Service method

Also, in GenericServlet, the service method is abstract. However, in HttpServlet, the service method is non-abstract. Thus, this is another important difference between GenericServlet and HttpServlet.

Signature

Moreover, public abstract class GenericServlet extends java.lang.Object, and implements Servlet, ServletConfig, and, java.io.Serializable. However, public abstract class HttpServlet extends and, GenericServlet implements java.io.Serializable.

Associated Subclass

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

Defined Package

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

Extending / Implementing

Furthermore, GenericServlet extends Object class and implements 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 GenericServlet and HttpServlet is that the GenericServlet is protocol independent that can be used with any protocol such as HTTP, SMTP, FTP, CGI etc. while HttpServlet is protocol dependent and is only used with HTTP protocol.

Reference:

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

Image Courtesy:

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

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply