Using SessionID and timestamps as identifiers, the key code is as follows:
Copy Code code as follows:
public class Sswpdjaction extends baseaction{
Public String execute () {
/** Business code **/
................
Set identity
This.setsessiontoken ();
Go to add page
return "Toadd";
}
Public String resave () {
if (This.token!= null && this.token.equals (This.getsessiontoken ())) {
/** Set new Identity **/
This.setsessiontoken ();
/** Business code **/
..............
return "Toadd";
}else{
PrintWriter out = null;
try{
Httpservletresponse.setcontenttype ("Text/html;charset=utf-8");
out = Httpservletresponse.getwriter ();
Out.println (' <script>alert (' Refresh submit form! ');</script> ");
Out.flush ();
}catch (IOException e) {
E.printstacktrace ();
}finally{
if (out!= null) {
Out.close ();
}
}
}
return null;
}
}
public class Baseaction extends actionsupport{
/**jsp page Identification **/
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;
}
}
/** Identity Generation **/
public void Setsessiontoken () {
String flag = USEMD5 (Httpsession.getid () + System.currenttimemillis ());
Httpsession.setattribute ("Token", flag);
Httpservletrequest.setattribute ("Sessiontoken", flag);
}
/**MD5 Encryption **/
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 page settings identify hidden fields:
Copy Code code as follows:
<form>
<input type= "hidden" name= "token" value= "${sessiontoken}"/>
</form>