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

           8  // 檢測(cè)重復(fù)問題
           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%>" />
          在表單提交頁面進(jìn)行如下處理
           1// 拿到表單的formhash
           2    String formhash = upload.getParameter("formhash");
           3    // 拿到session里面的集合
           4    Set<String> formhashSession = (Set<String>) session.getAttribute("formhashSession");
           5    // 如果沒有,則是重復(fù)提交,或者非法提交
           6    if (formhashSession == null || !formhashSession.contains(formhash)) {
           7      out.println("請(qǐng)不要重復(fù)提交!");
           8      return;
           9    }

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

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          有空多聯(lián)系~

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          java

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 临沧市| 长岭县| 巴青县| 玉屏| 四平市| 堆龙德庆县| 中西区| 丽水市| 永宁县| 获嘉县| 易门县| 靖远县| 武宁县| 闸北区| 惠来县| 老河口市| 会东县| 栾川县| 新营市| 科技| 札达县| 惠水县| 雅江县| 远安县| 湖州市| 抚顺市| 务川| 原阳县| 合作市| 饶平县| 大城县| 扶绥县| 桓台县| 五峰| 响水县| 会东县| 安多县| 都兰县| 崇信县| 周至县| 新竹县|