• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

service method not overridden

 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So do you recommend to put the logic in get/post or service ...?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No this is with respect to only post and get....
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic