• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

GenericServlet vs HttpServlet

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HttpServlet IS A GenericServlet. GenericServlet doesn't expose cookies because not all protocols use cookies.
 
Marshal
Posts: 4491
572
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also see https://coderanch.com/wiki/659865/Servlets-Faq#otherProtocols
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15485
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wrong. As Bear has said, GenericServlet handles NO protocol.
 
Tim Moores
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic