Use the Struts token mechanism to resolve the replication submission of the form
A few days ago, I have been troubled by this problem. I searched "Form Repeat Submit" in Google, but some are not very clear, so I have left some detours. Now I wrote it, can't be original.
Struts's Token mechanism can solve the problem that the form repeatedly submitted, the basic principle is that the server side will make the token value included in the request in the current user session before the server is processed. The value is compared to see if it matches. After processing the request, and before replying to the client, a new token will be generated, which will replace the old token saved in the user session in addition to the client. This way, if the user retires to the submission page and submit it again, the token passed by the client is inconsistent with the token of the server, and effectively prevents the occurrence of repeated submission.
At this time, it is actually two points, first: You need to have this token value in the request, how to save the token value in the request, actually to save some information in the page in the page, by hiding the field Saved, saved form, such as: , this value is genettoken () in the TokenProcessor class, is Calculated according to the current user's session ID and the LONG value of the current time. Second: After the client is submitted, we must consistent with the value included in the request, because the server will generate new token each time, so if it is repeated submit, the client's token value and The server-side token value is inconsistent. Here is to explain how to prevent repetition in the database.
In the Add method in Action, we need to save the token value in the page, just add a statement: SaveToken (Request); HTTPSERVLETRESPONSE RESPONSE) // The previous process omits SaveToken (Request); Return Mapping.FindForward ("add");} In the an action of the INSERT method, we compare the token value based on the token value in the form, as shown below: public ActionForward insert (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) if (isTokenValid (request, true)) {// // form is not to repeat the data stored here is the code} else {// form to repeat saveToken ( Request); // other processing code}}}