Backup is the most important
I saw one today, I tried it, I can use it at once, and I have not made it. There are three source code for three files. Let's take a look at what bugs, help change, thank you!
<%
//image.jsp
%>
<% @ 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)))))) Close, so only directly generate
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 ());
%>
<%
//a.jsp
%>
<% @ Page ContentType = "text / html; charset = GB2312"%>
3C
// DTD HTML 4.01 TransitionAl // en ">
<
Meta
Http-equiv = "expires" Content = "0">
hEAD>