Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

GenericServlet vs HttpServlet  RSS feed

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

What are the differences between GenericServlet and HttpServlet?

Why we can't use session and cookies while GenericServlet is protocol independent (so it can handle any type of protocol)?

What GenericServlet can do and HttpServlet can't?

Thank you.



 
Bartender
Posts: 9555
189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpServlet IS A GenericServlet. GenericServlet doesn't expose cookies because not all protocols use cookies.
 
Saloon Keeper
Posts: 2260
289
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you have mentioned, GenericServlet is for protocol-independent servlets, and is a base class for protocol-specific servlet classes.

For servlets which deal with HTTP protocol, HttpServlet extends GenericServlet with HTTP-specific functionality.
Similarly for SIP protocol, SipServlet extends GenericServlet with SIP-specific functionality.
 
Marshal
Posts: 67192
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Julio leopard banderas wrote:GenericServlet is protocol independent (so it can handle any type of protocol)


It's quite the opposite: GenericServlet handles no type of protocol. That is for the extending classes to provide.
 
Bartender
Posts: 20107
101
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J2EE doesn't actually assume that the only way to do servlets is via an HTTP server. The GenericServlet is the base class for any sort of server that works in a request/response paradigm. For example, long before Java was invented, IBM had a lightweight interactive application system called CICS. Actually, still, has, but back before PCs, TCP/IP, HTTP and the entire Internet, you needed something like that or be stuck juggling tapes and punched cards.

So GenericServlet could be used as a base class for a CICS application. And for all I know, it has been, although I got out of the mainframe-only world before Java came along, so I'm no longer in touch with IBM-specific products.

For webapps, you'l almost always want to use HttpServlet, since it affords the extra amenities that HTTP has to offer.

Note that this isn't the only case where Sun has distanced itself from specific frameworks. JavaServer Faces was also designed with the idea that HTML would not be the only possible form framework. And in fact, back then, WAP was used on many phones. Only when smartphones became the norm did WAP get pushed aside by HTML/CSS/JavaScript.
 
Saloon Keeper
Posts: 5127
135
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stephan van Hulst
Bartender
Posts: 9555
189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrong. As Bear has said, GenericServlet handles NO protocol.
 
Tim Moores
Saloon Keeper
Posts: 5127
135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that's a question of semantics: GenericServlet handles no protocol specifically, but it handles all protocols because all protocol-dependent classes that extend it inherit its (admittedly rudimentary) code - which is thus used to handle all those protocols.

So I would say GenericServlet implements no protocol, but it handles all of them.
 
Tim Holloway
Bartender
Posts: 20107
101
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to split the difference. GenericServlet handles no protocols. That's done elsewhere, out of sight. It could be said to support all request/response protocols, and that includes those that are entirely internal to the application, with no networking whatsoever.

That is, if I wanted to build an application framework where the applications were processing modules that were fed input from a single source and produced output to a single response point, I could make those applications "pluggable" by basing them on GenericServlet.
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!