• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

service method not overridden

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Why is that we dont override service method...

but ...have the methods doPost or doGet...defined in a servlet class...

If the logic is in these methods...then service method has to resolve which of these methods has to be invoked..

but if the logic was in the service...method...then it doesnt have to have the above overhead..

Regards
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you do need to have the logic somewhere, because although GET and POST are often used interchangeably, that's not always the case. I wouldn't think that the overhead is big enough to worry about.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So do you recommend to put the logic in get/post or service ...?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't recommend overriding the service method.

The doPost and doGet methods handle the two most common request methods.
When a browser submits a form the POST method is generally used.
Most other types of hits use the GET method (hyperlinks, image tags, etc..).

A common way to support both request types from a single servlet method without overriding your service method is just to have one method call the other.


[ June 30, 2006: Message edited by: Ben Souther ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My friend had the question in an interview...

Cant we override service method itself without using doget or doPost

so that we can reduce the overhead involved in resolving which method of

these two to invoke..

Regards
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if it's a TRACE, HEAD, GET, DELETE or OPTIONS request? Those need to be treated rather differently than GET and POST.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No this is with respect to only post and get....
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so that we can reduce the overhead involved in resolving which method of
these two to invoke..

Don't be silly, that just involves ONE CALL to request.getMethod() which you would have to do anyway if you override service.
[rant]
DON'T DON'T DON'T get distracted with these bizarre optimization schemes that circulate as rumours. Stick to the standard servlet API and concentrate on making your code clear.
Please realize that by the time your servlet code gets a request, a HUGE amount of work has already been done by the underlying socket code and servlet container to set up the request and response objects. Any CPU cycles saved by "clever" tricks will be invisible.
[/rant]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by A Kumar:
No this is with respect to only post and get....


How will you know if the request is using the POST or GET method if you override the mechanism that is reponsible for determining this?

This is precicely why we leave the service method alone and work in the respective doGet, doPost, doWhatever.... methods.

Also: Everything William just said..
[ June 30, 2006: Message edited by: Ben Souther ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sure that all is true that the various experts have said about it not being worth overriding service() to seek some tiny performance gain.

However, I have found one time when overriding service() does seem worthwhile to me. I override it to add some custom debug trace. The overriding service() method does some trace, then super.service(), then some more trace. It's a lot easier to add that trace in this one place, than in all those doXXX() methods.

Hopefully, those experts would consider this OK...?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you know what you're doing, there's nothing wrong with overriding service.
That's why it's there.
In your case, you've extended it to add some functionality common to all the request methods and then called super() to insure that all the standard functionality is called.
Just using it, instead of doGet or doPost, in hopes of getting a faster app is what the folks here were advising against.
[ June 30, 2006: Message edited by: Ben Souther ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65217
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also consider that servlet filters might be a better and cleaner means to add such functionality rather than overriding service().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic