java编写一个漂亮的验证码

淩亂°似流年 2022-02-19 05:52 330阅读 0赞

效果:
在这里插入图片描述

PicCodeServlet .java代码:

  1. /** 验证码 */
  2. @WebServlet(name = "PicCodeServlet", urlPatterns = "/code")
  3. public class PicCodeServlet extends HttpServlet {
  4. private Random random = new Random();
  5. /** 写一个方法随机获取颜色 */
  6. private Color getRandomColor() {
  7. //随机生成0-255之间整数
  8. int red = random.nextInt(256);
  9. int green = random.nextInt(256);
  10. int blue = random.nextInt(256);
  11. //参数:红,绿,蓝 (0-255)
  12. return new Color(red, green, blue);
  13. }
  14. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  15. // 1. 创建缓存图片:指定宽
  16. int width = 90, height = 30;
  17. //参数:宽,高,图片模式
  18. BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  19. // 2. 获取画笔对象
  20. Graphics graphics = img.getGraphics();
  21. // 3. 设置画笔颜色
  22. graphics.setColor(Color.WHITE);
  23. // 4. 填充矩形区域
  24. graphics.fillRect(0, 0, width, height);
  25. // 5. 从字符数组中随机得到字符
  26. char[] arr = { 'A', 'B', 'C', 'D', 'N', 'E', 'W', 'b', 'o', 'y','l','c','q',
  27. '0', '1', '2', '3', '4', '5', '6','7','8','9'};
  28. // 6. 循环4次,画4个字符
  29. for (int i = 0; i < 4; i++) {
  30. int index = random.nextInt(arr.length);
  31. char c = arr[index]; //随机得到一个字符
  32. // 7. 设置字的颜色为随机
  33. graphics.setColor(getRandomColor());
  34. // 8. 设置字体,大小为18。参数:字体,样式,大小
  35. graphics.setFont(new Font(Font.DIALOG, Font.BOLD + Font.ITALIC, 19));
  36. // 9. 将每个字符画到图片,x增加,y不变。
  37. //参数:画字符,x位置,y位置。把c从字符转成字符串
  38. graphics.drawString(String.valueOf(c), 10 + (i * 20), 20);
  39. }
  40. for (int i = 0; i < 8; i++) {
  41. // 10. 线的位置是随机的,x范围在width之中,y的范围在height之中。
  42. int x1 = random.nextInt(width);
  43. int y1 = random.nextInt(height);
  44. int x2 = random.nextInt(width);
  45. int y2 = random.nextInt(height);
  46. // 11. 画8条干扰线,每条线的颜色不同
  47. graphics.setColor(getRandomColor());
  48. graphics.drawLine(x1, y1, x2, y2);
  49. }
  50. // 12. 将缓存的图片输出到响应输出流中
  51. //参数:图片对象,图片格式,输出流
  52. ImageIO.write(img, "jpg", response.getOutputStream());
  53. }
  54. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  55. this.doPost(request, response);
  56. }
  57. }

login.html
这个页面使用了bootstrap框架

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <title>登录页面</title>
  8. <link href="css/bootstrap.min.css" rel="stylesheet">
  9. <script src="js/jquery-3.2.1.min.js"></script>
  10. <script src="js/bootstrap.min.js"></script>
  11. </head>
  12. <body>
  13. <div class="container" style="max-width:400px">
  14. <h3 style="text-align: center">用户登录</h3>
  15. <form action="login" method="post" >
  16. <div class="form-group">
  17. <label for="name">用户名:</label>
  18. <input type="text" name="name" class="form-control" id="name" placeholder="请输入用户名">
  19. </div>
  20. <div class="form-group">
  21. <label for="password">密码:</label>
  22. <input type="password" name="password" class="form-control" id="password" placeholder="请输入密码"/>
  23. </div>
  24. <div class="form-inline">
  25. <label for="vcode">验证码:</label>
  26. <input type="text" name="vcode" class="form-control" id="vcode" placeholder="验证码" style="width: 70px" maxlength="4"/>
  27. <!--code是servlet的访问地址-->
  28. <img src="code" style="width: 90px; height: 30px; cursor: pointer;" title="看不清,点击刷新" id="picCode">
  29. <script type="text/javascript"> //图片的点击事件 document.getElementById("picCode").onclick = function () { this.src = "code?n=" + new Date().getTime(); //从服务器再次读取验证码 }; </script>
  30. </div>
  31. <div style="text-align: center; padding-top: 20px;">
  32. <input type="submit" value=" 登 录 " class="btn btn-primary"/>
  33. </div>
  34. </form>
  35. </div>
  36. </body>
  37. </html>

发表评论

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

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

相关阅读