Prevent repeated submission of forms-Use of tokens in struts

Source: Internet
Author: User

 

Token can be used to prevent repeated page form submission problems when users refresh the page and click the back button:
The usage is as follows:

(1)

First, you must generate a token on a page to generate a random number.
You can create a page main. jsp
Then place a button or hyperlink on the page. The hyperlink points to an action class.
Used in this action class
This. savetoken (request); // generate token
Globals;
Return Mapping. findforward ("success"); // jump to the add page

(2)

Import the following two jar packages on the -- add page --
<% @ Page import = "org.apache.struts.taglib.html. constants" %>
<% @ Page import = "org. Apache. Struts. globals" %>
Then add a hidden domain on the page.
<Input type = "hidden" name = "<% = constants. token_key %>" value = "<% = session. getattribute (globals. transaction_token_key) %>"/>
The value in the name value here is fixed and cannot be changed.

(3)

In --- adduseraction -----, use istokenvalid (request) to verify the token. If the random number of token tokens generated is different
Go to the error page. If the difference is true, delete the token in the session and process the service.
If (! This. istokenvalid (request )){
Return Mapping. findforward ("error ");
}
This. resettoken (request );
// Follow the Business Code

Note:
The name of hidden must be constants. token_key.
The value of hidden must be session. getattribute (globals. transaction_token_key)

Repeated Page Submission Solution
Method 1: redirect to the new page after submission
<Forward name = "OK" Path = "/OK. jsp" Redirect = "true"/>
Set redirect = "true" to true;
Advantage: prevents users from clicking "refresh" to resubmit
Disadvantage: users cannot click "back" and submit again

Method 2: Use the token mechanism provided by struts
Similar to watching a movie, buy a ticket first, and check the ticket before entering the venue
Advantages: "refresh" and "back" can be avoided
Disadvantage: Implementation is a little troublesome.

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.