Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Not Overload Service Method.

 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not able to understand why should not we overload service method directly.Why we overload doGet or doPost methods.
I know we should implement doGet or doPost methods because service method call doGet or doPost.
I want to know what is "advantage of implement doGet or doPost methods then overloading service method."
If it is recommanded that we should always implement doGet or doPost then "why service method is implemented in HttpServlet class."

Thanks
 
Senthil Kumar
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this has been answered already.There are chances that you want to handle/service the requests based on the request method.
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am not able to understand can you please explain.
 
Vikrant Pandit
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm ... Http defines many methods ...like GET , POST , PUT ,TRACE ,HEAD etc.....

service method in HttpServlet forwards the request to one these methods based on the method specified in incoming request.

So in case if you want to your servlet to support get and post but not other methods , then you can only implement doGet and doPost and leave out the other methods . A client trying to do a put request on your servlet gets a status code of 400 meaning operation not supported ...

HTH
 
Senthil Kumar
Ranch Hand
Posts: 264
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There might be scenarios like you want to respond the GET request in a specific way and the POST request in a way.GET requests are generally meant for just fetching the data and giving it back.And POST request is for updating some thing in the backend.That's why it is called idempotenet.
But if you had overriden the serice method instead,you will have only one way of responding.

Hope this helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic