• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Subbu Aswathanarayan
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Junilu Lacar
Bartender
Pie
Posts: 6573
24
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
override doPost(). The Servlet API documentation specifically states that there is no need to override the service() method.
 
Jason Menard
Sheriff
Posts: 6450
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6450
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all of you for your replies.
Subbu
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic