• Post Reply Bookmark Topic Watch Topic
  • New Topic

About servlet's service() method  RSS feed

 
Raj chiru
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q: why we should not override servlet's Service() method?what is reason?
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

A simple answer would be otherwise you got to write the implementation to call doXXX()( for HTTP requests) depending on the request recieved which is generally not advisable.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better question: why would you?
 
ryan sukale
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Raj
The servlet's service method performs the task of determining the method that has been called ie- get/post/trace/head/options/put/delete. These are the 'big seven' methods since they are the most commonly used ones.
After determining the method that has been called, it then delegates the task to the corresponding method, ie. doget/doPost and so on.

Actually, even you can do this all by yourself by using the getMethod() function. But since it has already been done for you, you dont have to reinvent the wheel. That is what object oriented programming is all about.

Moreover, your request is most likely to contain only one of the above mentioned http method types. So, it is meaningful to call the appropriate function for the appropriate http method.

You can override the service method in only one scenario. And thats when you have operations that need to be performed irrespective of the type of request that has arrived, ie, for any request that arrives of the 'big seven'. Even then, try to delegate the task to the appropriate functions to the other methods since its good programming practice.

I hope the above explanation helps.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!