wood burning stoves 2.0*
The moose likes Servlets and the fly likes using service() vs. doPost() or doGet() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "using service() vs. doPost() or doGet()" Watch "using service() vs. doPost() or doGet()" New topic
Author

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

Judy Liddle
Greenhorn

Joined: Sep 28, 2000
Posts: 8
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

Joined: Jan 30, 2000
Posts: 3222
    
    5
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

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Oct 18, 2000
Posts: 2
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using service() vs. doPost() or doGet()
 
Similar Threads
service() in HttpServlet
difference between Generic servlet & Httpservlet
Why Not Overload Service Method.
Differences Between doGet(),doPost(),service()
Servlets 2 - reverse servlet