File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes service method not overridden Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "service method not overridden" Watch "service method not overridden" New topic
Author

service method not overridden

A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
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
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
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.


Ping & DNS - my free Android networking tools app
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
So do you recommend to put the logic in get/post or service ...?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
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
Marshal

Joined: Mar 22, 2005
Posts: 41816
    
  62
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

Joined: Jul 04, 2004
Posts: 979
No this is with respect to only post and get....
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Oct 30, 2001
Posts: 1970
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...?


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Jan 10, 2002
Posts: 61198
    
  66

Also consider that servlet filters might be a better and cleaner means to add such functionality rather than overriding service().


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: service method not overridden