java - java中如何防止刷新后重新提交

标签 java jquery html jsp

我有多个网页jsp,我用于刷新response.sendredirect("blabla.jsp")中重新提交,但其中一个页面工作正常,另一页按我提交后转到空白页面,并将行添加到数据库,这个问题有什么解决办法吗?谢谢你

`

<% String UC1 = "INIT";

    if (request.getParameter("add_spec") != null) {
        UC1 = "ADD_SPEC";
    }
    if (UC1.equals("INIT")) {
        List<Speciality> specs = SpecialityController.INSTANCE.findAll();
%>

<%@include file="./WEB-INF/Add_Spec.jspf" %>
<%@include file="./WEB-INF/view_all_specs.jspf" %> 
<%}
    if (UC1.equals("ADD_SPEC")) {
        String spec = request.getParameter("speciality");

        SpecialityController.INSTANCE.create(new Speciality(spec));
        List<Speciality> specs = SpecialityController.INSTANCE.findAll();
          response.sendRedirect("main_admin.jsp");

%>

<%@include file="./WEB-INF/Add_Spec.jspf" %>
<%@include file="./WEB-INF/view_all_specs.jspf" %> 
<% }
%>

`

最佳答案

您可以使用Post/Redirect/Get模式。

When a web form is submitted to a server through an HTTP POST request, a web user that attempts to refresh the server response in certain user agents can cause the contents of the original POST request to be resubmitted, possibly causing undesired results, such as a duplicate web purchase. To avoid this problem, many web developers use the PRG pattern - instead of returning a web page directly, the POST operation returns a redirection command.

换句话说,当您提交数据时,您应该重定向到可以查看(获取)刚刚添加的数据的页面。

这样刷新就不会重新提交数据。

或者,您可以使用 CSRF/XSRF -类似 token 。

尽管this example是用 PHP 写的,你应该明白它的要点。

更新

更好的是,您可以查看这个 CSRF 示例 https://services.teammentor.net/article/00000000-0000-0000-0000-000000040a2e

关于java - java中如何防止刷新后重新提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50455802/

相关文章:

java - 将 Vector 转换为 HTML5 Canvas(DXF 到 Canvas 坐标)

html - 使用 jspdf 字体将 HTML 表导出为 pdf 不支持

javascript - 当用户按 Enter 键时,jQueryUI 自动完成返回 AJAX 错误 0

html - <Body> 标签的边距和填充

html - 让浏览器在三字段登录中存储登录详细信息

java - 将 1 小时添加到 android 中的当前时间

java - 如何获取动态创建的文本字段的值

java.io.IOException : mark/reset not supported

javascript - ajax 代码没有在后台将我导航到 url

javascript - 标题基于不断变化的图像?