• 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

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Why is it not a good practice to override the service() method?
Marilyn
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic