Java prevents repeated forms from being submitted

Source: Internet
Author: User

Prevent repeated submission of forms, or prevent refreshing and submitting forms by pressing F5.

This problem is often encountered in Web development.

Currently, there are three mainstream solutions:

1. Use scripts to solve the problem

2. Redirect to another page

3. Use the S: token tag

Because I used s2sh for development, I chose the third method.

<S: token/> is used to prevent repeated submission of forms in struts2. Its implementation class is org. Apache. struts2.views. jsp. UI. tokentag, inherited from org. Apache. struts2.views. jsp. componenttagsupport.

Tokentag componenttagsupport. dostarttag visualizes the control
Tokentag componenttagsupport. doendtag component. End call component (token)
The token is inherited from Org. apache. struts2.components. uibean, so first call uibean. end method, in uibean. the last call to the end method is defined as the protected method evaluateextraparams. This method is provided to the subclass extension of uibean and is used in the evaluateextraparams method of the token.
String token = buildtoken (tokenname );
The buildtoken method actually calls the tokenhelper. settoken method. note two points in the settoken method,
1. generateguid () method. This method is used to generate the token Value algorithm.
2. session. Put (tokenname, token). You can see that the generated value is stored in the session. Wait for future comparison.

After a guid is generated, the token interceptor checks whether the token submitted by the client form and the session saved in the session are equals. If equals is used, action is executed. Otherwise, the interceptor directly returns the invaid. Token result, and the corresponding action method will not be executed.

So the solution is:

1. Add the <s: token/> label to the front-end JSP form before submission (</form>). You do not need to modify the tag in the action.

2. Make the following configuration in the configuration file.

<Action name = "generatechoose" class = "com. fzdna. application. agent. actions. admin. manager. numbermanager. choosenumberaction "<br/> method =" generatechoose "> <br/> <Interceptor-ref name =" defaultstack "/> <br/> <Interceptor-ref name =" token" /> <br/> <result name = "success">/admin/manager/numbermanager/generatechoosenumber. JSP </result> <br/> <result name = "error">/admin/manager/numbermanager/generatechoosenumber. JSP </result> <br/> <result name = "invalid. token "type =" Redirect "> generatechoosenumber. DO </result> <br/> </Action>

This prevents repeated submission of forms.

 

Simply put, script control is used. This method is inherently flawed. If the client prohibits the use of scripts, even if your script is well written, it will be in vain. It is recommended that you perform control on the server.

Front-end JSP page:

<Form action = "duplicateaction. Do" method = "Post" onsubmit = "Return checksubmit ();"> </form>

The JS Code is as follows:

<MCE: script language = "JavaScript"> <! -- </P> <p> var checksubmitflag = false; <br/> function checksubmit () {<br/> If (checksubmitflag = true) {<br/> return false; <br/>}< br/> checksubmitflag = true; <br/> return true; <br/>}< br/> document. ondblclick = function docondblclick () {<br/> window. event. returnvalue = false; <br/>}< br/> document. onclick = function doconclick () {<br/> If (checksubmitflag) {<br/> window. event. returnvalue = false; <br/>}< br/> // --> </MCE: SCRIPT>

 

As for the transit page, you can jump to a transit JSP page when submitting the form.

With the help of the opinions of online talents and personal experience, I would like to give a brief summary.

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.