Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use GenericServlet over HttpServlet?

Tags:

java

servlets

I understand that the GenericServlet is protocol independent and an abstract class. So my questions is when someone would chose to use GenericServlet? Do you know of any specific example in mind that uses GenericServlet?

I have always correlated servlets with HTTP protocol and its responses and requests. I can not think of how a servlet can be useful in a protocol other than HTTP. It doesn't mean that it can not be. The option of being used in other protocol might have not been utilized in the past and there is no need for it anymore. Or it might be me that I don't have enough knowledge of it.

like image 561
sheidaei Avatar asked Oct 08 '13 17:10

sheidaei


2 Answers

The short answer to your question is "Never. Practically speaking."

The long answer is that GenericServlet is an abstract (can't be instantiated) protocol-independent servlet, which is an endpoint accessible over a network like the web. GenericServlet provides some basic lifecycle management, but you have to extend it to make anything useful.

HttpServlet is an implementation of GenericServlet that handles many of the low-level details of the HTTP protocol like headers, chunking, cookies, and so on. As a practical matter, this is the only implementation anyone cares about.

I suppose it is theoretically possible one could extend GenericServlet to implement a different protocol like FTP or SMTP, but why bother?

Hope that helps.

like image 131
Vidya Avatar answered Sep 28 '22 19:09

Vidya


GenericServlet doesn't implements all 10 HTTP protocol request (get post put delete head trace connect options etc.). so If you extend GenericServlet then all the request gonna invoke service() method. as solution to this HttpServlet implements them so you can perform difference action according to the Request Received.

like image 43
Rajat Avatar answered Sep 28 '22 18:09

Rajat