Prevent form resubmit after pressing back button

Lucy Ferrier :

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

Supun Amarasinghe :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=470743&siteId=1