posts - 5,  comments - 1,  trackbacks - 0
          防止重復提交的hashCode
          在表單顯示頁面
           1//生成一個formhash,算法可以自己定,不隨便重復就可以了
           2  String formhash = MD5.encode(Long.toString(new Date().getTime()));
           3  //讀取當前session里面的hashCode集合,此處使用了Set,方便判斷。
           4  Set<String> formhashSession = (Set<String>) session.getAttribute("formhashSession");
           5  if (formhashSession == null{
           6    formhashSession = new HashSet<String>();
           7  }

           8  // 檢測重復問題
           9  while (formhashSession.contains(formhash)) {
          10    formhash = MD5.encode(Long.toString(new Date().getTime()));
          11  }

          12  // 保存到session里面
          13  formhashSession.add(formhash);
          14  // 保存
          15  session.setAttribute("formhashSession", formhashSession);
          表單里面增加如下字段
          <input type="hidden" name="formhash" id="formhash" value="<%=formhash%>" />
          在表單提交頁面進行如下處理
           1// 拿到表單的formhash
           2    String formhash = upload.getParameter("formhash");
           3    // 拿到session里面的集合
           4    Set<String> formhashSession = (Set<String>) session.getAttribute("formhashSession");
           5    // 如果沒有,則是重復提交,或者非法提交
           6    if (formhashSession == null || !formhashSession.contains(formhash)) {
           7      out.println("請不要重復提交!");
           8      return;
           9    }

          10    // 下面進行其它的操作
          11    // 
          12    // 最后,如果操作成功,從session里面把這個formhash 刪掉!
          13    // 以免用戶少填寫了某個字段,造成表單無法再次提交
          14    formhashSession.remove(formhash);
          15    session.setAttribute("formhashSession", formhashSession);
          posted on 2008-02-28 16:52 herry 閱讀(232) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          有空多聯系~

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          java

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 商河县| 柳州市| 凤庆县| 苏尼特右旗| 宝山区| 阿鲁科尔沁旗| 岫岩| 乐安县| 开远市| 攀枝花市| 阜平县| 密山市| 洪洞县| 鞍山市| 徐汇区| 苏州市| 阜平县| 澄迈县| 望江县| 灌南县| 喀喇沁旗| 万盛区| 滨州市| 印江| 永新县| 伊宁市| 花垣县| 乌拉特中旗| 台北县| 安义县| 合水县| 农安县| 宣汉县| 巍山| 浮梁县| 新乡市| 丽江市| 泽普县| 琼中| 馆陶县| 武清区|