防止在按下返回按钮后重新提交表单

问题描述:

我在这里有些微妙的情况.在我的组织中,我们设计库存管理系统,它是一个基于JSP页面和servlet进行处理的Web应用程序.

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.

已要求我解决特定问题.我们有一个带有HTML表单表的JSP页面,其中有库存详细信息.当用户手动输入详细信息并提交表单时,库存详细信息将在数据库中更新,并且可以正常工作.

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.

先谢谢了.

您可以在隐藏属性的帮助下使用javascript函数来重新加载网页.当用户按下后退按钮时,基于隐藏属性的值,页面将被重新加载而不加载缓存的页面.

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>

此方法的一个缺点是,如果您的客户的浏览器已禁用JS,则此方法将无效.否则,它将有效.

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