【问题标题】:Double Form Submission JSP Servlet双表单提交 JSP Servlet
【发布时间】:2016-04-22 13:47:03
【问题描述】:

我是 JSP 和 Servlet 的新手。我在 JSP 示例 (register.jsp) 中有一个表单,可以帮助我将数据发送到 Servlet 以便将值插入数据库。但是,在我成功插入之后,如果我点击与 (register.jsp) 相同的 URL,它会再次将我之前输入的相同数据重新提交到我的数据库中。我该如何防止这种情况?以下是我的代码

JSP

  <form action="ServletComment" method="post" class="form-inline" role="form">
        <div class="form-group">
            <input class="form-control" type="text" placeholder="Your comments" name="userComment" />
             <input type="hidden" name="Action" value="updateComment" />
        </div>
        <div class="form-group">
            <button class="btn btn-default"> Add</button>
        </div>
    </form>

小服务程序

String checkComment = null;
    checkComment = request.getParameter("Action");

    if(checkComment.equals("updateComment"))
    {
        // my coding
    }

request.getRequestDispatcher("/register.jsp").forward(request,response);  

【问题讨论】:

    标签: jsp servlets


    【解决方案1】:

    您可能会在您的servlet 中以一种常见的方式收到请求。对于不同的HTTP 请求,您应该override 不同的方法。喜欢:

        //here only http get request will go in this method
            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                //do whatever you want
            }
    
    
        //here only http post request will go in this method    
            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                String checkComment = null;
                checkComment = request.getParameter("Action");
    
                if(checkComment.equals("updateComment"))
                 {
                    // my coding
                 }
    
                request.getRequestDispatcher("/register.jsp").forward(request,response);
            }  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-18
      • 2014-11-09
      • 2016-05-02
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多