doPost or doGet or service() which one should i use

Hi all, in one of my servlets i am overriding both the doPost() and doGet() methods.if my servlet gets called due to a GET request i just need to give an error message.but if it gets loaded due to a POST request i need to do a lot of processing.should i do this processing in the doPost() method itself or should i do it in service() method.which is advisable and why? Thanks Subbu

Th preferred method is to provide a doXXX() method, such as doGet() or doPost(), and not to override the service() method. You would probably only have to override the service() method if one of the doXXX methods didn't suit your purpose for some reason.

hi Subbu I think that use service() method which is multi-threaded so you do not need to take care about multi-threading. if you want to use the features of doGet() or doPost() just write in your html code <METHOD=POST....> - html code for the same code in servlet will be public void service(req,res) it is same as public void doPost(req,res) same is true for doGet()

Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450

posted Jun 28, 2001 06:42:00

0

Originally posted by sumit vashishta:hi Subbu I think that use service() method which is multi-threaded so you do not need to take care about multi-threading. if you want to use the features of doGet() or doPost() just write in your html code <METHOD=POST....> - html code for the same code in servlet will be public void service(req,res) it is same as public void doPost(req,res) same is true for doGet()

I'm not sure what you are trying to say here, since a new thread is spawned every time the doXXX methods (doGet, doPost, etc..) are called. Subbu, if you have to process something when the servlet receives a post, put that in the doPost() method. That is the preferred way. But don't take my word for it, check out pp36-37 of Marty Hall's Core Servlets and JavaServer Pages if you can for his reasoning on not overriding the service() method. Jason