JSP randomly generated Verification Code

Source: Internet
Author: User
Code:
  1. Code:
  2. 1. A. jsp
  3. <% @ Page contenttype = "text/html; charset = gb2312" %>
  4. <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
  5. <HTML>
  6. <Head>
  7. <Title> certificate code input page </title>
  8. <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
  9. <Meta HTTP-EQUIV = "Pragma" content = "no-Cache">
  10. <Meta HTTP-EQUIV = "cache-control" content = "no-Cache">
  11. <Meta HTTP-EQUIV = "expires" content = "0">
  12. </Head>
  13. <Body>
  14. <Form method = post action = "check. jsp">
  15. <Table>
  16. <Tr>
  17. <TD align = left> the verification code generated by the system: </TD>
  18. <TD> </TD>
  19. </Tr>
  20. <Tr>
  21. <TD align = left> enter the above verification code: </TD>
  22. <TD> <input type = text name = Rand maxlength = 4 value = ""> </TD>
  23. </Tr>
  24. <Tr>
  25. <TD colspan = 2 align = center> <input type = submit value = "Submit detection"> </TD>
  26. </Tr>
  27. </Form>
  28. </Body>
  29. </Html>
  30. 2. image. jsp
  31. <% @ Page contenttype = "image/JPEG; charset = gb18030" Import = "Java. AWT. *, Java. AWT. image. *, Java. util. *, javax. imageIO. * "%>
  32. <%!
  33. Color getrandcolor (int fc, int BC) {// obtain a random color from a given range
  34. Random random = new random ();
  35. If (FC> 255) fc = 255;
  36. If (BC> 255) BC = 255;
  37. Int r = FC + random. nextint (BC-Fc );
  38. Int G = FC + random. nextint (BC-Fc );
  39. Int B = FC + random. nextint (BC-Fc );
  40. Return new color (R, G, B );
  41. }
  42. %>
  43. <%
  44. // Set the page not to cache
  45. Response. setheader ("Pragma", "No-Cache ");
  46. Response. setheader ("cache-control", "No-Cache ");
  47. Response. setdateheader ("expires", 0 );
  48. // Create an image in memory
  49. Int width = 60, Height = 20;
  50. Bufferedimage image = new bufferedimage (width, height, bufferedimage. type_int_rgb );
  51. // Obtain the image Context
  52. Graphics G = image. getgraphics ();
  53. // Generate a random class
  54. Random random = new random ();
  55. // Set the background color
  56. G. setcolor (getrandcolor (200,250 ));
  57. G. fillrect (0, 0, width, height );
  58. // Set the font
  59. G. setfont (new font ("Times New Roman", Font. Plain, 18 ));
  60. // Draw a border
  61. // G. setcolor (new color ());
  62. // G. drawrect (0, 0, width-1, height-1 );
  63. // Generates 155 random interference lines, making the authentication code in the image hard to be detected by other programs.
  64. G. setcolor (getrandcolor (160,200 ));
  65. For (INT I = 0; I <155; I ++)
  66. {
  67. Int x = random. nextint (width );
  68. Int y = random. nextint (height );
  69. Int XL = random. nextint (12 );
  70. Int yl = random. nextint (12 );
  71. G. drawline (X, Y, x + XL, Y + yl );
  72. }
  73. // Obtain the random ID code (4 digits)
  74. String srand = "";
  75. For (INT I = 0; I <4; I ++ ){
  76. String Rand = string. valueof (random. nextint (10 ));
  77. Srand + = rand;
  78. // Display the authentication code to the image
  79. G. setcolor (new color (20 + random. nextint (110), 20 + random. 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.
  80. G. drawstring (RAND, 13 * I + 6, 16 );
  81. }
  82. // Save the authentication code to the session
  83. Session. setattribute ("RAND", srand );
  84. // The image takes effect
  85. G. Dispose ();
  86. // Output the image to the page
  87. ImageIO. Write (image, "Jpeg", response. getoutputstream ());
  88. %>
  89. 3. Check. jsp
  90. <% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *" errorpage = "" %>
  91. <HTML>
  92. <Head>
  93. <Title> verification page </title>
  94. <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
  95. <Meta HTTP-EQUIV = "Pragma" content = "no-Cache">
  96. <Meta HTTP-EQUIV = "cache-control" content = "no-Cache">
  97. <Meta HTTP-EQUIV = "expires" content = "0">
  98. </Head>
  99. <Body>
  100. <%
  101. String Rand = (string) Session. getattribute ("RAND ");
  102. String input = request. getparameter ("RAND ");
  103. %>
  104. The verification code generated by the system is: <% = Rand %> <br>
  105. The verification code you entered is: <% = input %> <br>
  106. <Br>
  107. <%
  108. If (RAND. Equals (input )){
  109. %>
  110. <Font color = green> the input is the same and the authentication is successful! </Font>
  111. <%
  112. } Else {
  113. %>
  114. <Font color = Red> If the input is different, authentication fails! </Font>
  115. <%
  116. }
  117. %>
  118. </Body>
  119. </Html>

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.