Rony
Rony

Reputation: 473

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

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

Upvotes: 24

Views: 82285

Answers (11)

anil
anil

Reputation: 1

  1. http servlet is an class which is specific and allows only http protocol not allowed to other protocol like ftp smtp
  2. generic servlet are allowed all types of protocol only one abstract method that is service method and other four method are concrete method
  3. generic servlet is the superclass of http servlet

Upvotes: 0

Varshil Patel
Varshil Patel

Reputation: 161

  1. Servlet interface
    Its the super interface for GenericServlet and HttpServlet. It contains 5 abstract methods and all inherited by GenericServlet and HttpServlet.
    • Problem
      If you want to create servlet by implementing Servlet interface all the 5 abstract methods of the interface Servlet should be overridden eventhough Programmer is not interested
    • Usage
      Only implement Servlet interface if you like to develop your own container.
  2. GenericServlet
    Its the immediate subclass of Servlet interface and super class of HttpServlet. In this class, one abstract method exist: service(). Other 4 abstract methods are implemented in this class.
    • Problem
      All methods are concrete except service() method, So you have to implement it as a callback method.
    • Usage
      It is protocol independent so Can be used with any protocol like FTP, SMTP, HTTP etc.
  3. HttpServlet
    Its subclass of both GenericServlet and Servlet interface. Immediate super class of HttpServlet is GenericServlet. HttpServlet does not contain any abstract method. Eventhough the HttpServlet does not contain any abstract methods, it is declared as abstract class by the Designers to not to allow the anyone to create an object directly because a Servlet object is created by the Servlet Container.
    • Problem
      HttpServlet is protocol dependent and used specific to HTTP protocol only.
    • Usage
      service() method need not be overridden. It can be replaced by doGet() or doPost() methods with the same parameters. Its most used method to create servlet.


      hierarchy
      Reference: way2java.com

Upvotes: 2

Dnyaneshwar Tambe
Dnyaneshwar Tambe

Reputation: 41

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.

Upvotes: 2

Dave Newton
Dave Newton

Reputation: 160261

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

Upvotes: 28

rajasekhar
rajasekhar

Reputation: 1

All classes, interfaces, and methods present in the javax.servlet package are protocol independent (generic to all protocols).

In contrast, all classes, interfaces, and methods present in the javax.servlet.http package are protocol dependent (specific to the HTTP protocol)

Upvotes: 0

Jaydip Baldha
Jaydip Baldha

Reputation: 106

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

Upvotes: 8

Mohit
Mohit

Reputation: 41

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.

Upvotes: 4

rajkannoju
rajkannoju

Reputation: 11

-->GenericServlet can process multiple clients request from a single form. Whereas, HttpServlet can process multiple clients requesting from multiple HTML forms.

--> GenericServlet is Stateless and HttpServlet is Stateful.

Upvotes: 1

K_Anas
K_Anas

Reputation: 31466

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.

Upvotes: 14

pb2q
pb2q

Reputation: 59627

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.

Upvotes: 2

Mikko Maunu
Mikko Maunu

Reputation: 42114

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.

Upvotes: 12

Related Questions