Jsp implements login verification code, jsp implements Verification Code
Enter the input information index. jsp
<Html> <body>
<Forpolichod =PostAction ="Result. jsp">
<Inputtype =TextName =InputMaxlength =4>
0Src ="Image. jsp">
<Inputtype ="Submit"Value ="Submit">
</Form> </body> Generate image. jsp
<% @ Page contentType ="Image/JPEG"
Import ="Java. awt. *, java. awt. image. *, java. util. *, javax. imageio .*"
PageEncoding ="GBK"%>
<%! Color getRandColor (IntFc,IntBc) {// obtain a random color from a given range
Random random =NewRandom ();
If(Fc> 255)
Fc = 255;
If(Bc> 255)
Bc = 255;
IntR = fc + random. nextInt (bc-fc );
IntG = fc + random. nextInt (bc-fc );
IntB = fc + random. nextInt (bc-fc );
Return newColor (r, g, B );
} %>
<%
// Set the page not to cache
Response. setHeader ("Pragma", "No-cache ");
Response. setHeader ("Cache-Control", "no-cache ");
Response. setDateHeader ("Expires", 0 );
// Create an image in memory
IntWidth = 60, height = 20;
BufferedImage image =NewBufferedImage (width, height,
BufferedImage. TYPE_INT_RGB );
// Obtain the image Context
Graphics g = image. getGraphics ();
// Generate a random class
Random random =NewRandom ();
// Set the background color
G. setColor (getRandColor (200,250 ));
G. fillRect (0, 0, width, height );
// Set the font
G. setFont (NewFont ("Times New Roman", Font. PLAIN, 18 ));
// Draw a border
// G. setColor (newColor ());
// G. drawRect (0, 0, width-1, height-1 );
// Generates 155 random interference lines, making the authentication code in the image hard to be detected by other programs.
G. setColor (getRandColor (160,200 ));
For(IntI = 0; I <100; I ++ ){
IntX = random. nextInt (width );
IntY = random. nextInt (height );
IntXl = random. nextInt (12 );
IntYl = random. nextInt (12 );
G. drawLine (x, y, x + xl, y + yl );
}
// Obtain the random ID code (4 digits)
String sRand = "";
For(IntI = 0; I <4; I ++ ){
String rand = String. valueOf (random. nextInt (10 ));
SRand + = rand;
// Display the authentication code to the image
G. setColor (NewColor (20 + random. nextInt (110), 20 + random
. NextInt (110), 20 + random. nextInt (110); // the color of the called 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 );
}
// Save the authentication code to the SESSION
Session. setAttribute ("code", sRand );
// The image takes effect
G. dispose ();
// Output the image to the page
ImageIO. write (image, "JPEG", response. getOutputStream ());
%>
Verify whether the result. jsp is entered correctly
<% @ Page language ="Java"Import ="Java. util .*"PageEncoding ="GBK"%>
<Html> <body>
<%
String input = request. getParameter ("input ");
String code = (String) session. getAttribute ("code ");
If(Input. equals (code )){
Out. println ("Verification Successful! ");
}Else{
Out. println ("Verification Failed! ");
}
%>
</Body> Possible problems:
Your eclipse will prompt you: the graphic. drawString () method is incorrect.
Your jdk version is too high, but it doesn't matter. Just lower the compatible version of this project.
Procedure:
Right-click the process, select propriety, and then select "Java compiler"
Reduce the jdk compatible version to 1.4.