This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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 ]
SCJP 1.4 (86%), SCWCD 1.4 (86%), SCBCD 1.3 (85%), SCDJWS (Just Started...) - Satisfaction Lies in Our EFFORT, Not in the ATTAINMENT
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.