我自己用的验证码?以前的是验证码的主要代码,这次是实用的

版权声明:转载请指明出处 https://blog.csdn.net/weixin_42321963/article/details/82343794

需要引入页面位置的代码:

验证码:<input type="text" name="check"><br>
<img src="ver.jsp" onclick="this.src='ver.jsp?'+Math.random()">

需要得到的验证码的方法:

String check = request.getParameter("check");
String ver = session.getAttribute("codeValidate").toString();
out.print(check + "<br>");
out.print(ver + "<br>");

验证码代码如下,需要封装的:

<%@ page import="java.awt.*" %>
<%@ page import="java.awt.image.BufferedImage" %>
<%@ page import="java.util.Random" %>
<%@ page import="javax.imageio.ImageIO" %>
<%@ page import="java.io.IOException" %>
<%@ page contentType="image/jpeg;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>验证码</title>
    </head>
    <body>
        <h3>验证码</h3>
        <%
            int width=100;
            int height=100;
            int positionX=10;
            int positionY=50;
            Font font = new Font("微软雅黑", Font.BOLD, 25);
            BufferedImage img = new BufferedImage(width, height, 1);
            Graphics g = img.getGraphics();
            g.setFont(font);
            int x = positionX;
            int y = positionY;
            Color color = getColor(true);
            g.setColor(Color.white);
            g.drawRect(0, 0, width, height);
            color = getColor(true);
            g.setColor(Color.BLACK);
            g.fillRect(1, 1, width - 2, height - 2);
            char[] chars = getString(4);
            Random ran = new Random();
            String ccc="";
            for (int i = 0; i < chars.length; i++) {
                color = getColor(true);
                g.setColor(color);
                g.drawString(chars[i] + "", positionX, positionY);
                positionX = positionX + 15 + ran.nextInt(10);
                positionY = y + ran.nextInt(10);
                ccc=ccc+chars[i];
            }
            System.out.println(ccc);
            chars = getString(50);
            for (int i = 0; i < chars.length; i++) {
                font = new Font("微软雅黑", Font.BOLD, ran.nextInt(15));
                g.setFont(font);
                color = getColor(false);
                g.setColor(color);
                g.drawString(chars[i] + "", positionX, positionY);
                positionX = ran.nextInt(width);
                positionY = ran.nextInt(height);
            }
            int lineCount = 140;
            for (int i = 0; i < lineCount; i++) {
                color = getColor(false);
                g.setColor(color);
                g.drawLine(ran.nextInt(width), ran.nextInt(width), ran.nextInt(width), ran.nextInt(width));
            }
            try {
                ImageIO.write(img, "jpg",response.getOutputStream() );
            } catch (IOException e) {
                e.printStackTrace();
            }
            session.setAttribute("codeValidate", ccc);
            out.clear();
            out = pageContext.pushBody();


        %>
        <%!
            public static Color getColor(boolean b) {
                Random ran = new Random();
                int[] col = new int[3];
                for (int i = 0; i < 3; i++) {
                    col[i] = ran.nextInt(255);
                }
                Color color;
                if (b) {
                    color = new Color(col[0], col[1], col[2], 254);
                } else {
                    color = new Color(col[0], col[1], col[2], ran.nextInt(120));
                }
                return color;
            }
            public static char[] getString(int n) {
                String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
                char chars[] = new char[n];
                Random ran = new Random();
                for (int i = 0; i < n; i++) {
                    chars[i] = str.charAt(ran.nextInt(str.length()));
                }
              /*  for (char a : chars) {
                    System.out.println(a);
                }*/
                return chars;
            }

        %>


    </body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_42321963/article/details/82343794