• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • JSP避免Form重復提交的三種方案

    發表于:2007-07-04來源:作者:點擊數: 標簽:
    責任編輯:fuxing [網人帝國] 添加時間:2004-8-2 7:56:38 人氣:4024 【大 中


    責任編輯:fuxing [網人帝國] 添加時間:2004-8-2 7:56:38 人氣:4024 【大 中 小】



      1 javascript ,設置一個變量,只允許提交一次?!?

      <script language="javascript">

       var checkSubmitFlg = false;

       function checkSubmit() {

       if (checkSubmitFlg == true) {

       return false;

       }

       checkSubmitFlg = true;

       return true;

       }

       document.ondblclick = function docondblclick() {

       window.event.returnValue = false;

       }

       document.onclick = function doconclick() {

       if (checkSubmitFlg) {

       window.event.returnValue = false;

       }

       }

      </script>

      

      <html:form action="myAction.do" method="post" onsubmit="return checkSubmit();">  

      2 還是javascript,將提交按鈕或者image置為disable  

       <html:form action="myAction.do" method="post"

       onsubmit="getElById(''submitInput'').disabled = true; return true;">   

       <html:image styleId="submitInput" src="images/ok_b.gif" border="0" /> 

       </html:form>  

      3 利用struts的同步令牌機制  

      利用同步令牌(Token)機制來解決Web應用中重復提交的問題,Struts也給出了一個參考實現。

      基本原理: 

      服務器端在處理到達的請求之前,會將請求中包含的令牌值與保存在當前用戶會話中的令牌值進行比較,看是否匹配。在處理完該請求后,且在答復發送給客戶端之前,將會產生一個新的令牌,該令牌除傳給客戶端以外,也會將用戶會話中保存的舊的令牌進行替換。這樣如果用戶回退到剛才的提交頁面并再次提交的話,客戶端傳過來的令牌就和服務器端的令牌不一致,從而有效地防止了重復提交的發生?!?

      if (isTokenValid(request, true)) {

       // your code here

       return mapping.findForward("suclearcase/" target="_blank" >ccess");

      } else {

       saveToken(request);

       return mapping.findForward("submitagain");

      } 

      Struts根據用戶會話ID和當前系統時間來生成一個唯一(對于每個會話)令牌的,具體實現可以參考TokenProcessor類中的generateToken()方法?! ?

      1. //驗證事務控制令牌,<html:form >會自動根據session中標識生成一個隱含input代表令牌,防止兩次提交

      2. 在action中:  

       //<input type="hidden" name="org.apache.struts.taglib.html.TOKEN"

       // value="6aa35341f25184fd996c4c918255c3ae">

       if (!isTokenValid(request))

    errors.add(ActionErrors.GLOBAL_ERROR,

       new ActionError("error.transaction.token"));

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

      3. action有這樣的一個方法生成令牌  

       protected String generateToken(HttpServletRequest request) {  

       HttpSession session = request.getSession();

       try {

       byte id[] = session.getId().getBytes();

       byte now[] =

       new Long(System.currentTimeMillis()).toString().getBytes();

       MessageDigest md = MessageDigest.getInstance("MD5");

       md.update(id);

       md.update(now);

       return (toHex(md.digest()));

       } catch (IllegalStateException e) {

       return (null);

       } catch (NoSuchAlgorithmException e) {

       return (null);

       }

       }





    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>