(编辑:jimmy 日期: 2024/11/11 浏览:2)
使用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>