waysun一路陽光

          不輕易服輸,不輕言放棄.--心是夢的舞臺,心有多大,舞臺有多大。踏踏實實做事,認(rèn)認(rèn)真真做人。

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
            167 隨筆 :: 1 文章 :: 64 評論 :: 0 Trackbacks
          來源:http://www.aygfsteel.com/pdw2009/archive/2006/04/09/40094.html

          首先必須要通過一個Action再轉(zhuǎn)向那個添加記錄的頁面,轉(zhuǎn)向函數(shù)如下.

           public ActionForward tokenTest(
             ActionMapping mapping,
             ActionForm form,
             HttpServletRequest request,
             HttpServletResponse response) throws Exception {
            saveToken(request);//把一個token ID保存到Session,并在且要轉(zhuǎn)到的頁面
                 //的<html:form>中添加一個<input type="hideen">的標(biāo)答.
            return mapping.findForward("add");
           }


          一個輸出入頁面如容如下:

          <%@ page language="java" pageEncoding="UTF-8"%>

          <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
          <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
          <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
          <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>


          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html:html lang="true">
            <head>
              <html:base />
              
              <title>tokentest.jsp</title>
              
              <meta http-equiv="pragma" content="no-cache">
              <meta http-equiv="cache-control" content="no-cache">
              <meta http-equiv="expires" content="0">    
              <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
              <meta http-equiv="description" content="This is my page">
            </head>
            
            <body>
                <br>
                <logic:present name="error">
                   <pre style="color:#ff2255"><bean:write name="error"/></pre>
                </logic:present>
                <center>
                <html:form action="/insert.do" method="post">
                  <table border="0" cellspacing="0" >
                    <tr>
                      <td width="30%">用戶名</td>
                      <td width="70%"><html:text property="username"/></td>
                    </tr>
                    <tr>
                      <td>地址:</td>
                      <td><html:text property="address"/></td>
                    </tr>
                    <tr>
                      <td colspan="2"><html:submit value="提交"/></td>
                    </tr>
                  </table>
                 </html:form>
                </center>
            </body>
          </html:html>
          頁面的處理Action內(nèi)容如下:
             public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {
            TokenTestForm tokenTestForm = (TokenTestForm) form;
            if(!isTokenValid(request)){ //重復(fù)提交
             request.setAttribute("error","不能得復(fù)提交!!!");
             //saveToken(request); 重新生成tokenid,
             return mapping.findForward("return");
            }else{
             resetToken(request);
            }
            //執(zhí)行相關(guān)操作
            System.out.println(tokenTestForm.getUsername()+"--"+tokenTestForm.getAddress());
            return mapping.findForward("ok");
           }

          至此已完成,至于原理,就自己去查一些資料就完全明白了....


          2.來源http://www2.cnblogs.com/snoopy/articles/54699.html

          使用Struts的Token機(jī)制解決表單的重復(fù)提交

          前幾天被這個問題困擾了,在Google中搜“表單重復(fù)提交”,也搜到不少資料,但有的講的不是很清楚,所以走了些彎路,現(xiàn)在寫下來,不能算原創(chuàng)吧。


          Struts的Token(令牌)機(jī)制能夠很好的解決表單重復(fù)提交的問題,基本原理是:服務(wù)器端在處理到達(dá)的請求之前,會將請求中包含的令牌值與保存在當(dāng)前用戶會話中的令牌值進(jìn)行比較,看是否匹配。在處理完該請求后,且在答復(fù)發(fā)送給客戶端之前,將會產(chǎn)生一個新的令牌,該令牌除傳給客戶端以外,也會將用戶會話中保存的舊的令牌進(jìn)行替換。這樣如果用戶回退到剛才的提交頁面并再次提交的話,客戶端傳過來的令牌就和服務(wù)器端的令牌不一致,從而有效地防止了重復(fù)提交的發(fā)生。


          這時其實也就是兩點(diǎn),第一:你需要在請求中有這個令牌值,請求中的令牌值如何保存,其實就和我們平時在頁面中保存一些信息是一樣的,通過隱藏字段來保存,保存的形式如: 〈input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="6aa35341f25184fd996c4c918255c3ae"〉,這個value是TokenProcessor類中的generateToken()獲得的,是根據(jù)當(dāng)前用戶的session id和當(dāng)前時間的long值來計算的。第二:在客戶端提交后,我們要根據(jù)判斷在請求中包含的值是否和服務(wù)器的令牌一致,因為服務(wù)器每次提交都會生成新的Token,所以,如果是重復(fù)提交,客戶端的Token值和服務(wù)器端的Token值就會不一致。下面就以在數(shù)據(jù)庫中插入一條數(shù)據(jù)來說明如何防止重復(fù)提交。


          在Action中的add方法中,我們需要將Token值明確的要求保存在頁面中,只需增加一條語句:saveToken(request);,如下所示:
          public ActionForward add(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response)
          //前面的處理省略
          saveToken(request);
          return mapping.findForward("add");
          }
          在Action的insert方法中,我們根據(jù)表單中的Token值與服務(wù)器端的Token值比較,如下所示:
          public ActionForward insert(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response)
          if (isTokenValid(request, true)) {
          // 表單不是重復(fù)提交
          //這里是保存數(shù)據(jù)的代碼
          } else {
          //表單重復(fù)提交
          saveToken(request);
          //其它的處理代碼
          }
          }

           說明:在前一個轉(zhuǎn)向提交信息的頁面需要saveToken(request);

          在保存頁面使用

                   if(!isTokenValid(request))

                   { //重復(fù)提交

                      System.out.println("重復(fù)提交");


                     System.out.println("能得復(fù)提交!!!");

                   }

                   else

                   {

                          request.setAttribute("saveInfo", info);

                          logger.debug("save successful");

                          resetToken(request); //刪除session中的令牌 

                   ward=mapping.findForward("notice");

                   }

          posted on 2009-04-13 17:13 weesun一米陽光 閱讀(314) 評論(0)  編輯  收藏 所屬分類: Struts
          主站蜘蛛池模板: 库伦旗| 崇仁县| 临沂市| 乐都县| 桑日县| 莱西市| 天镇县| 磐石市| 固始县| 宜君县| 礼泉县| 高陵县| 内江市| 远安县| 缙云县| 洪泽县| 杭锦后旗| 花垣县| 博爱县| 哈巴河县| 昭通市| 新干县| 嘉荫县| 庆阳市| 那曲县| 汝州市| 布尔津县| 凤山县| 延庆县| 定远县| 绥阳县| 明星| 呼玛县| 稻城县| 金平| 射阳县| 乐安县| 祁阳县| 镇远县| 台中县| 手游|