How to avoid POSTDATA resend warning

POSTDATA resend warning is a common problem when developing web applications. Before discussing about the solution, let's know what is the problem actually and when it arises.

About the problem :

We see this warning when we try to revisit a page that has accepted POSTDATA using browser's history mechanism. Usually we do it by browser's BACK button or refreshing a page. When a page in browser history requested with POST method, the browser thinks this POSTDATA is important to process this page. So, it asks the user if he wants to send the POSTDATA again or not. Different browsers show this warning differently. For example, the images below shows how Firefox and Internet Explorer show this warning.

POSTDATA resend warning in Mozilla Firefox
POSTDATA resend warning in Internet Explorer
Now, If the user accepts the confirmation, the page is reloaded with previously sent data using POST method; otherwise some browsers don't take any action and some shows a "Webpage has expired" message. In some situations, this re-submitting is useful, but most of the times it's not expected. It's more harmful when user accepts it without understanding and causes multiple entries of previously entered data in database.

The easy solution :
The easy solution is simply redirecting. When we redirect a page, the target page is loaded without any POST data that comes with request.
Let's solve this problem by hand. Say we have 2 pages - A and B. A has a feedback form which submits data by POST request method to B. B inserts the information into database and shows a success message.
Now, if the user try to reload the page by refresh, back button or any other way, browser will show the confirmation message. If user accepts this, the message will inserted again. But after inserting to database, If we redirect to another page C and C shows the success message, the problem will not occur. Because, in the final output to the browser will come from C which didn't use any POST request data. The code should be something like this:

Extending the solution :
The solution I explained above can show just an static message like "Thanks for your feedback" or "Registration completed successfully" etc. But usually we like to include some information about sender or submitted information in the success of failure message. For example:
Hi Andrew, your registration is successfully completed. An email is sent to you at andy001@yahoo.com with more information.In the above message, the purple colored information has to be taken from POST data. But the POST data will be lost when redirecting. So, it's not possible by the above solution.
What we can do to solve this is that we can use SESSION to store those data which we want to display with message in redirected page. Just like this:
In the target page, when showing the success message, we can get the information from SESSION easily.
Hope this will solve your problem. Please tell me if any confusion or question with this article.
See you again.

Comments

Popular posts from this blog

Dynamic Image in Jasper Report

Simple Invoice Creation With Jasper Report

Auto Increment Oracle Table Id Mapping With JPA Entity