美文网首页
Java+JSP实现算数验证码

Java+JSP实现算数验证码

作者: 念念碎平安夜 | 来源:发表于2019-12-30 14:09 被阅读0次
    类似《纯Java+JSP验证码》
    一、创建Servlet类验证结果
    @WebServlet("/valiCode2")
    public class ValiCode2 extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // 1. 得到数据
            String inCode = request.getParameter("inCode").toString().toLowerCase();
            String valiCode = request.getSession().getAttribute("valiCode").toString().toLowerCase();
            // 2. 验证是否正确
            if (inCode.equals(valiCode)) {
                response.sendRedirect("index.jsp");
            } else {
                request.getSession().setAttribute("err", "验证码输入错误,请重新输入!");
                // 返回上一页
                String url = request.getHeader("Referer");
                response.sendRedirect(url);
            }
        }
    
    }
    
    二、生成算数画板
    @WebServlet("/newCode2")
    public class NewCode2 extends HttpServlet {
        private Random random = new Random(); // 随机数对象
        private int width = 140; // 宽度
        private int height = 30; // 高度
        private int fontsize = 18; // 字体大小
        private String str = "+-*/";
        private int value = -1; // 保存计算结果
    
        private String randCode() {
            int one = random.nextInt(100);
            int two = random.nextInt(100) + 1;
            char op = str.charAt(random.nextInt(str.length()));
            switch (op) {
            case '+':
                value = one + two;
                break;
            case '-':
                value = one - two;
                break;
            case '*':
                value = one * two;
                break;
            case '/':
                value = one / two;
                break;
            }
            return "" + one + op + two + "=?";
        }
    
        // 返回随机颜色
        private Color randColor() {
            int r = random.nextInt(256);
            int g = random.nextInt(256);
            int b = random.nextInt(256);
            return new Color(r, g, b);
        }
    
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // 1.创建画板
            BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            // 2.创建画笔
            Graphics2D pen = img.createGraphics();
            // 3.生成随机内容
            String code = randCode();
            request.getSession().setAttribute("valiCode", value);
            // 4.绘制内容
            // 4.1设置绘制区域
            pen.fillRect(0, 0, width, height);
            // 4.2设置字体
            pen.setFont(new Font("微软雅黑", Font.BOLD, fontsize + random.nextInt(5)));
            // 4.3按顺序逐个绘制字符
            for (int i = 0; i < code.length(); i++) {
                pen.setColor(randColor());
                // 绘制字符
                pen.drawString(code.charAt(i) + "", 5 + i * fontsize, (fontsize + height) / 2 + random.nextInt(5));
            }
            // 4.4绘制噪音线
            for (int i = 0; i < 2; i++) {
                pen.setColor(randColor());
                pen.setStroke(new BasicStroke(3));
                pen.drawLine(random.nextInt(width / 2), random.nextInt(height), random.nextInt(width),
                        random.nextInt(height));
            }
    
            // 5.存为图片并发送
            ServletOutputStream out = response.getOutputStream();
            ImageIO.write(img, "png", out);
            out.flush();
            out.close();
        }
    }
    
    三、用户登录页面
    <style type="text/css">
    .code_a {
        color: #0000ff;
        font-size: 12px;
        text-decoration: none;
        cursor: pointer;
    }
    
    #imgCode {
        cursor: pointer;
    }
    </style>
    <script type="text/javascript">
        function changeCode() {
            var imgCode = document.getElementById("imgCode");
            imgCode.src = "newCode2?" + Math.random();
        }
    </script>
    </head>
    <body>
        <form action="valiCode2" method="post">
            <label>验证码:</label> <input type="text" id="inCode" name="inCode" /> <img
                alt="" src="newCode2" align="center" id="imgCode"
                onclick="changeCode()"> <a class="code_a"
                onclick="changeCode()">换一张</a><br /> <input type="submit" value="登录" />
        </form>
        <div style="color: red;">${err}</div>
    </body>
    

    相关文章

      网友评论

          本文标题:Java+JSP实现算数验证码

          本文链接:https://www.haomeiwen.com/subject/eoqloctx.html