A Simple question..
2 posts in topic
Flat View  Flat View

Posted By:   Madhan_Prabahar
Posted On:   Monday, September 15, 2003 03:59 AM

I'm having a simple (may be silly) question...

In a servlet(or jsp) how to avoid the request being posted again when F5 is pressed(refreshed)...



Re: A Simple question..

Posted By:   Lunkwill_Vroomfondel  
Posted On:   Wednesday, October 1, 2003 06:54 AM

consider this:

you post your data to a servlet X.

this servlet doesnt do the output, but "redirects" to another page (could also be a servlet) Y.

now pressing f5 would reload the page Y and not "repost" your data.


Re: A Simple question..

Posted By:   Christopher_Koenigsberg  
Posted On:   Monday, September 15, 2003 07:31 AM

Actually it is neither simple, nor silly!

The way http works, you cannot prevent the client from re-submitting. So you have to guard against it on the server side. The Struts API provides a 'token' for this; otherwise I think you have to code it yourself (unless there's some taglib somewhere?).

I think basically you do a cycle, of set, check, and clear, on a semi-random 'token' of some sort, in the session and in a hidden field on the jsp response, to track whether this is the first time this form has been submitted or not.

E.g. you get a request for a page which is a jsp form. You generate a new 'token', set it in session scope (I guess this has to be synchronized), and render it in a hidden field on the jsp. Then when the user submits the form, you get the hidden field parameter and compare it against the synchronize token. If it matches, you have a good, first-time request from that form, so you immediately clear the token in the session (maybe you want to do both test + clear "atomically", in the same synchronized method). Then if a new request comes in afterwards, with the same token e.g. a re-submit of the same page, it will no longer match the token in the session (that you just cleared), so you know to disregard it.

About | Sitemap | Contact