HTML registration verification code

Source: Internet
Author: User

Java code:

Public class captcharender implements serializable {


Private Static final long serialversionuid =-916701543933591834l;
Private Static final int width = 62, Height = 20;
Private Static final string [] strarr = {"3", "4", "5", "6", "7", "8", "9 ", "A", "B", "C", "D", "E", "F", "g", "H", "J", "K ", "M", "n", "P", "Q", "r", "S", "T", "U", "V", "W ", "X", "Y "};

Private string randomcodekey;

Public captcharender (string randomcodekey ){
If (stringkit. isblank (randomcodekey ))
Throw new illegalargumentexception ("randomcodekey can not be blank ");
This. randomcodekey = randomcodekey;
}

Public void render (){
Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );
String vcode = drawgraphic (image );
Vcode = encrypt (vcode );
Cookie = new cookie (randomcodekey, vcode );
Cookie. setmaxage (-1 );
Cookie. setpath ("/");
Response. addcookie (cookie );
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
Response. setcontenttype ("image/JPEG ");

Servletoutputstream SOS = NULL;
Try {
SOS = response. getoutputstream ();
ImageIO. Write (image, "Jpeg", SOS );
} Catch (exception e ){
Throw new runtimeexception (E );
}
Finally {
If (SOS! = NULL)
Try {SOS. Close ();} catch (ioexception e) {e. printstacktrace ();}
}
}

Private string drawgraphic (bufferedimage image ){
// Obtain the image Context
Graphics G = image. creategraphics ();
// Generate a random class
Random random = new random ();
// Set the background color
G. setcolor (getrandcolor (200,250 ));
G. fillrect (0, 0, width, height );
// Set the font
G. setfont (new font ("Times New Roman", Font. Plain, 18 ));

// Generates 155 random interference lines, making the authentication code in the image hard to be detected by other programs.
G. setcolor (getrandcolor (160,200 ));
For (INT I = 0; I <155; I ++ ){
Int x = random. nextint (width );
Int y = random. nextint (height );
Int XL = random. nextint (12 );
Int yl = random. nextint (12 );
G. drawline (X, Y, x + XL, Y + yl );
}

// Obtain the random ID code (4 digits)
String srand = "";
For (INT I = 0; I <4; I ++ ){
String Rand = string. valueof (strarr [random. nextint (strarr. Length)]);
Srand + = rand;
// Display the authentication code to the image
G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110 )));
// The Color of the called function is the same. It may be because the seed is too close and can only be directly generated.
G. drawstring (RAND, 13 * I + 6, 16 );
}

// The image takes effect
G. Dispose ();
Return srand;
}

/*
* Obtain random colors in a given range
*/
Private color getrandcolor (int fc, int BC ){
Random random = new random ();
If (FC> 255)
Fc = 255;
If (BC> 255)
BC = 255;
Int r = FC + random. nextint (BC-Fc );
Int G = FC + random. nextint (BC-Fc );
Int B = FC + random. nextint (BC-Fc );
Return new color (R, G, B );
}

Private Static final string encrypt (string srcstr ){
Try {
String result = "";
Messagedigest MD = messagedigest. getinstance ("MD5 ");
Byte [] bytes = md. Digest (srcstr. getbytes ("UTF-8 "));
For (byte B: bytes ){
String hex = integer. tohexstring (B & 0xff). touppercase ();
Result + = (Hex. Length () = 1 )? "0": "") + hex;
}
Return result;
} Catch (exception e ){
Throw new runtimeexception (E );
}
}

}

HTML code:

<Input type = "text" maxlength = "6" id = "random_code" name = "random_code" style = "width: 80px;">

<A href = "#" onclick = "changeimg ();"> <SPAN class = "login_style"> can't see it clearly? </Span> </a>

JS call:

Function changeimg ()
{
$ ("# Image"). ATTR ("src", '/IMG? S = '+ math. Random ());
}

Note:

There is a Temporary Folder temp in teomcat, and the verification code on the page is always X. Later, I checked the data room and couldn't find this because the temp temporary file was deleted.

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.