亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

防止在 Web 應用程序中不使用 JavaScript 或 JQuery 的多次提交按鈕單擊?

防止在 Web 應用程序中不使用 JavaScript 或 JQuery 的多次提交按鈕單擊?

繁星coding 2021-11-24 14:52:52
我的簡單 web 應用程序如下: 我有一個 JSP 表單 ( MyForm.JSP),它接受用戶輸入并將其傳遞給我的第一個 servlet ( "/myfirstservlet")。這個 servlet 處理用戶輸入值的 SQL 插入到我的Fruits表中,然后將用戶重定向到我的結果 servlet ( "/results")。我的結果 servlet 然后檢查 an"ADD"參數,如果“真”(即等于"success"),它最終將用戶重定向到我的結果 JSP(Results.JSP),它存儲在路徑中: WEB-INF/MyFolder/Results.jsp。我的 JSP 表單 ( MyForm.JSP) 也存儲在路徑中: WEB-INF/MyFolder/MyForm.jsp我這樣做是為了防止用戶通過單擊 Results JSP 頁面上的刷新按鈕重新提交表單,從而避免對之前剛剛輸入到數據庫中的相同數據進行多次輸入。我現在的問題是:如何防止點擊提交按鈕,用戶多的窗體上的時間(MyForm.JSP),從而防止相同數據的多個行越來越進入我的數據庫WITHOUT使用JavaScript或JQuery的?基本上,我想在我的服務器而不是客戶端驗證表單只提交了一次。我的 JSP 表單 ( MyForm.JSP):<form action="myfirstservlet" do="POST">   <input type="text" name="fruit"><br>   <input type="text" name="color"><br>   <input type="submit" value="Submit"></form>我的第一個 servlet ( "/myfirstservlet"):protected void doPost(...){   String fruit = request.getParameter("fruit");   String color = request.getParameter("color");   String sql = "INSERT INTO fruits (fruit, color) VALUES" + "(\"" + fruit +  "\", \""  + color +  "\");";   utilitySQL.sqlInsert(sql); // My utility class that handles sql inserts   response.sendRedirect("results?ADD=SUCCESS");}我的結果 servlet ( "/results"):protected void doPost(...){   response.setContentType("text/html");       if (request.getParameter("ADD").equals("SUCCESS"))      request.getRequestDispatcher("WEB-INF/MyFolder/Results.jsp").forward(request, response);}我的結果 JSP ( Results.JSP):<body><h1>Results JSP</h1>  //Reads data from MySQL database and prints it as an Array List.</body>
查看完整描述

1 回答

?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

如果你有一個登錄用戶的 id 字段,這會更容易,因為你可以為特定用戶提交的結果創建一個表,在將其輸入到 Fruits 表之前,檢查用戶是否已經提交了相同的數據.


從它的外觀來看,您似乎沒有任何用戶標識字段,因此防止重復的一種黑客方法可能是利用會話。


會話對于當前使用您的應用程序/網站的用戶是唯一的。每個訪問您的網站/應用程序的人都會獲得自己唯一的會話 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();

          }


}

編輯 1:


關于不為每個 servlet 重復數據庫操作的評論。你需要把邏輯分開。人們通常的做法是為所有數據庫操作創建一個單獨的類。


例如...


創建一個名為 的類FruitDao,在這里保存所有與水果相關的數據庫操作


公共類 FruitDao{


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");

}


查看完整回答
反對 回復 2021-11-24
  • 1 回答
  • 0 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號