使用sessionID和時間戳作為標識,關鍵代碼如下:
復制代碼 代碼如下:
public class SswpdjAction extends BaseAction{
public String execute(){
/**業務代碼**/
................
//設置標識
this.setSessionToken();
//轉到添加頁面
return "toAdd";
}
public String reSave(){
if(this.token != null && this.token.equals(this.getSessionToken())){
/**設置新標識**/
this.setSessionToken();
/**業務代碼**/
..............
return "toAdd";
}else{
printWriter out = null;
try{
httpServletResponse.setContentType("text/html;charset=UTF-8");
out = httpServletResponse.getWriter();
out.println("<script>alert('刷新提交表單!');</script>");
out.flush();
}catch(IOException e){
e.printStackTrace();
}finally{
if(out != null){
out.close();
}
}
}
return null;
}
}
public class BaseAction extends ActionSupport{
/**jsp頁面標識**/
protected String token;
public String getToken(){
return token;
}
public void setToken(String token){
this.token = token;
}
public String getSessionToken(){
if(null != httpSession.getAttribute("Token")){
return httpSession.getAttribute("Token");
}else{
return null;
}
}
/**標識生成**/
public void setSessionToken(){
String flag = useMd5(httpSession.getId() + System.currentTimeMillis());
httpSession.setAttribute("Token", flag);
httpServletRequest.setAttribute("SessionToken", flag);
}
/**MD5加密**/
private String useMd5(String str){
byte[] bs = str.getBytes();
String result = null;
try{
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(bs);
result = md5.digest().toString();
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}finally{
}
return result;
}
}
JSP頁面設置標識隱藏域:
復制代碼 代碼如下:
<form>
<input type="hidden" name="token" value="${SessionToken}" />
</form>