What is Servlet vs GenericServlet vs HttpServlet?
Servlets are platform-independent server-side components, being written in Java. Before going for differences, first let us see how the three Servlet, GenericServlet, HttpServlet are related, their signatures and also at the end similarities.
See how to write a Servlet.
public class Validation extends HttpServlet
To write a servlet, everyone goes to extend the abstract class HttpServlet, like Frame is required to extend to create a frame.
Following figure shows the hierarchy of Servlet vs GenericServlet vs HttpServlet and to know from where HttpServlet comes.
Figure on Servlet vs GenericServlet vs HttpServlet
Observe the hierarchy and understand the relationship between the three (involved in multilevel inheritance). With the observation, a conclusion can be arrived, to write a Servlet three ways exist.
a) by implementing Servlet (it is interface)
b) by extending GenericServlet (it is abstract class)
c) by extending HttpServlet (it is abstract class)
The minus point of the first way is, all the 5 abstract methods of the interface Servlet should be overridden eventhough Programmer is not interested in all (like the interface WindowListener to close the frame). A smart approach is inheriting GenericServlet (like using WindowAdapter) and overriding its only one abstract method service(). It is enough to the programmer to override only this method. It is a callback method (called implicitly). Still better way is extending HttpServlet and need not to override any methods as HttpServlet contains no abstract methods. Eventhough the HttpServlet does not contain any abstract methods, it is declared as abstract class by the Designers to not to allow the Programmer to create an object directly because a Servlet object is created by the system (here system is Servlet Container).
1. Servlet interface
It is the super interface for the remaining two – GenericServlet and HttpServlet. It contains 5 abstract methods and all inherited by GenericServlet and HttpServlet. Programmers implement Servlet interface who would like to develop their own container.
It is the immediate subclass of Servlet interface. In this class, only one abstract method service() exist. Other 4 abstract methods of Servlet interface are given implementation (given body). Anyone who extends this GenericServlet should override service() method. It was used by the Programmers when the Web was not standardized to HTTP protocol. It is protocol independent; it can be used with any protocol, say, SMTP, FTP, CGI including HTTP etc.
public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable
When HTTP protocol was developed by W3C people to suit more Web requirements, the Servlet designers introduced HttpServlet to suit more for HTTP protocol. HttpServlet is protocol dependent and used specific to HTTP protocol only.
The immediate super class of HttpServlet is GenericServlet. HttpServlet overrides the service() method of GenericServlet. HttpServlet is abstract class but without any abstract methods.
With HttpServlet extension, service() method can be replaced by doGet() or doPost() with the same parameters of service() method.
public abstract class HttpServlet extends GenericServlet implements java.io.Serializable
Being subclass of GenericServlet, the HttpServlet inherits all the properties (methods) of GenericServlet. So, if you extend HttpServlet, you can get the functionality of both.
Let us tabulate the differences for easy understanding and remembering.
|Can be used with any protocol (means, can handle any protocol). Protocol independent.||Should be used with HTTP protocol only (can handle HTTP specific protocols) . Protocol dependent.|
|All methods are concrete except service() method. service() method is abstract method.||All methods are concrete (non-abstract). service() is non-abstract method.|
|service() should be overridden being abstract in super interface.||service() method need not be overridden.|
|It is a must to use service() method as it is a callback method.||Being service() is non-abstract, it can be replaced by doGet() or doPost() methods.|
|Extends Object and implements interfaces Servlet, ServletConfig and Serializable.||Extends GenericServlet and implements interface Serializable|
|Direct subclass of Servet interface.||Direct subclass of GenericServlet.|
|Defined javax.servlet package.||Defined javax.servlet.http package.|
|All the classes and interfaces belonging to javax.servlet package are protocol independent.||All the classes and interfaces present in javax.servlet.http package are protocol dependent (specific to HTTP).|
|Not used now-a-days.||Used always.|
1. One common feature is both the classes are abstract classes.
2. Used with Servlets only.