I was searching for exact difference between javax.servlet.http.HttpServlet
, javax.servlet.GenericServlet
and javax.Servlet
unable to find it out.
"Exact Difference" means
javax.servlet.GenericServlet
existenceThe 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.
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.
A servlet is a java class that implements the interface javax. servlet. Servlet. The abstract class GenericServlet contains protocol independent implementation of some maintenance code useful for all servlets. The abstract class HttpServlet contains HTTP specific implementation and all HTTP-based servlets extend this.
GenericServlet makes writing servlets easier. It provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface. GenericServlet also implements the log method, declared in the ServletContext interface.
"Exact difference" meaning what? The API lists the exact differences.
Servlet
is an interface defining what a servlet must implement.
GenericServlet
is just that, a generic, protocol-independent servlet.
HttpServlet
is a servlet tied specifically to the HTTP protocol.
Are you asking when you'd use any of those?
In general, you'd extend HttpServlet
to implement an application's web layer.
You might implement Servlet
if you're writing your own container or handling everything yourself. You might extend GenericServlet
to handle a different protocol, but you might not.
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
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.
-> 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).
Servlet:-
GenericServlet:-
HttpServlet:-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With