JSP页面生成随机验证码

向右看齐 2022-08-03 05:05 288阅读 0赞

1、 随机生成的数字验证码

image.jsp:

  1. <%@ page import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%>
  2. <%@ page import="java.io.OutputStream"%>
  3. <%!
  4. Color getRandColor(int fc, int bc) {
  5. Random random = new Random();
  6. if (fc > 255)
  7. fc = 255;
  8. if (bc > 255)
  9. 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. <%
  17. try {
  18. response.setHeader("Pragma", "No-cache");
  19. response.setHeader("Cache-Control", "no-cache");
  20. response.setDateHeader("Expires", 0);
  21. int width = 60, height = 20;
  22. BufferedImage image = new BufferedImage(width, height,
  23. BufferedImage.TYPE_INT_RGB);
  24. OutputStream os = response.getOutputStream();
  25. Graphics g = image.getGraphics();
  26. Random random = new Random();
  27. g.setColor(getRandColor(200, 250));
  28. g.fillRect(0, 0, width, height);
  29. g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
  30. g.setColor(getRandColor(160, 200));
  31. for (int i = 0; i < 155; i++) {
  32. int x = random.nextInt(width);
  33. int y = random.nextInt(height);
  34. int xl = random.nextInt(12);
  35. int yl = random.nextInt(12);
  36. g.drawLine(x, y, x + xl, y + yl);
  37. }
  38. String sRand = "";
  39. for (int i = 0; i < 4; i++) {
  40. String rand = String.valueOf(random.nextInt(10));
  41. sRand += rand;
  42. g.setColor(new Color(20 + random.nextInt(110), 20 + random
  43. .nextInt(110), 20 + random.nextInt(110)));
  44. g.drawString(rand, 13 * i +4, 16);
  45. }
  46. //把生成的验证码放入session中,以便后面验证时使用
  47. session.setAttribute("rand", sRand);
  48. g.dispose();
  49. ImageIO.write(image, "JPEG", os);
  50. os.flush();
  51. os.close();
  52. os = null;
  53. response.flushBuffer();
  54. out.clear();
  55. out = pageContext.pushBody();
  56. } catch (IllegalStateException e) {
  57. System.out.println(e.getMessage());
  58. e.printStackTrace();
  59. }
  60. %>
  1. 随即生成的字母和数字的验证码

enimg.jsp:

  1. <%@ page import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%>
  2. <%@ page import="java.io.OutputStream"%>
  3. <%!
  4. Color getRandColor(int fc, int bc) {
  5. Random random = new Random();
  6. if (fc > 255)
  7. fc = 255;
  8. if (bc > 255)
  9. 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. <%
  17. try {
  18. response.setHeader("Pragma", "No-cache");
  19. response.setHeader("Cache-Control", "no-cache");
  20. response.setDateHeader("Expires", 0);
  21. int width = 110, height = 20;
  22. BufferedImage image = new BufferedImage(width, height,
  23. BufferedImage.TYPE_INT_RGB);
  24. OutputStream os = response.getOutputStream();
  25. Graphics g = image.getGraphics();
  26. Random random = new Random();
  27. g.setColor(getRandColor(200, 250));
  28. g.fillRect(0, 0, width, height);
  29. g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
  30. g.setColor(getRandColor(160, 200));
  31. for (int i = 0; i < 155; i++) {
  32. int x = random.nextInt(width);
  33. int y = random.nextInt(height);
  34. int xl = random.nextInt(12);
  35. int yl = random.nextInt(12);
  36. g.drawLine(x, y, x + xl, y + yl);
  37. }
  38. String[] s = { "A", "B", "C", "D", "E", "F", "G", "H", "I",
  39. "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U",
  40. "V", "W", "X", "Y", "Z" };
  41. String sRand = "";
  42. for (int i = 0; i < 4; i++) {
  43. String rand = "";
  44. if (random.nextBoolean()) {
  45. rand = String.valueOf(random.nextInt(10));
  46. } else {
  47. int index = random.nextInt(25);
  48. rand = s[index];
  49. }
  50. sRand += rand;
  51. g.setColor(new Color(20 + random.nextInt(10), 20 + random
  52. .nextInt(110), 20 + random.nextInt(110)));
  53. g.drawString(rand, 17 * i + 6, 16);
  54. }
  55. //把生成的验证码放入session中,以便后面验证时使用
  56. session.setAttribute("rand", sRand);
  57. g.dispose();
  58. ImageIO.write(image, "JPEG", os);
  59. os.flush();
  60. os.close();
  61. os = null;
  62. response.flushBuffer();
  63. out.clear();
  64. out = pageContext.pushBody();
  65. } catch (IllegalStateException e) {
  66. System.out.println(e.getMessage());
  67. e.printStackTrace();
  68. }
  69. %>

3.使用

authcode.jsp:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <!-- 点击“看不清点我”,触发onclick事件,会调用该方法 -->
  10. <script language="javascript">
  11. function loadimage(){
  12. document.getElementByIdx_x("randImage").src = "image.jsp?" + Math.random(); //后跟一个随机数,否则验证码不会变
  13. }
  14. </script>
  15. <body>
  16. <h1>登陆</h1>
  17. <hr>
  18. <form action="AuthcodeServlet" method="post">
  19. username:<input type="text" name="username" /><br>
  20. password:<input type="text" name="password" /><br>
  21. authcode:<input type="text" name="rand" /><br>
  22. <!-- 显示验证码图片,第一次进入页面,验证码有这里显示 -->
  23. <img alt="code..." name="randImage" id="randImage" src="image.jsp"
  24. width="60" height="20" border="1" align="absmiddle">
  25. <!-- 更改验证码 -->
  26. <a href="javascript:loadimage();"><font class="pt95">看不清点我</font></a><br>
  27. <input type="submit" value="login" />
  28. </form>
  29. </body>
  30. </html>

发表评论

表情:
评论列表 (有 0 条评论,288人围观)

还没有评论,来说两句吧...

相关阅读