publicclassSswpdjActionextendsBaseAction{
publicStringexecute(){
/**业务代码**/
................
//设置标识
this.setSessionToken();
//转到添加页面
return"toAdd";
}
publicStringreSave(){
if(this.token!=null&&this.token.equals(this.getSessionToken())){
/**设置新标识**/
this.setSessionToken();
/**业务代码**/
..............
return"toAdd";
}else{
printWriterout=null;
try{
httpServletResponse.setContentType("text/html;charset=UTF-8");
out=httpServletResponse.getWriter();
out.println("<script>alert('刷新提交表单!');</script>");
out.flush();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(out!=null){
out.close();
}
}
}
returnnull;
}
}
publicclassBaseActionextendsActionSupport{
/**jsp页面标识**/
protectedStringtoken;
publicStringgetToken(){
returntoken;
}
publicvoidsetToken(Stringtoken){
this.token=token;
}
publicStringgetSessionToken(){
if(null!=httpSession.getAttribute("Token")){
returnhttpSession.getAttribute("Token");
}else{
returnnull;
}
}
/**标识生成**/
publicvoidsetSessionToken(){
Stringflag=useMd5(httpSession.getId()+System.currentTimeMillis());
httpSession.setAttribute("Token",flag);
httpServletRequest.setAttribute("SessionToken",flag);
}
/**MD5加密**/
privateStringuseMd5(Stringstr){
byte[]bs=str.getBytes();
Stringresult=null;
try{
MessageDigestmd5=MessageDigest.getInstance("MD5");
md5.update(bs);
result=md5.digest().toString();
}catch(NoSuchAlgorithmExceptione){
e.printStackTrace();
}finally{
}
returnresult;
}
}
JSP页面设置标识隐藏域:
代码如下:
<form>
<inputtype="hidden"name="token"value="${SessionToken}"/>
</form>
|