I. Why do I get a duplicate submission of a form?
1. When the user fills out the information in the form, click the Submit button, Ken will be able to click the Submit button because he did not see the success information, resulting in the server side received two of the same information, if this information to be saved in the database, Then there will be two duplicate data, or a database operation exception may occur.
2. When the user fills in the form information completes, clicks the form submits, even if responds in time, may also appear the form duplicates submits the situation. The general server-side program after processing the user submitted information, the call is the Request.Dispatcher.forward () method to direct the user's request to a successful page, after the user sees the success information, click Refresh, the browser will again submit the user's previous input data, Just now. This is because the URL that the browser lock retains is the URL of the previous form submission if the Request.Dispatcher.forward () method is called to return a successful page, if Httpservletresponse.sendredirect () is used Method redirects the client to a successful page, there is no duplicate commit problem.
Two. How to avoid duplicate submission of forms
There are two scenarios for avoiding form recurrence, one in JavaScript scripting and the second on the server side to prevent forms from repeating commits, and we mainly focus on the server side to handle duplicate submissions of forms.
1. To access the page that contains the form, the server side in this session, creates a session object and produces a token value, which is the value of the hidden input field, which is sent to the client as a single form, and the colleague will save the token value to the session.
2. The user submits the page, the server side first determines the token value in the request parameter and the session to save to the amount of the token value is equal, if equal, clear the session of the token value, and then perform the data eat the operation, if not equal, prompts the user has submitted the form, and produces a new token value , save the newly generated token value as the value of the hidden input field when the user re-accesses the submit data page in the session.
three. Use Struts2 to prevent forms from repeating submissions
STRUTS2 enables the interceptor to check if the form is repeated, and he also uses a synchronous token to make a judgment of repeating the form.
The first step:
First, use the <s:token> tag in the table and specify the name of the one or two token,<s:token> tag to create a new token value and save the token value to the session with the token name you specified. This token value is a randomly generated sequence of encrypted characters.
from there, you can see that the,<s:token> tag generates two hidden fields in a form. The first hidden field name is fixed, struts.token.name. The name of the second hidden field is the value of the <s:token> tag Name property, and his value is the randomly generated encrypted token value. The server-side interceptor first finds the request parameter name User.token that holds the token value based on the Struts.token.name request parameters, and then gets the value of the token by getting the User.token request parameter.
Step Two:
You need to refer to the Tokeninterceptor or Tokensessionstoreinterceptor interceptor in the action configuration, These two interceptors are already defined in Struts-defatul.xml, but are not included in the Defaultstatck interceptor.
We use the Tokeninterceptor interceptor first, and when the form is submitted, the Tokeninterceptor interceptor gets the request, Obtains the standard Struts.token.name request parameter, obtains the request parameter name which holds the token value, then obtains the token value according to the request parameter, then tokeninterceptor the <s from the session according to the token name just obtained: Token> the token value that was previously saved to the session, and compares the two token values. If the token value in the session equals the token value in the request parameter, then delete the token value in the session and call the Handlevalidtoken () method, which is called directly, and he first calls the action method to process the request, and if two values are not equal, Then the Handelinvalidtoken () method is called, which first adds an action-level error message to the action (the action needs to implement the Validationaware,actionsupport base class to implement the interface). The Invalid_token_code result code is then returned directly, thus skipping the action execution. Invalid_token_code is a static constant defined in Tokeninterceptor with a value of Invalid.token
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.or G/dtds/struts-2.3.dtd "><Struts> <!--You can modify the configuration file later without restarting Tomcat as long as the following configuration is added to the configuration file - <constantname= "Struts.devmode"value= "true" /><!--Name: The value of the constant: constant - <!--solve the problem of solving Chinese garbled characters - <!--International information Inside code - <constantname= "Struts.i18n.encodeing"value= "UTF-8" /> < Packagename= "Default"namespace="/"extends= "Struts-default"> <Actionname= "token"class= "Cn.action.RegistAction" > <!--Configuring token Interceptors - <Interceptor-refname= "Defaultstack"></Interceptor-ref> <Interceptor-refname= "token"></Interceptor-ref> <!--If the commit is repeated, jump to error.jsp - <resultname= "Invalid.token">/error.jsp</result> <resultname= "Success">/success.jsp</result> </Action> </ Package></Struts>
Action
Packagecn.action;ImportCom.opensymphony.xwork2.ActionSupport; Public classRegistactionextendsactionsupport{PrivateString name; PrivateString pwd; @Override PublicString Execute ()throwsException { for(inti = 0; i < 5000000; i++) {System.out.println ("===="); } returnSUCCESS; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicString getpwd () {returnpwd; } Public voidsetpwd (String pwd) { This. PWD =pwd; } }
Registration page:
<body>
<s:actionerror/> <s:form action= "token" > <s:token name= "User.token" ></s:token> <s:textfield label= "user name" name= "name" ></s:textfield> <s:password label= "password" name= "pwd" ></s:password> <s:submit value= "Submit" ></s:submit> </s:form> </body >
If the user submits two consecutive times, it will go away causing the form to repeat the submission and will jump to the page to display an error message. You can also configure this error message.
Avoid duplicate submission and wait pages for forms