Image. jsp
<% @ Page import = "Java. AWT. *, java. AWT. image. *, java. util. *, javax. ImageIO. *" %>
<% @ Page import = "Java. Io. outputstream" %>
<%!
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 );
}
%>
<%
Try {
Response. setheader ("Pragma", "No-Cache ");
Response. setheader ("cache-control", "No-Cache ");
Response. setdateheader ("expires", 0 );
Int width = 60, Height = 20;
Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );
Outputstream OS = response. getoutputstream ();
Graphics G = image. getgraphics ();
Random random = new random ();
G. setcolor (getrandcolor (200,250 ));
G. fillrect (0, 0, width, height );
G. setfont (new font ("Times New Roman", Font. Plain, 18 ));
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 );
}
String srand = "";
For (INT I = 0; I <4; I ++ ){
String Rand = string. valueof (random. nextint (10 ));
Srand + = rand;
G. setcolor (new color (20 + random. nextint (110), 20 + random. nextint (110), 20 + random. nextint (110 )));
G. drawstring (RAND, 13 * I + 6, 16 );
}
Session. setattribute ("RAND", srand );
G. Dispose ();
ImageIO. Write (image, "Jpeg", OS );
OS. Flush ();
OS. Close ();
OS = NULL;
Response. flushbuffer ();
Out. Clear ();
Out = pagecontext. pushbody ();
}
Catch (illegalstateexception E)
{
System. Out. println (E. getmessage ());
E. printstacktrace ();
} %>