简体   繁体   English

刷新JSP表单重新提交数据

[英]Refreshing a JSP form resubmits the data

I have a jsp page called patient.jsp with a Form which is a pop-up. 我有一个名为JSP页面patient.jspForm是一个弹出。 This form is submitted using post method. 此表单是使用post方法提交的。 Once this form reached the servlet, something like below take place. 此表单到达servlet后,将发生以下类似情况。

request.setAttribute("id",id);
RequestDispatcher dispatch = getServletContect().getRequestDispatcher("/patient.jsp");
dispatch.forward(request,response);

There is a big problem. 有个大问题。 Once this is forwarded back to the patient.jsp , if the user refresh the web page, everything he previously entered into the forms will be resubmitted and saved in the database. 一旦将其转发回patient.jsp ,如果用户刷新网页,则他先前输入表单的所有内容都会重新提交并保存在数据库中。

We used RequestDispatcher because we have to pass an attribute from Request scope. 我们使用RequestDispatcher是因为我们必须从Request范围传递属性。 Any idea how to solve this? 任何想法如何解决这个问题?

First you should redirect and not forward: 首先,您应该重定向而不是转发:

response.sendRedirect("patient.jsp");

Make sure the relative path is correct. 确保相对路径正确。

From here you have two options: 在这里,您有两个选择:

  1. Set the attribute in the session and not in the request, then you can get it in the jsp. session而不是在请求中设置属性,然后可以在jsp中获得它。 Of course you need to deal with parallel requests using this, so the name of the attribute should be unique each time. 当然,您需要使用它来处理并行请求,因此属性名称每次都应该是唯一的。
  2. Send the attribute as a http get parameter (if it is serializable): response.sendRedirect("patient.jsp?id=273"); 将属性作为http get参数发送(如果可序列化): response.sendRedirect("patient.jsp?id=273");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM