One solution to the problem of opening multiple pages with verification code at the same time

Source: Internet
Author: User

For details about how to use the jsp image Verification Code under Struts, refer to another article html "> jsp image verification code. The principle is to generate a four-digit Verification Code randomly, write it into the Session, and generate an image to display it. This leads to a problem. If multiple pages with verification code are opened at the same time, only the verification code on the final page passes verification. If the verification code is incorrect.

In fact, this problem was found when the verification code function was added, but it has not been changed, because few people open multiple login pages, even if an error occurs, refresh it once. Recently, the senior engineer in charge of the project has been focusing on this issue, so he has to change it.

The key code for this error is:

Request. getSession (). setAttribute ("checkcode", sRand );

That is to say, the verification code is generated every time in the checkcode variable of the Session. In this way, each generated Verification Code overwrites the previous value.

There are many solutions on the Internet, one of which is to add a timestamp to the Verification code.

Use the following code when saving the verification code to the Session:

String timestamp = (String) request. getQueryString (); // here is a parameter
Request. getSession (). setAttribute ("checkcode" + timestamp, sRand );

In fact, the principle is very simple, that is, adding a timestamp to distinguish the variables stored in the Session.

Add:

<Input id = "timestamp" type = "hidden" name = "timestamp" value = "">

Write a refresh script for the image at the same time.

Function loadimage (){
Var timestamp = (new Date (). valueOf (); // timestamp.
Document. getElementById ("randImage"). src = "<% = request. getContextPath () %>/image. jsp? "+ Timestamp;
Document. getElementById ("timestamp"). value = timestamp;
}

The server directly extracts the checkcode using the following code in form's validate.

String checkcode = (String) request. getSession (). getAttribute ("checkcode" + timestamp );

Remember to clean up the session as soon as you use it:

Request. getSession (). removeAttribute ("checkcode" + timestamp );

Reference: http://blog.kongxz.com/2010/01/solution-to-verifying-code-in-multiple-instances/

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.