CheckNum. jsp
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *, java. SQL. *" pageEncoding = "UTF-8" %>
<% @ Page contentType = "image/jpeg" import = "java. awt. *, java. awt. image. *, javax. imageio. *" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<%!
Color getRandColor (int fc, int bc) // random Color selection within a given range
{
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 );
}
%>
<%
Response. setHeader ("Pragma", "No-cache"); // set the page to not Buffer
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 );
Graphics g = image. getGraphics (); // obtain the image Context
Random random = new Random (); // generates Random objects.
G. setColor (getRandColor (200,250 ));
G. fillRect (0, 0, width, height );
G. setFont (new Font ("Times New Roman", Font. PLAIN, 18); // set the Font
For (int I = 0; I <155; I ++)
{
Int x = random. nextInt (width );
Int y = random. nextInt (height );
Int x1 = random. nextInt (12 );
Int y1 = random. nextInt (12 );
G. drawLine (x, y, x + x1, y + y1 );
}
// Generate a random Verification Code
String sRand = "";
For (int I = 0; I <4; I ++)
{
String rand = String. valueOf (random. nextInt (10 ));
SRand + = rand;
// Display the verification code to the image
G. setColor (new Color (20 + random. nextInt (110), 20 + random. nextInt (110), 20 + random. nextInt (110 )));
// Set the color of the random number.
G. drawString (rand, 13 * I + 6, 16 );
}
// Store the verification code in the session
Session. setAttribute ("rand", sRand );
// The image takes effect
G. dispose ();
// Output the image to the page
ImageIO. write (image, "JPEG", response. getOutputStream ());
Out. clear ();
Out = pageContext. pushBody ();
%>
You can log on to login. jsp using the following method;
Verification Code: <input class = "imgbutton" name = "checknum" type = "text" size = "15" id = "checknum" maxlength = "4"/>
<br/>
<Input type = "submit" name = "submit" value = "login"/>