sheidaei
sheidaei

Reputation: 10342

When to use GenericServlet over HttpServlet?

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.

Upvotes: 7

Views: 6041

Answers (2)

Rajat Verma
Rajat Verma

Reputation: 2590

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.

Upvotes: 1

Vidya
Vidya

Reputation: 30320

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.

Upvotes: 10

Related Questions