posts - 5,  comments - 1,  trackbacks - 0
          防止重復(fù)提交的hashCode
          在表單顯示頁面
           1//生成一個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  // 檢測重復(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%>" />
          在表單提交頁面進行如下處理
           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("請不要重復(fù)提交!");
           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 閱讀(234) 評論(0)  編輯  收藏

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


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

          有空多聯(lián)系~

          常用鏈接

          留言簿(3)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          java

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 元阳县| 乳山市| 江油市| 肥乡县| 凭祥市| 白玉县| 新闻| 武胜县| 萨迦县| 手游| 乾安县| 衢州市| 高台县| 胶州市| 郸城县| 焦作市| 章丘市| 乌拉特后旗| 庆元县| 顺义区| 红安县| 镇沅| 宜都市| 沧州市| 凤庆县| 大连市| 屯昌县| 政和县| 三原县| 松原市| 武夷山市| 五指山市| 稷山县| 平江县| 岑溪市| 大邑县| 和平县| 巴中市| 雅江县| 华宁县| 稻城县|