Prevent form resubmit after pressing back button

12,563

Solution 1

You can use a javascript function with the help of a hidden attribute to reload the web page. When the user press the back button,based on the value of the hidden attribute, page will be reloaded without loading the cached page.

Your approach of clearing cache is correct. Coupled with that, you can use this approach.

<input type="hidden" id="refreshed" value="no">
    <script type="text/javascript">

           onload=function(){
               var e=document.getElementById("refreshed");
               if(e.value=="no")e.value="yes";
               else{e.value="no";location.reload();}
           }

    </script>

One drawback of this approach is if your clients' browsers have disabled JS, this will not work.Otherwise it should work.

Solution 2

When the user press the browser's back button, user can come to the previous page where he submitted the details. And when the user submit this, data is saved once more to the database.

According to how you described it, that is based on a doGet request. Which means every time you visit that URL, it will send the request with whatever parameters were added.

As someone already mentioned, if you switch the form to a post method and switch the Servlet to a doPost, you won't have this issue anymore.

Alternatively you can circumvent this with a javascript solution. Here are some options:

Solution 3

You can use this code also

$(document).ready(function() {
    function disableBack() { window.history.forward() }

    window.onload = disableBack();
    window.onpageshow = function(evt) { if (evt.persisted) disableBack() }
});

Solution 4

You must use a Post-Redirect-Get pattern: https://en.m.wikipedia.org/wiki/Post/Redirect/Get.

Actually, every use of standard HTML forms with method="post" should be implemented with that pattern. It doesn't have any use for AJAX-posted forms, which actually could be another solution but will require more work and probably some architectural changes.

Solution 5

I had this same problem while building a django web app, and my solution was to not allow caching of the html that contains the form. In your request handler, do not allow the browser to cache the page. This will force the browser to get the page fresh from the document.

Which, in this case, you can just verify in your request handler if the requested form has already been submitted.

My code for reference:

from django.views.decorators.cache import never_cache
    @never_cache
      def GetForm(request, pk):
          # Logic #
          if (IsFormCompleted(pk)):
             # Handle request #
Share:
12,563
Lucy Ferrier
Author by

Lucy Ferrier

~~And in the daylight we can hitchhike to Maine I hope that someday I’ll see without these frames~~

Updated on June 15, 2022

Comments

  • Lucy Ferrier
    Lucy Ferrier almost 2 years

    I am in bit of a delicate situation here. In my organization we design stock management systems and it is a web application based on JSP pages and servlets which handles them.

    I have been asked to fix a specific problem. We have a JSP page with an HTML form table where there are stock details. When user enters the details manually and submit the form, stock details updated in the database and it works fine.

    Problem is this : When the user press the browser's back button, user can come to the previous page where he submitted the details. And when the user submit this, data is saved once more to the database.I need to prevent this behaviour.(Something likeclear and reload the page.)

    Things I did so far : clear the browser cache.Code works fine but not the expected result.

    Unfortunately I cannot share the code due to company regulations. What I need is a help to prevent this behaviour or a workaround.

    Thanks in advance..