Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Doubts in service()/doPost()/doGet()

 
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt, What is the situation in which we need to override the service()? In lots of example I have seen, instead of overriding the doPost/doGet, they override the service()? Is there any necessity to override the service() than overriding doPost()/doGet()?

And also, I have seen some examples that both doGet and doPost are overridden in the same servlet? Why? and What for? They have used like below:


if the above servlet is called directly wihout from any form, the doGet will first execute right?
[ December 31, 2006: Message edited by: Micheal John ]
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well sometimes people call the methods in that way so that both doPost and doGet functionalities are supported. Very rarely would you have to override the service method to achieve an end result.
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the things the service method does is to distinguish between the various HTTP methods. The available HTTP methods are GET, POST, HEAD, TRACE, PUT, DELETE and OPTIONS. If service is overridden, then the dispatching to the proper doXYZ methods has to be performed by the servlet code itself. Note that the javadocs state "There's no need to override this method."

You're right that doGet would be called if a URL is entered into a browser. Sometimes it can be useful to have GET and POST do different things in a servlet, but I'd consider that not such a good design. At other times you might want to specifically disallow either GET and POST, in which case it's handy to be able to differentiate between the two.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic