JSP Refresh page Form Repeat submit problem resolution share _JSP programming

Source: Internet
Author: User
Tags md5 md5 encryption

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.