使用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>