Solution to repeated submission of JSP refresh Page forms

Source: Internet
Author: User

Use sessionID and timestamp as the identifier. The key code is as follows:

Copy codeThe Code is as follows:
Public class SswpdjAction extends BaseAction {

Public String execute (){
/** Business Code **/
................
// Set the identifier
This. setSessionToken ();
// Go To The add page
Return "toAdd ";
}
  
Public String reSave (){
If (this. token! = Null & this. token. equals (this. getSessionToken ())){
/** Set a new ID **/
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 the submission form! '); </Script> ");
Out. flush ();
        
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
If (out! = Null ){
Out. close ();
}
}
}
Return null;
}
}

Public class BaseAction extends ActionSupport {

/** Jsp page ID **/
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;
}
}

/** ID 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;
}
}

The following figure shows the hidden fields of the JSP page settings:

Copy codeThe Code is as follows:
<Form>
<Input type = "hidden" name = "token" value = "$ {SessionToken}"/>
</Form>

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.