File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes doPost or doGet or service() which one should i use Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "doPost or doGet or service() which one should i use" Watch "doPost or doGet or service() which one should i use" New topic
Author

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

Subbu Aswathanarayan
Ranch Hand

Joined: Jun 22, 2001
Posts: 73
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


Subbu
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4458
    
    6

override doPost(). The Servlet API documentation specifically states that there is no need to override the service() method.


Junilu - [How to Ask Questions] [How to Answer Questions]
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
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.
sumit vashishta
Greenhorn

Joined: Jun 14, 2001
Posts: 15
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
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
Subbu Aswathanarayan
Ranch Hand

Joined: Jun 22, 2001
Posts: 73
Thanks all of you for your replies.
Subbu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doPost or doGet or service() which one should i use