<% @ Page ContentType = "Image / JPEG" Import = "java.awt. *,
Java.awt.Image. *, java.util. *, javax.imageio. * "%>
<%!
Color GetrandColor (int FC, int BC) {// Get random colors
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);
}
%>
<%
/ / Set the page does not cache
Response.setHeader ("Pragma", "NO-Cache");
Response.setHeader ("cache-control", "no-cache");
Response.SetDateHeader ("Expires", 0);
/ / Create image in memory
INT width = 60, height = 20;
BufferedImage Image = New BufferedImage (width, height, bufferedimage.type_int_rgb);
/ / Get the graphical context
Graphics g = image.getgraphics ();
/ / Generate a random class
Random Random = new random ();
// Set the background color
G.SetColor (GetrandColor (200, 250));
g.fillRect (0, 0, Width, Height);
// Set the font
G.SetFont (New Font ("Times New Roman", Font.Plain, 18));
// Sea Box
//g.setcolor (New Color ());
//g.drawrect (0, 10 ,width-1, Height 1);
// randomly generate 155 interferent lines, so that the authentication code in the image is not easily detected by other programs
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);
}
// Take the randomly generated authentication code (4 digits)
String SRAND = "";
For (int i = 0; i <4; i ) {
String Rand = String.Valueof (random.nextint (10));
SRAND = Rand;
// Display the authentication code to the image
G.SetColor (New Color (20 Random.Nextint (110), 20 Random.Nextint (110), 20 Random.Nextint (110))));
// The color of the call function is the same, it may be because the seed is too close, so it can only be generated directly.
g.drawstring (rand, 13 * i 6, 16);
// Deposit the authentication code into the session
Session.setttribute ("Rand", SRAND);
// Image take effect
g.dispose ();
// Output image to page
Imageio.write (Image, "JPEG", response.getOutputStream ());
%>
File A.jsp using the verification code picture
<% @ Page ContentType = "text / html; charset = GB2312"%>
hEAD>