Homepage
Web. xml
<Servlet>
<Servlet-Name> randomcodectrl </servlet-Name>
<Servlet-class> com. chainway. util. randomcodectrl </servlet-class>
<Load-on-startup> 4 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-Name> randomcodectrl </servlet-Name>
<URL-pattern>/randomcodectrl </url-pattern>
</Servlet-mapping>
Randomcodectrl:
Package com. chainway. util;
Import java. Io. ioexception;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Public class randomcodectrl extends httpservlet {
Private Static final long serialversionuid = 1l;
Protected void doget (httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
Resp. setcontenttype ("image/JPEG ");
Resp. setheader ("Pragma", "No-Cache ");
Resp. setheader ("cache-control", "No-Cache ");
Resp. setdateheader ("expires", 0 );
Randomcode rc = new randomcode ();
Try {
RC. getrandcode (req, resp );
} Catch (exception e ){
System. Err. println (E );
}
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Doget (request, response );
}
}
// Generate a class
// Digital text image Verification Code
Package com. chainway. util;
Import java. AWT. color;
Import java. AWT. Font;
Import java. AWT. graphics;
Import java. AWT. image. bufferedimage;
Import java. util. Random;
Import javax. ImageIO. ImageIO;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;
Import javax. servlet. http. httpsession;
Public class randomcode {
/**
* Random acquisition of a font
* @ Param random Random Number
* @ Return font returns a new font.
*/
Private synchronized font getsfont (random ){
Return new font ("fixedsys", Font. center_baseline, 18 );
}
/**
* Returns a random color.
* @ Param int FC Random Number
* @ Param int BC Random Number
* @ Param random Random Number
* @ Return color returns a new color.
*/
Synchronized color getrandcolor (int fc, int BC, random ){
If (FC> 255) fc = 255;
If (BC> 255) BC = 255;
Int r = FC + random. nextint (bc-fc-6 );
Int G = FC + random. nextint (bc-fc-4 );
Int B = FC + random. nextint (bc-fc-8 );
Return new color (R, G, B );
}
/**
* Generate random number Image
*/
Public synchronized void getrandcode (httpservletrequest request, httpservletresponse response) throws exception {
System. setproperty ("Java. AWT. Headless", "true ");
Httpsession session = request. getsession ();
Int width = 80, Height = 26; // you can specify the image size.
Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );
Graphics G = image. getgraphics ();
Random random = new random ();
G. fillrect (0, 0, width, height); // set the border
G. setfont (new font ("Times New Roman", Font. roman_baseline, 18 ));
G. setcolor (getrandcolor (111,133, random ));
// Generate random line
For (INT I = 0; I <11; I ++ ){
Int x = random. nextint (width );
Int y = random. nextint (height );
Int XL = random. nextint (13 );
Int yl = random. nextint (15 );
G. drawline (X, Y, x + XL, Y + yl );
}
// Generate random points
G. setcolor (getrandcolor (130,150, random ));
// Generate 5 random numbers
String srand = "";
For (INT I = 0; I <5; I ++ ){
G. setfont (getsfont (random ));
G. setcolor (new color (random. nextint (101), random. nextint (111), random. nextint (121 )));
// String Rand = string. valueof (getrandomstring (random. nextint (36 )));
String Rand = string. valueof (getrandomstring (random. nextint (10 )));
Srand + = rand;
G. Translate (random. nextint (3), random. nextint (3 ));
G. drawstring (RAND, 13 * I, 16 );
}
Session. removeattribute ("RAND ");
Session. setattribute ("RAND", srand );
G. Dispose ();
ImageIO. Write (image, "Jpeg", response. getoutputstream ());
}
Public synchronized string getrandomstring (INT num ){
String randstring = "0123456789 ";
// String randstring = "0123456789 abcdefghijklmnopqrstuvwxyz ";
Return string. valueof (randstring. charat (Num ));
}
}
// Logon Judgment Method
Compare the verification code numbers from the session