Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between GenericServlet, HttpServlet and a Servlet?

Tags:

java

servlets

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

  1. Usage
  2. Reason behind javax.servlet.GenericServlet existence
like image 778
Rony Avatar asked Jul 17 '12 20:07

Rony


People also ask

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.

What is the difference between GenericServlet and HttpServlet explain with code snippet?

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.

What is servlet hierarchy GenericServlet and HttpServlet?

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.

What is GenericServlet?

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.


5 Answers

"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.

like image 139
Dave Newton Avatar answered Oct 30 '22 01:10

Dave Newton


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
  1. GenericServlet defines a generic, protocol-independent servlet.
  2. GenericServlet gives a blueprint and makes writing servlet easier.
  3. GenericServlet provides simple versions of the life-cycle methods init and destroy and of the methods in the ServletConfig interface.
  4. GenericServlet implements the log method, declared in the ServletContext interface.
  5. To write a generic servlet, it is sufficient to override the abstract service() method.

javax.servlet.http.HttpServlet

Signature:

public abstract class HttpServlet extends GenericServlet implements java.io.Serializable
  1. HttpServlet defines a HTTP protocol specific servlet.
  2. HttpServlet gives a blueprint for Http servlet and makes writing them easier.
  3. HttpServlet extends the GenericServlet and hence inherits the properties GenericServlet.
like image 38
K_Anas Avatar answered Oct 30 '22 01:10

K_Anas


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.

like image 21
Mikko Maunu Avatar answered Oct 30 '22 02:10

Mikko Maunu


-> 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).

like image 24
Jaydip Baldha Avatar answered Oct 30 '22 03:10

Jaydip Baldha


Servlet:-

  1. The Servlets runs as a thread in a web-container instead of in a seperate OS process.
  2. Only one object is created first time when first request comes, other request share the same object.
  3. Servlet is platform independent.
  4. Servlet is fast.

GenericServlet:-

  1. General for all protocol.
  2. Implements Servlet Interface.
  3. Use Service method.

HttpServlet:-

  1. Only for HTTP Protocol.
  2. Inherit GenericServlet class.
  3. Use doPost, doGet method instead of service method.
like image 41
Mohit Avatar answered Oct 30 '22 03:10

Mohit