jsp生成验证码

深藏阁楼爱情的钟 2022-07-13 11:59 270阅读 0赞

使用的框架是springMVC+spring+Mybatis

生成的验证码的jsp是一张图片:可以以图片的方式加载到所需要的页面上,
生成验证码的jsp文件代码(image.jsp):

  1. <%@ page contentType="image/JPEG"
  2. import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
  3. pageEncoding="utf-8"%>
  4. <%!Color getRandColor(int fc, int bc) {
  5. //给定范围获得随机颜色
  6. Random random = new Random();
  7. if (fc > 255)
  8. fc = 255;
  9. if (bc > 255)
  10. bc = 255;
  11. int r = fc + random.nextInt(bc - fc);
  12. int g = fc + random.nextInt(bc - fc);
  13. int b = fc + random.nextInt(bc - fc);
  14. return new Color(r, g, b);
  15. }%>
  16. <%
  17. //设置页面不缓存
  18. response.setHeader("Pragma", "No-cache");
  19. response.setHeader("Cache-Control", "no-cache");
  20. response.setDateHeader("Expires", 0);
  21. // 在内存中创建图象
  22. int width = 60, height = 20;
  23. BufferedImage image = new BufferedImage(width, height,
  24. BufferedImage.TYPE_INT_RGB);
  25. // 获取图形上下文
  26. Graphics g = image.getGraphics();
  27. //生成随机类
  28. Random random = new Random();
  29. // 设定背景色
  30. g.setColor(getRandColor(200, 250));
  31. g.fillRect(0, 0, width, height);
  32. //设定字体
  33. g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
  34. //画边框
  35. //g.setColor(new Color());
  36. //g.drawRect(0,0,width-1,height-1);
  37. // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
  38. g.setColor(getRandColor(160, 200));
  39. for (int i = 0; i < 100; i++) {
  40. int x = random.nextInt(width);
  41. int y = random.nextInt(height);
  42. int xl = random.nextInt(12);
  43. int yl = random.nextInt(12);
  44. g.drawLine(x, y, x + xl, y + yl);
  45. }
  46. // 取随机产生的认证码(4位数字)
  47. String sRand = "";
  48. for (int i = 0; i < 4; i++) {
  49. String rand = String.valueOf(random.nextInt(10));
  50. sRand += rand;
  51. // 将认证码显示到图象中
  52. g.setColor(new Color(20 + random.nextInt(110), 20 + random
  53. .nextInt(110), 20 + random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
  54. g.drawString(rand, 13 * i + 6, 16);
  55. }
  56. // 将认证码存入SESSION
  57. session.setAttribute("code", sRand);
  58. // 图象生效
  59. g.dispose();
  60. // 输出图象到页面
  61. ImageIO.write(image, "JPEG", response.getOutputStream());
  62. out.clear();
  63. out = pageContext.pushBody();
  64. %>

同于加载验证码的jsp文件,

  1. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <meta http-equiv="pragma" content="No-cache">
  7. <title>登陆</title>
  8. </head>
  9. <body>
  10. <form action="/eshop/user/login" method="post">
  11. <!-- 只需要以图片的形式加载就可以了 -->
  12. 验证码:<img src="/eshop/image.jsp"><input type="text" name="code">
  13. </form>
  14. </body>
  15. </html>

验证码生成的随机字符串保存到了session里面,可以从session里面获得使用controller获得,

  1. @RequestMapping("login")
  2. public String login(String code,HttpServletRequest request){
  3. System.out.println("输入的验证码:"+code);
  4. String sessioncode = (String) request.getSession().getAttribute("code");
  5. System.out.println("图片中的验证码:"+sessioncode);
  6. return "login";
  7. }

发表评论

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

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

相关阅读

    相关 验证生成

    视图函数里 > 1,导入图片,画布,画笔,画笔上的字体,定义坐标 > 2,从外部导入字体(字体导入到static静态中) > 3,随机生成验证码,验证码颜色,验证