aspose file tools*
The moose likes Servlets and the fly likes Idempotent Request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Idempotent Request" Watch "Idempotent Request" New topic
Author

Idempotent Request

mridul das
Greenhorn

Joined: Jul 15, 2005
Posts: 19
I am calling a helper class from the servlet which does the database operations. What happens is there is one page which does and insertion in the database. When I refresh that page it causes to insert the values once againg in the database.
A new request is generated and causes DB operations. I want to stop it from hitting the DB but since a new request is generated I cant identify it as the request is becoz the user has refreshed the page or he's clicked the button which causes it to insert the values.
I am using the get method in the form which is supposed to be idempotent
Please help.
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135
Generally you need some way to identify duplicated requests from the same client.

Heuristically, you can put a token along with the form submission or hyperlink and send the token along with the request. The servlet can check the session (or other places where you can keep the token) if there already a token in process. If another token already in place, ignore the request (or send back some message). Once the process done, remove the token and the servlet is ready to process another submission.


SCJP, SCWCD, SCJWS, IBM 700,IBM 701, IBM 704, IBM 705, CA Clarity Technical<br /> <br /><a href="http://eddyleesinti.blogspot.com" target="_blank" rel="nofollow">http://eddyleesinti.blogspot.com</a>
mridul das
Greenhorn

Joined: Jul 15, 2005
Posts: 19
Where do I remove the token. The process completes when it comes back to the JSP. But if I remove the token in the JSP and then I refresh the page, it generates another request. The token is not found and the DB is hit again.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
I am using the get method in the form which is supposed to be idempotent


That's precisely the problem. GET should be idempotent, but you have implemented something that is not. For a DB insert, you should use POST. Then the browser will warn you -if you hit reload- that the operation is about to be repeated.
[ October 24, 2005: Message edited by: Ulf Dittmer ]
Eddy Lee Sin Ti
Ranch Hand

Joined: Oct 06, 2005
Posts: 135

Where do I remove the token. The process completes when it comes back to the JSP. But if I remove the token in the JSP and then I refresh the page, it generates another request. The token is not found and the DB is hit again


You can remove the token at the completion of the request.

When you said "process completes when it comes back to the JSP", do you mean that the request already successfully completed? If yes, then the next refresh will of course generate a new valid request to be processed. The token approach only will prevent the client from submitting the same form multiple times concurrently. If you need to make sure that the client submit the data in the form only once in the lifetime of the session/application, don't remove the token, persist it somewhere.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There was just another post on the board about POST-redirect-GET. POST an update to a servlet which sends a redirect to the browser, then the browser issues a GET to see the results. The user can bookmark or refresh the GET address to view the results any time they want. Does that work for you?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Ulf Dittmer:


That's precisely the problem. GET should be idempotent, but you have implemented something that is not. For a DB insert, you should use POST. Then the browser will warn you -if you hit reload- that the operation is about to be repeated.

[ October 24, 2005: Message edited by: Ulf Dittmer ]


The pop-up that has two options retry and cancel is because of this reason???

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
Originally posted by rathi ji:


The pop-up that has two options retry and cancel is because of this reason???

Thanks.


Basically, yes.
Alex Sharkoff
Ranch Hand

Joined: Apr 11, 2004
Posts: 209
Hi,

the following article explains very nicely the concept of redirecting after post method: http://www.theserverside.com/articles/article.tss?l=RedirectAfterPost




Alex (SCJP 1.4, SCBCD 1.3, SCWCD 1.4, SCJD 1.4)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Idempotent Request