在Web应用程序中不使用JavaScript或JQuery防止多次单击提交按钮?

在Web应用程序中不使用JavaScript或JQuery防止多次单击提交按钮?,java,mysql,jsp,web-applications,data-entry,Java,Mysql,Jsp,Web Applications,Data Entry,我的简单web应用程序如下:我有一个JSP表单(MyForm.JSP),它接受用户输入并将其传递给我的第一个servlet(“/myfirstservlet”) 此servlet将用户输入值的SQL插入myFruits表,然后将用户重定向到my results servlet(“/results”) My results servlet然后检查一个“ADD”参数,如果参数为“true”(即它等于“success”),它最终会将用户重定向到My results JSP(results.JSP),它

我的简单web应用程序如下:我有一个JSP表单(
MyForm.JSP
),它接受用户输入并将其传递给我的第一个servlet(
“/myfirstservlet”

此servlet将用户输入值的SQL插入my
Fruits
表,然后将用户重定向到my results servlet(
“/results”

My results servlet然后检查一个
“ADD”
参数,如果参数为“true”(即它等于
“success”
),它最终会将用户重定向到My results JSP(
results.JSP
),它存储在路径中:
WEB-INF/MyFolder/results.JSP

我的JSP表单(
MyForm.JSP
)也存储在路径中:
WEB-INF/MyFolder/MyForm.JSP

我这样做是为了防止用户通过单击Results JSP页面上的refresh按钮重新提交表单,从而避免之前输入数据库的相同数据的多个条目

我现在的问题是:如何防止用户在我的表单(
MyForm.JSP
)上多次单击提交按钮,从而防止相同数据的多行进入我的数据库,而不使用JavaScript或JQuery

基本上,我想在服务器(而不是客户端)中验证表单是否只提交了一次

我的JSP表单(
MyForm.JSP
):

我的结果servlet(
“/results”
):

我的结果JSP(
Results.JSP
):


如果您有一个登录用户的id字段,这将更容易,因为您可以为特定用户提交的结果创建一个表,在将其输入到FROUTS表之前,检查该用户是否已经提交了相同的数据

从外观上看,它似乎没有任何用户标识字段,因此防止重复的一种黑客方法可能是使用会话

会话对于当前使用您的应用程序/网站的用户是唯一的。每个访问您的网站/应用程序的人都会获得自己唯一的会话id。(它们存储为cookie)

例如:

protected void doPost(...){
   String fruit = request.getParameter("fruit");
   String color = request.getParameter("color");

   //unless you wanna complicate things, i would create a string out of the two parameters and store it into an arraylist of strings
   String value = fruit+color; 

   HttpSession session = (request.getSession()); //get session
   if(null == session.getAttribute("duplicates")){ //if session variable empty then we know that user has not submitted anything yet so we let them insert into db

     insertFruit(fruit,color); //add to db

     ArrayList<String> duplicates = new ArrayList<String>(); //create arraylist
     duplicates.add(value); //add our unique value
     session.setAttribute("duplicates", duplicates); //set as session variable

    }else{
     //here the session variable is not empty so that means the user has already submitted something so lets check the arraylist and make sure the value does not already exist

     ArrayList<String> duplicates = (ArrayList<String>) session.getAttribute("duplicates");

     if(!duplicates.contains(value)){
      //if arraylist does not contain the same value, then it's safe to add
       insertFruit(fruit,color); //add to db

      //forgot this part
      duplicates.add(value);
      session.setAttribute("duplicates", duplicates); //update the variable
     }


    }


   response.sendRedirect("results?ADD=SUCCESS");
}

public void insertFruit(String fruit, String color){

       try(Connection connect = SQLHelperClass.connectOnly()){
         PreparedStatement pst = connect.prepareStatement("INSERT INTO practice (fruit, color) VALUES (?, ?);");

        pst.setString(1, fruit);
        pst.setString(2, color);

        pst.executeUpdate();

          }catch (SQLException e) {
            e.printStackTrace();
          }

}
要从servlet调用此函数,只需执行以下操作:

protected void doPost(...){
   FruitDao fdao = new FruitDao(); // get the db class for fruits
   String fruit = request.getParameter("fruit");
   String color = request.getParameter("color");

   //unless you wanna complicate things, i would create a string out of the two parameters and store it into an arraylist of strings
   String value = fruit+color; 

   HttpSession session = (request.getSession()); //get session
   if(null == session.getAttribute("duplicates")){ //if session variable empty then we know that user has not submitted anything yet so we let them insert into db

     fdao.insertFruit(fruit,color); //add to db

     ArrayList<String> duplicates = new ArrayList<String>(); //create arraylist
     duplicates.add(value); //add our unique value
     session.setAttribute("duplicates", duplicates); //set as session variable

    }else{
     //here the session variable is not empty so that means the user has already submitted something so lets check the arraylist and make sure the value does not already exist

     ArrayList<String> duplicates = (ArrayList<String>) session.getAttribute("duplicates");

     if(!duplicates.contains(value)){
      //if arraylist does not contain the same value, then it's safe to add
        fdao.insertFruit(fruit,color); //add to db

      //forgot this part
      duplicates.add(value);
      session.setAttribute("duplicates", duplicates); //update the variable
     }


    }


   response.sendRedirect("results?ADD=SUCCESS");
}
protectedvoiddopost(…){
FruitDao fdao=new FruitDao();//获取水果的db类
字符串fruit=request.getParameter(“fruit”);
字符串颜色=request.getParameter(“颜色”);
//除非你想让事情复杂化,否则我会用两个参数创建一个字符串,并将其存储到字符串的arraylist中
字符串值=水果+颜色;
HttpSession会话=(request.getSession());//获取会话
如果(null==session.getAttribute(“duplicates”){//如果会话变量为空,那么我们知道用户还没有提交任何内容,所以我们让他们插入到数据库中
fdao.insertFruit(水果,颜色);//添加到数据库
ArrayList duplicates=新建ArrayList();//创建ArrayList
重复项。添加(值);//添加我们的唯一值
session.setAttribute(“duplicates”,duplicates);//设置为会话变量
}否则{
//这里session变量不是空的,这意味着用户已经提交了一些内容,所以让我们检查arraylist并确保该值不存在
ArrayList duplicates=(ArrayList)session.getAttribute(“duplicates”);
如果(!duplicates.contains(值)){
//如果arraylist不包含相同的值,则可以安全地添加
fdao.insertFruit(水果,颜色);//添加到数据库
//忘了这部分
重复。添加(值);
session.setAttribute(“duplicates”,duplicates);//更新变量
}
}
response.sendRedirect(“结果?添加=成功”);
}

<代码> >你考虑<代码> <代码> JavaScript吗?我已经知道在SelvOK中验证是更好的做法,但是你能告诉我你正在验证什么吗?是否有服务器输入的用户输入数据的表单,如果是真的,那么阻止来自同一数据的表单的其他多个提交点击。基本上,它是为了确保每个填写和提交的表单一次只在数据库中插入一行。通常在创建web表单时,通过引导用户体验来引导用户行为是一种良好的做法。禁用按钮或显示流程覆盖是避免多次提交的常用方法。如果希望在服务器端执行此操作,请考虑是否希望后端api限制多个
POST
请求?如果是这样的话,DB完整性或速率限制可能是不错的选择。这里需要注意的是,如果用户删除了他们的ssid cookie,他们将能够提交相同的数据。另外,请查看insertFruit方法,这就是您应该如何连接数据库的方法。通过将其包装在try-catch块中,它将自动为您关闭连接。session.setAttributes()需要一个对象作为第二个参数。我的db connection SQLHelperClass有另一个外部类,该类有一个返回连接对象的方法connectOnly()。这种方式比为我拥有的每个servlet重复编写它更不理想吗?@JaeBin
setAttribute
not
setAttributes
protected void doPost(...){

   response.setContentType("text/html");    

   if (request.getParameter("ADD").equals("SUCCESS"))
      request.getRequestDispatcher("WEB-INF/MyFolder/Results.jsp").forward(request, response);

}
<body>
<h1>Results JSP</h1>


  //Reads data from MySQL database and prints it as an Array List.

</body>
protected void doPost(...){


       String fruit = request.getParameter("fruit");
       String color = request.getParameter("color");

       try 
        {
            String sql2 = "INSERT INTO practice (fruit, color) VALUES (?, ?);";
            Connection connect = SQLHelperClass.connectOnly();
            PreparedStatement pstmt;
            pstmt = connect.prepareStatement(sql2);
            pstmt.setString(1, fruit);
            pstmt.setString(2, color);

            pstmt.execute();

            response.sendRedirect("results?ADD=success");
        } 

        catch (SQLException e) 
        {           
            e.printStackTrace();
        }

}
protected void doPost(...){
   String fruit = request.getParameter("fruit");
   String color = request.getParameter("color");

   //unless you wanna complicate things, i would create a string out of the two parameters and store it into an arraylist of strings
   String value = fruit+color; 

   HttpSession session = (request.getSession()); //get session
   if(null == session.getAttribute("duplicates")){ //if session variable empty then we know that user has not submitted anything yet so we let them insert into db

     insertFruit(fruit,color); //add to db

     ArrayList<String> duplicates = new ArrayList<String>(); //create arraylist
     duplicates.add(value); //add our unique value
     session.setAttribute("duplicates", duplicates); //set as session variable

    }else{
     //here the session variable is not empty so that means the user has already submitted something so lets check the arraylist and make sure the value does not already exist

     ArrayList<String> duplicates = (ArrayList<String>) session.getAttribute("duplicates");

     if(!duplicates.contains(value)){
      //if arraylist does not contain the same value, then it's safe to add
       insertFruit(fruit,color); //add to db

      //forgot this part
      duplicates.add(value);
      session.setAttribute("duplicates", duplicates); //update the variable
     }


    }


   response.sendRedirect("results?ADD=SUCCESS");
}

public void insertFruit(String fruit, String color){

       try(Connection connect = SQLHelperClass.connectOnly()){
         PreparedStatement pst = connect.prepareStatement("INSERT INTO practice (fruit, color) VALUES (?, ?);");

        pst.setString(1, fruit);
        pst.setString(2, color);

        pst.executeUpdate();

          }catch (SQLException e) {
            e.printStackTrace();
          }

}
public void insertFruit(String fruit, String color){

       try(Connection connect = SQLHelperClass.connectOnly()){
         PreparedStatement pst = connect.prepareStatement("INSERT INTO practice (fruit, color) VALUES (?, ?);");

        pst.setString(1, fruit);
        pst.setString(2, color);

        pst.executeUpdate();

          }catch (SQLException e) {
            e.printStackTrace();
          }

}
protected void doPost(...){
   FruitDao fdao = new FruitDao(); // get the db class for fruits
   String fruit = request.getParameter("fruit");
   String color = request.getParameter("color");

   //unless you wanna complicate things, i would create a string out of the two parameters and store it into an arraylist of strings
   String value = fruit+color; 

   HttpSession session = (request.getSession()); //get session
   if(null == session.getAttribute("duplicates")){ //if session variable empty then we know that user has not submitted anything yet so we let them insert into db

     fdao.insertFruit(fruit,color); //add to db

     ArrayList<String> duplicates = new ArrayList<String>(); //create arraylist
     duplicates.add(value); //add our unique value
     session.setAttribute("duplicates", duplicates); //set as session variable

    }else{
     //here the session variable is not empty so that means the user has already submitted something so lets check the arraylist and make sure the value does not already exist

     ArrayList<String> duplicates = (ArrayList<String>) session.getAttribute("duplicates");

     if(!duplicates.contains(value)){
      //if arraylist does not contain the same value, then it's safe to add
        fdao.insertFruit(fruit,color); //add to db

      //forgot this part
      duplicates.add(value);
      session.setAttribute("duplicates", duplicates); //update the variable
     }


    }


   response.sendRedirect("results?ADD=SUCCESS");
}