Servlet Verification Code

Source: Internet
Author: User

Public class authimageservlet extends httpservlet
{
Private Static final string content_type = "text/html; charset = gb2312 ";
// Set the letter size and size
Private font mfont = new font ("Times New Roman", Font. Plain, 17 );

Public void Init () throws servletexception
{
Super. INIT ();
}
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 );
}

Public void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception
{
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
// Indicates that the generated response is an image.
Response. setcontenttype ("image/JPEG ");
Int width = 100, Height = 18;
Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );
Graphics G = image. getgraphics ();
Random random = new random ();
G. setcolor (getrandcolor (200,250 ));
G. fillrect (1, 1, width-1, height-1 );
G. setcolor (new color (102,102,102 ));
G. drawrect (0, 0, width-1, height-1 );
G. setfont (mfont );
G. setcolor (getrandcolor (160,200 ));
// Draw a random line
For (INT I = 0; I <155; I ++)
{
Int x = random. nextint (width-1 );
Int y = random. nextint (height-1 );
Int XL = random. nextint (6) + 1;
Int yl = random. nextint (12) + 1;
G. drawline (X, Y, x + XL, Y + yl );
}
// Draw a random line from the other direction
For (INT I = 0; I <70; I ++)
{
Int x = random. nextint (width-1 );
Int y = random. nextint (height-1 );
Int XL = random. nextint (12) + 1;
Int yl = random. nextint (6) + 1;
G. drawline (X, Y, X-XL, Y-yl );
}
// Generate a random number and convert the random number to a letter
String srand = "";
For (INT I = 0; I <6; I ++)
{
Int itmp = random. nextint (26) + 65;
Char CTMP = (char) itmp;
Srand + = string. valueof (CTMP );
G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110 )));
G. drawstring (string. valueof (CTMP), 15 * I + 10, 16 );
}
Httpsession session = request. getsession (true );
Session. setattribute ("RAND", srand );
G. Dispose ();
ImageIO. Write (image, "Jpeg", response. getoutputstream ());
}
Public void destroy ()
{
}

}

Then you can use src = "The Servlet Path" on the image in JSP.

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.