aspose file tools*
The moose likes Servlets and the fly likes service() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "service() method" Watch "service() method" New topic
Author

service() method

Marilyn Monickam
Ranch Hand

Joined: Jun 05, 2001
Posts: 66
Hi
Why is it not a good practice to override the service() method?
Marilyn
Preethi Suryam
Ranch Hand

Joined: Nov 17, 2000
Posts: 92
Hi!
Basically the default service() method in an HTTP servlet routes the request to another method based on the HTTP transfer method (POST, GET, etc.) For example, HTTP POST requests are routed to the doPost() method, HTTP GET requests are routed to the doGet() method, and so on. This enables the Servlet to perform different processing on the request data depending on the transfer method. Since the routing takes place in service(), you generally do not override service() in an HTTP Servlet. Instead, override doGet() and/or doPost(), etc., depending on the type of request you expect.
Hope its clear.
Regards!
Preethi
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Sometimes it is useful though to override service(). For example, if there's some preprocessing code that is common to both doGet() and doPost(), you might want to move that code to service() and follow it with super.service().
------------------
Miftah Khan
- Sun Certified Programmer for the Java 2 Platform
- Sun Certified Web Component Developer for the J2EE Platform
Marilyn Monickam
Ranch Hand

Joined: Jun 05, 2001
Posts: 66
Hi Preethi,
Yeah I know that they are generally routed from the
service() method.Are there any performance overheads ?.You say they are generally not overridden but why?
Thanks,
Marilyn
Preethi Suryam
Ranch Hand

Joined: Nov 17, 2000
Posts: 92
Hi Miftah you are right, i agree with you.It depends on what kind of routing we are implementing.Hope this answers your question Marylin.definetly there will be some performance overheads.
Preethi.
Marilyn Monickam
Ranch Hand

Joined: Jun 05, 2001
Posts: 66
Preethi,
Though I understand what you say,I am not still convinced about why the method should not be overridden.Can you furnish some more details? This is a very common question in the interview.
Marilyn
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Reasons for avoiding service overriding:
By overriding service(), without calling super.service(), you're giving up too much free functionality, such as method routing, and OPTIONS, TRACE and HEAD functionality.
Also, GET and POST calls would essentially be rendered the same.
==> Suppose there's a POST request on your servlet, and you do a request.getQueryString(). This wouldn't be meaningful, whereas it would be for a GET request.
If GET and POST are indeed supposed to do the same thing, then just have doPost() call doGet(). This would be far more maintainable, since you can always change doPost() in the future if requirements change.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: service() method
 
Similar Threads
servlets
Did implicit objects are instance variables or local variables in _jspService
Web service method call behaviour?
Post to Servlet from Applet doesn't work
protected Method service???