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.