An example class that generates a verification code using Java code

Source: Internet
Author: User

Call in JSP as follows:

<Table width = "100%" border = "0" cellpadding = "0" cellspacing = "0" style = "margin: 0 auto;">
<Tr>
<TD width = "20"> <HTML: Text property = "validatecode" size = "8"/> </TD>

<TD> & nbsp; </TD> <TD align = "Left" valign = "TOP"> <Div id = "validatecodeimage" valign = "TOP"> </div>

<SCRIPT type = "text/JavaScript"> updatevalidatecode (70, 25) </SCRIPT> <

/TD>
</Tr>
</Table>

JS scripts

Function updatevalidatecode (width, height ){
Document. getelementbyid ('validatecodeimag '). innerhtml = '/validatecode. do "style =" cursor: Hand "alt =" if you cannot see the verification code, please click picture refresh "/> ';
}

Import java. AWT. color;
Import java. AWT. Font;
Import java. AWT. graphics;
Import java. AWT. image. bufferedimage;
Import java. Io. ioexception;
Import java. util. Random;

Import javax. ImageIO. ImageIO;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

Import org. Apache. commons. Lang. stringutils;
Import org. Apache. Struts. action. Action;
Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Action. actionforward;
Import org. Apache. Struts. Action. actionmapping;

/**
* Myeclipse struts creation date: 05-31-2007
*
* XDoclet definition:
*
* @ Struts. Action validate = "true"
*/
Public class validatecodeaction extends action {
/*
* Generated Methods
*/

/**
* Method Execute
*
* @ Param Mapping
* @ Param form
* @ Param request
* @ Param response
* @ Return actionforward
*/
Public actionforward execute (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response ){
// Set the length and width of the image
Int width = stringutils. isnotblank (request. getparameter ("width "))? Integer
. Parseint (request. getparameter ("width "))
: 0;
Int Height = stringutils. isnotblank (request. getparameter ("height "))? Integer
. Parseint (request. getparameter ("height "))
: 0;
If (width <70 | width> 140 ){
Width = 70;
}
If (height <25 | height> 50 ){
Height = 25;
}

// Create memory image
Bufferedimage image = new bufferedimage (width, height,
Bufferedimage. type_int_rgb );
// Obtain the image Context
Graphics G = image. creategraphics ();

// Set the background color of the image (because it is a background, so it is light)
G. setcolor (getrandcolor (180,250 ));
G. fillrect (0, 0, width, height );
// Set the font
G. setfont (new font ("Arial", Font. Bold, 19 ));

// Four verification codes are generated by default.
Int length = 4;
// Set Random Seed
Java. util. Random Rand = new random ();

// Set the alternative verification code, which includes "a-Z" and numbers "0-9". Some confusing characters are removed.
String base = "abcdefghijkmnqrstuvwxyz23456789 ";
Int size = base. Length ();
Stringbuffer STR = new stringbuffer ();
For (INT I = 0; I <length; I ++ ){
Int start = Rand. nextint (size );
String tmpstr = base. substring (start, start + 1 );

Str. append (tmpstr );
// Generate a random color (because it is a foreground, so it is too deep)
G. setcolor (getrandcolor (10,150 ));

// Draw the word on the Image
// G. drawstring (Str. tostring (), 4, 17 );
G. drawstring (tmpstr, 13 * I + 6 + Rand. nextint (5), 14 + Rand
. Nextint (6 ));
// Random image points
For (Int J = 0; j <20; j ++ ){
Int x = Rand. nextint (width );
Int y = Rand. nextint (height );
G. drawoval (X, Y, 0, 0 );
}
/*
// Draw a random line
For (Int J = 0; j <2; j ++ ){
Int x = Rand. nextint (width );
Int y = Rand. nextint (height );
Int X1 = Rand. nextint (12 );
Int Y1 = Rand. nextint (12 );
G. drawline (X, Y, x + X1, Y + Y1 );
}
*/
}

// Save the authentication code to the session
Request. getsession (). setattribute ("validatecode", str. tostring ());

// The image takes effect
G. Dispose ();

// Set the page not to cache
Response. setdateheader ("expires",-1 );
Response. setheader ("cache-control ",
"No-store, Private, post-check = 0, pre-check = 0, Max-age = 0 ");
Response. setheader ("Pragma", "No-Cache ");

// Output the image to the page
Try {
ImageIO. Write (image, "Jpeg", response. getoutputstream ());
} Catch (ioexception e ){
E. printstacktrace ();
}

Return NULL;
}

// Obtain a random color in a given range
Color getrandcolor (int fc, int BC ){
Random random = new random ();
'If (FC> 255 ){
Fc = 255;
}
If (BC & gt; 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 );
}

}

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.