Introduction to JSP: several solutions to avoid repeated submission of Form forms

Source: Internet
Author: User

It is a very difficult question to submit a beginner Form for JSP beginners. How can we avoid multiple submission of Form forms? You can start from the following aspects:

1. For beginner JSP, you can set it in Javascript first.

Set a variable and only allow the form to be submitted once.

 
  1. ﹤script language="javascript"﹥   
  2.  var checkSubmitFlg = false;   
  3.  function checkSubmit() {   
  4.  if (checkSubmitFlg == true) {   
  5.  return false;   
  6.  }   
  7.  checkSubmitFlg = true;   
  8.  return true;   
  9.  }   
  10.  document.ondblclick = function docondblclick() {   
  11.  window.event.returnValue = false;   
  12.  }   
  13.  document.onclick = function doconclick() {   
  14.  if (checkSubmitFlg) {   
  15.  window.event.returnValue = false;   
  16.  }   
  17.  }   
  18. ﹤/script﹥   
  19.    
  20. ﹤html:form action="myAction.do" method="post" onsubmit="return checkSubmit();"﹥  

2. for JSP, you must master another Javascript setting.

Set the form submission button or image to disable

 
  1.  ﹤html:form action="myAction.do" method="post"   
  2.  onsubmit="getElById('submitInput').disabled = true; return true;"﹥    
  3.  ﹤html:image styleId="submitInput" src="images/ok_b.gif" border="0" /﹥    
  4.  ﹤/html:form﹥ 

3. Be good at using the STRUTS synchronization token mechanism in the entry stage of JSP.
 
The synchronous Token mechanism is used to solve the problem of repeated Form submission in Web applications. Struts also provides a reference implementation.

Basic Principles:

Before processing the incoming request, the server compares the token value contained in the request with the token value saved in the current user session to see if it matches. After the request is processed and the reply is sent to the client, a new token will be generated. In addition to sending the token to the client, the old token saved in the user session is also replaced. In this way, if the user goes back to the submission page and submits the request again, the token sent from the client is inconsistent with the token sent from the server, effectively preventing repeated submission.

  1. If (isTokenValid (request, true )){
  2. // Your code here
  3. Return mapping. findForward ("success ");
  4. } Else {
  5. SaveToken (request );
  6. Return mapping. findForward ("submitagain ");
  7. }

STRUTS generates a unique token for each session based on the user session ID and the current system time. For details, refer to the generateToken () method in the TokenProcessor class.

1. // verify the transaction control token.

2. In action:

 
  1. // <InputType="Hidden" Name="Org.apache.struts.taglib.html. TOKEN"
  2. //Value="6aa35341f25184fd996c4c918255c3ae">
  3. If (! IsTokenValid (request ))
  4. Errors. add (ActionErrors. GLOBAL_ERROR,
  5. New ActionError ("error. transaction. token "));
  6. ResetToken (request); // Delete the token in the session

3. action has such a method to generate a token

 
  1. protected String generateToken(HttpServletRequest request) {   
  2.  HttpSession session = request.getSession();   
  3.  try {   
  4.  byte id[] = session.getId().getBytes();   
  5.  byte now[] =   
  6.  new Long(System.currentTimeMillis()).toString().getBytes();   
  7.  MessageDigest md = MessageDigest.getInstance("MD5");   
  8.  md.update(id);   
  9.  md.update(now);   
  10.  return (toHex(md.digest()));   
  11.  } catch (IllegalStateException e) {   
  12.  return (null);   
  13.  } catch (NoSuchAlgorithmException e) {   
  14.  return (null);   
  15.  }   
  16.  }  


During update, the form button is prevented from being repeatedly clicked. The Session is used for judgment.

4. Master the page mode when getting started with JSP

 
  1. ﹤input type="hidden " name=" ﹤% =com.lims.util.SynchroToken.TOKEN_NAME%﹥ " value =" ﹤%= com.lims.util.SynchroToken.getToken(request)%﹥" ﹥    
  2.  
  3. SynchroToken.java  
  4. package com.lims.util;  
  5. import org.apache.struts.util.*;  
  6. import javax.servlet.http.*;  
  7. import javax.servlet.jsp.*;  
  8. import org.apache.struts.action.*;  
  9. /**  
  10.   * ﹤p﹥Title: SynchroToken ﹤/p﹥  
  11.   * ﹤p﹥Description: ﹤/p﹥  
  12.   * ﹤p﹥Copyright: Copyright (c) 2004﹤/p﹥  
  13.   * ﹤p﹥Company: NetStar﹤/p﹥  
  14.   * @author Jstar  
  15.   * @version 1.0  
  16.   * Created in 2004/04/21  
  17.   */  
  18. public class SynchroToken{  
  19.  public final static java.lang.String TOKEN_NAME = "_token";  
  20.  public static boolean checkToken (HttpServletRequest request){  
  21.    boolean isEqual = false;  
  22.    HttpSession session = request.getSession ();  
  23.    String formToken = request.getParameter (TOKEN_NAME);  
  24.    String sessionToken = (String)session.getAttribute (TOKEN_NAME);  
  25.    System.out.println ("formToken: " + formToken + " sessionToken: " +  
  26.                        sessionToken);  
  27.    if (formToken != null && sessionToken == null){  
  28.      session.setAttribute (TOKEN_NAME, formToken);  
  29.      isEqual = true;  
  30.    }  
  31.    return isEqual;  
  32.  }  
  33.  /**  
  34.   * Insert the method's description here.  
  35.   * Creation date: (4/19/2004 3:23:25 PM)  
  36.   * @return java.lang.String  
  37.   * @param request javax.servlet.http.HttpServletRequest  
  38.   */  
  39.  public static String getToken (HttpServletRequest request){  
  40.    String token = "" + System.currentTimeMillis ();  
  41.    HttpSession session = request.getSession ();  
  42.    if (session != null){  
  43.      session.removeAttribute (TOKEN_NAME);  
  44.    }  
  45.    return token;z  
  46.  }  
  47.  /**  
  48.   * Insert the method's description here.  
  49.   * Creation date: (4/19/2004 3:24:10 PM)  
  50.   * @return java.lang.String  
  51.   */  
  52.  final static java.lang.String getTOKEN_NAME (){  
  53.    return TOKEN_NAME;  
  54.  }  
  55.  public static String message (PageContext pageContext, String key) throws  
  56.      JspException{  
  57.    return RequestUtils.message (pageContext, null, null, key);  
  58.  }  
  59. }   
  60.  

5. You can also add a transit page

From the perspective of getting started with JSP, you can add a transit page. Although this is not visually good, the page is obviously cumbersome, however, this is a good way to effectively avoid repeated submission of Form forms.


  1. Use JSP include mechanism to improve appearance
  2. Six common issues for JSP Programming
  3. Use of scripts Based on JSP tutorials
  4. How Tomcat improves performance on JSP pages
  5. What is JSP and its strong weakness?

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.