Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Micheal John
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 ]
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic