onsubmit=
利用同步令牌(Token)機制來解決Web應用中重復提交的問題
基本原理
服務器端在處理到達的請求之前
// your code here
return mapping
} else {
saveToken(request);
return mapping
}
Struts根據用戶會話ID和當前系統時間來生成一個唯一(對於每個會話)令牌的
if (!isTokenValid(request))
errors
new ActionError(
resetToken(request); //刪除session中的令牌
protected String generateToken(HttpServletRequest request) {
HttpSession session = request
try {
byte id[] = session
byte now[] =
new Long(System
MessageDigest md = MessageDigest
md
md
return (toHex(md
} catch (IllegalStateException e) {
return (null);
} catch (NoSuchAlgorithmException e) {
return (null);
}
} 在更新的時候防止按鈕重復點擊
在JSP/Servlet中可以
JSP頁面
SynchroToken
package com
import org
import javax
import org
/**
*
Title: SynchroToken
*
Description:
*
Copyright: Copyright (c)
*
Company: NetStar
* @author Jstar
* @version
* Created in
*/
public class SynchroToken{
public final static java
public static boolean checkToken (HttpServletRequest request){
boolean isEqual = false;
HttpSession session = request
String formToken = request
String sessionToken = (String)session
System
sessionToken);
if (formToken != null && sessionToken == null){
session
isEqual = true;
}
return isEqual;
}
/**
* Insert the method
* Creation date: (
* @return java
* @param request javax
*/
public static String getToken (HttpServletRequest request){
String token =
HttpSession session = request
if (session != null){
session
}
return token;z
}
/**
* Insert the method
* Creation date: (
* @return java
*/
final static java
return TOKEN_NAME;
}
public static String message (PageContext pageContext
JspException{
return ssage (pageContext
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28913.html