java 防止表单重新提交并在页面刷新时插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14189545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent form resubmission and insert into database on page refresh
提问by Kiran Badi
I have a servlet which takes data from the form and inserts it in the database.In servlet I make use of beans and get/set data via beans.Once the insert happens ,user is redirected to the view which shows as how exactly the page looks.This page is JSP. Now when I do this page refresh(jsp),it again does form data insert into the database.Now I need to prevent this from happening.What I was thinking was rather than redirecting it to jsp,let me redirect to another servlet and from that servlet let me do the get request and forward it to jsp for view.With this approach, I have refactor my jsp and I have lot many jsp which will be impacted.
我有一个 servlet,它从表单中获取数据并将其插入到数据库中。在 servlet 中,我使用 bean 并通过 bean 获取/设置数据。一旦插入发生,用户将被重定向到显示页面的视图看起来。这个页面是JSP。现在,当我执行此页面刷新(jsp)时,它再次将表单数据插入到数据库中。现在我需要防止这种情况发生。我的想法是,而不是将其重定向到 jsp,让我重定向到另一个 servlet,然后从该 servlet 让我执行 get 请求并将其转发到 jsp 以供查看。通过这种方法,我重构了我的 jsp,并且我有很多会受到影响的 jsp。
Is their any other way I can prevent insert into the database when page refresh is done ?
当页面刷新完成时,他们还有其他方法可以防止插入数据库吗?
I also checked on database side assuming that I can add unique constraints, I do have some unique columns that gets generated via initial servlet,so on each insert values are different,so I cannot really use the solution on DB side.
我还检查了数据库端,假设我可以添加唯一约束,我确实有一些通过初始 servlet 生成的唯一列,所以每个插入值都不同,所以我不能真正在 DB 端使用解决方案。
Can someone suggest me if there exists work arounds for this ?
有人可以建议我是否存在解决方法吗?
Update:Adding code which does insert, I am refreshing the page manually.Seems like this is bug which I missed out to consider earlier.It does the insert correctly in the database and shows correctly all data in the view jsp.When the view jsp is refreshed, I get double inserts in the database.
更新:添加插入的代码,我正在手动刷新页面。似乎这是我之前错过的错误。它在数据库中正确插入并在视图jsp中正确显示所有数据。当视图jsp刷新了,我在数据库中得到了双重插入。
Update .removed the code.Will post relevant parts of the code rather than whole code.
更新。删除了代码。将发布代码的相关部分而不是整个代码。
Issue is resolved.
问题已解决。
javax.servlet.http.HttpSession session = request.getSession();
if (session.getAttribute("insertflag") == null) {
mybean sf = new mybean();
sf.insert(form);
session.setAttribute("insertflag", insertid);
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/test/showtestform.jsp");
rd.forward(request, response);
} else {
RequestDispatcher rd = request.getRequestDispatcher("/errorpage.jsp");
rd.forward(request, response);
}
In error page jsp I remove the attribute set and then provide the link in that page to come back again to home page.
在错误页面 jsp 中,我删除了属性集,然后在该页面中提供链接以再次返回主页。
回答by Deepak Singhal
A simple workaround can be that you put a flag in Session; whenever insert is done.. And before inserting always check session if this flag is present.
一个简单的解决方法是在 Session 中放置一个标志;每当插入完成..并且在插入之前总是检查会话是否存在此标志。
In the servlet; Before inserting into the database; make following check:
在 servlet 中;在插入数据库之前;进行以下检查:
if (session.getAttribute("recordInsertedSuccessfully") == null )
{
//proceed with insertion
//after inserting into the database we should do :
session.putAttribute("recordInsertedSuccessfully","true");
} else {
//case of form re-submission
}
If there is a possibility of a valid scenario of user inserting two of these records ( might be user browsing and then again inserting record in same table ; which is a valid scenario ) ; then we need to also add following code before the form is shown to user:
如果存在用户插入其中两条记录的有效场景的可能性(可能是用户浏览然后再次在同一个表中插入记录;这是一个有效场景);然后我们还需要在表单显示给用户之前添加以下代码:
session.removeAttribute("recordInsertedSuccessfully");
回答by Manish Nagar
submit your form using javascript with onclick event
使用带有 onclick 事件的 javascript 提交表单
do not submit form using <input type=submit>
and use <input type=button>
不要使用<input type=submit>
和使用 提交表单 <input type=button>
回答by Asif Mushtaq
What you say about session attributes? on form Submit create new session attribute and invalidate it when successfully work done.
您对会话属性有何看法?在表单提交上创建新的会话属性,并在成功完成工作后使其无效。
And on action servlet, add condition if session attribute is available then continue the form submission if not then redirect to form page.
在操作 servlet 上,如果会话属性可用,则添加条件,如果不可用,则继续提交表单,然后重定向到表单页面。