• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of HttpServlet over GenericServlet

 
Jigar Naik
Ranch Hand
Posts: 762
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the need for having HTTPServlet class when we already have GenericServlet class which can handle all type of requests?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpServlet adds capabilities to GenericServlet (which knows nothing about HTTP). For example, it can discriminate between the various HTTP methods. If you were to use GenericServlet then your code would have to do that instead.
 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some of uses of HttpServlet over GenericServlet:

GenericServlet is for servlets that might not use HTTP, like for instance FTP servlets. Of course, it turns out that there's no such thing as FTP servlets, but they were trying to plan for future growth when they designed the spec.

In short GenericServlet is protocol independent, whereas HttpServlet is protocol dependent

In GenericServlets you cannot use Cookies or HttpSession.
Session tracking is not possible, redirection is not possible.

Http servlets overrides doGet and doPost methods. Generic servlet overides service method.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Short version: use HttpServlet.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Further you can have a look at the HTTP specific methods to understand the difference. Cookies for example, make sense with Http only and cannot be included into the generic servlet
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic