I was searching for exact difference between javax.servlet.http.HttpServlet
, javax.servlet.GenericServlet
and javax.Servlet
unable to
-> 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).
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.
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.
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.
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
javax.servlet.http.HttpServlet
Signature:
public abstract class HttpServlet extends GenericServlet implements java.io.Serializable
Servlet:-
GenericServlet:-
HttpServlet:-