Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using service() vs. doPost() or doGet()

 
Judy Liddle
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am modifying code which uses service() in its servlets.
In the past I have used doPost or doGet.
Could someone please explain the pros and cons of using service() instead of one of the other methods?
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In his book CORE SERVLETS AND JAVA SERVER PAGES ( http://www.amazon.com/exec/obidos/ASIN/0130893404/electricporkchop ) Marty Hall says:
Don't override service()!
If you need to execute the same logic when either POST or GET methods are invoked, just make one method call the other one where all the logic is. I left the book at the office so I can't quote all the reasons why (plenty), but I just remember that he convinced me when I read it months ago
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Judy,
The main reason not to override doPost method is 'we can't add support for other services latter'. Actually doPost is like a 'router'. Depending upon the type of HTTP request (HEAD/GET/PUT/POST/TRACE/OPTIONS), it calls the corresponding do*** methods. (doGet/doPut/doPost...etc). So when we override this method we basically loose this feature.
There are some cases which may convince this concept.
1. When a HTTP request asks for a page with some condition in its header, like 'give me this page if it is modified since ---DATE--'. So what the first invoked service method does is , it automatically calls the 'getLastModified' method of this servlet , which returns the time in millisecs, and compares the HTTP request's 'If-modified-since' header's value and act accordingly.
2. When a Http request of HEAD comes. which means the servlet need not send the content but only the headers of the page. This happens when link validators check a page if it is working like that. They just ask for HEAD and if they get any response, that means the link is ok. Just to check the link we needn't download the WHOLE page isn't? So for this type of situations, the doPost does the exact work of calling the 'doGet' method but extracts the Headers alone and send to the request.
So basically we want to keep the above said 'special features' of service method and make use of them. THis is the reason behind it.
regds
maha anna
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last response stated "The main reason not to override doPost method..." but I think the writer meant to say "The main reason not to overriide the "service" method..."
It's the service method that calls the other doXxx methods.
 
Jay Faulk
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seems to me that you can call super from your inherited service method.
while there is no reason to override service, if a request has been performed, it may be necessary to do so if the first page itself is a servlet which is in charge of redirecting info to different jsp pages.
just read all this stuff - i am new in this area.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic