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

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 濮阳市| 安泽县| 多伦县| 桦南县| 天祝| 丹凤县| 和平区| 富宁县| 金平| 新平| 太仓市| 长汀县| 正镶白旗| 从化市| 双牌县| 淮南市| 喀喇沁旗| 宁乡县| 信丰县| 桐庐县| 石嘴山市| 潼关县| 江川县| 宿松县| 德令哈市| 从江县| 介休市| 剑河县| 阜宁县| 那曲县| 耒阳市| 色达县| 东丽区| 辽宁省| 横山县| 河北区| 敖汉旗| 云和县| 泰和县| 福安市| 南城县|