JSP (struts) solutions to avoid repeated form submission

Source: Internet
Author: User
1. Javascript. Set a variable and submit it only once.

<Script language = "JavaScript"> var checksubmitflg = false; function checksubmit () {If (checksubmitflg = true) {return false;} checksubmitflg = true; return true;} document. ondblclick = function docondblclick () {window. event. returnvalue = false;} document. onclick = function doconclick () {If (checksubmitflg) {window. event. returnvalue = false ;}}</SCRIPT> <HTML: Form Action = "myaction. do "met Hod = "Post" onsubmit = "Return checksubmit ();"> 2 or Javascript: Set the submit button or image to disable <HTML: Form Action = "myaction. do "method =" Post "onsubmit =" getelbyid ('submitinput '). disabled = true; return true; "> <HTML: Image styleid =" submitinput "src =" images/OK _ B .gif "border =" 0 "/> Errors. add (actionerrors. global_error, new actionerror ("error. transaction. token "); resettoken (request); // Delete the token in the session 3. action has such a method to generate the token protected string generatetoken (httpservletrequest request) {httpsession session = request. getsession (); try {byte ID [] = session. GETID (). getbytes (); byte now [] = new long (system. currenttimemillis ()). tostring (). getbytes (); messagedigest MD = messagedigest. getinstance ("MD5"); Md. update (ID); Md. update (now); Return (tohex (MD. digest ();} catch (illegalstateexception e) {return (null);} catch (nosuchalgorithmexception e) {return (null );}}

During the update process, the button is prevented from being repeatedly clicked. The session is used to determine whether the JSP page can be used in JSP/servlet.
<Input type = "hidden" name = "<% = com. LIMs. util. synchrotoken. token_name %> "value =" <% = com. LIMs. util. synchrotoken. gettoken (request) %> ">
Synchrotoken. Java
PackageCom. LIMS. util;ImportOrg. Apache. Struts. util .*;
ImportJavax. servlet. http .*;
ImportJavax. servlet. jsp .*;
ImportOrg. Apache. Struts. Action .*;/**
* <P> title: synchrotoken </P>
* <P> Description: </P>
* <P> copyright: Copyright (c) 2004 </P>
* <P> company: netstar </P>
* @ Author jstar
* @ Version 1.0
* Created in 2004/04/21
*/Public ClassSynchrotoken {Public Final StaticJava. Lang. String token_name = "_ token ";Public Static BooleanChecktoken (httpservletrequest request ){BooleanIsequal =False; Httpsession session = request. getsession (); string formtoken = request. getparameter (token_name); string sessiontoken = (string) session. getattribute (token_name); system. out. println ("formtoken:" + formtoken + "sessiontoken:" + sessiontoken );If(Formtoken! =Null& Sessiontoken =Null) {Session. setattribute (token_name, formtoken); isequal =True;}ReturnIsequal;}/*** Insert the method's description here. * creation date: (4/19/2004 3:23:25 pm) * @ return Java. lang. string * @ Param request javax. servlet. HTTP. httpservletrequest */Public StaticString gettoken (httpservletrequest request) {string token = "" + system. currenttimemillis (); httpsession session = request. getsession ();If(Session! =Null) {Session. removeattribute (token_name );}ReturnToken; z}/*** Insert the method's description here. * creation date: (4/19/2004 3:24:10 pm) * @ return java. Lang. String */Final StaticJava. Lang. String gettoken_name (){ReturnToken_name ;}Public StaticString message (pagecontext, string key)ThrowsJspexception {ReturnRequestutils. Message (pagecontext,Null,Null, Key );}}

4. Add a transit page
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.