In development, verification codes are commonly used to effectively prevent such problems from constantly trying to log on to a specific registered user using brute-force cracking of a specific program.
This demo contains three files:
1. index. jsp: logon page
2. image. jsp: image page for generating Verification Code
3. result. jsp: result page
[Page display]
[Page code]
1. index. jsp
Xml Code
01. 02. <form method = post action = "result. jsp">
03. <input type = text name = input maxlength = 4>
04.
05. <input type = "submit" value = "submit">
06. </form> </body> [Note]: (1) Use the maxlength attribute to restrict the input characters;
(2) Use the label to display the generated Verification Code image.
2. image. jsp
01. <% @ page contentType = "image/JPEG"
02. import = "java. awt. *, java. awt. image. *, java. util. *, javax. imageio .*"
03. pageEncoding = "GBK" %>
04. <%! Color getRandColor (int fc, int bc) {// obtain a random Color from a given range
05. Random random = new Random ();
06. if (fc> 255)
07. fc = 255;
08. if (bc> 255)
09. bc = 255;
10. int r = fc + random. nextInt (bc-fc );
11. int g = fc + random. nextInt (bc-fc );
12. int B = fc + random. nextInt (bc-fc );
13. return new Color (r, g, B );
14.} %>
15. <%
16. // set the page to not cache
17. response. setHeader ("Pragma", "No-cache ");
18. response. setHeader ("Cache-Control", "no-cache ");
19. response. setDateHeader ("Expires", 0 );
20.
21. // create an image in the memory
22. int width = 60, height = 20;
23. BufferedImage image = new BufferedImage (width, height,
24. BufferedImage. TYPE_INT_RGB );
25.
26. // obtain the image Context
27. Graphics g = image. getGraphics ();
28.
29. // generate a random class
30. Random random = new Random ();
31.
32. // set the background color
33. g. setColor (getRandColor (200,250 ));
34. g. fillRect (0, 0, width, height );
35.
36. // set the font
37. g. setFont (new Font ("Times New Roman", Font. PLAIN, 18 ));
38.
39. // draw the border www.2cto.com
40. // g. setColor (new Color ());
41. // g. drawRect (0, 0, width-1, height-1 );
42.
43. // 155 interference lines are randomly generated, making the authentication code in the image hard to be detected by other programs.
44. g. setColor (getRandColor (160,200 ));
45. for (int I = 0; I <100; I ++ ){
46. int x = random. nextInt (width );
47. int y = random. nextInt (height );
48. int xl = random. nextInt (12 );
49. int yl = random. nextInt (12 );
50. g. drawLine (x, y, x + xl, y + yl );
51 .}
52.
53. // obtain the random ID code (4 digits)
54. String sRand = "";
55. for (int I = 0; I <4; I ++ ){
56. String rand = String. valueOf (random. nextInt (10 ));
57. sRand + = rand;
58. // display the authentication code in the image
59. g. setColor (new Color (20 + random. nextInt (110), 20 + random
60 .. nextInt (110), 20 + random. nextInt (110); // The color from the call function is the same, probably because the seed is too close, so it can only be generated directly.
61. g. drawString (rand, 13 * I + 6, 16 );
62 .}
63.
64. // Save the authentication code to the SESSION
65. session. setAttribute ("code", sRand );
66.
67. // The image takes effect
68. g. dispose ();
69.
70. // output the image to the page
71. ImageIO. write (image, "JPEG", response. getOutputStream ());
72. %>
[Note]:
(1) set the contentType value to "image/JPEG"
3. result. jsp
<% @ Page language = "java" import = "java. util. *" pageEncoding = "GBK" %>
01. 02. <%
03. String input = request. getParameter ("input ");
04. String code = (String) session. getAttribute ("code ");
05. if (input. equals (code )){
06. out. println ("Verification Successful! ");
07.} else {
08. out. println ("Verification Failed! ");
09 .}
10. %>
11. body> html>
From ___ loveOfForever