Prevent form resubmit after pressing back button

Viewed 14274

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..

5 Answers

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() }
});

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 #
Related