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

6 篇文章 0 订阅

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

验证码:<input type="text" name="check"><br>
<img src="ver.jsp" οnclick="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>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值