java编写简单的验证码

本文详细介绍了如何使用Java和Servlet技术生成并显示验证码图片,包括验证码的生成算法、图片渲染及前端显示和刷新机制,同时提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


******************************servlet***************************

        

        BufferedImage image = CheckCodeUtil.getImage();
        String checkCodeString = CheckCodeUtil.getCodeString();
        req.getSession().setAttribute("code", checkCodeString);
        //设置响应类型
        resp.setContentType("image/jpeg");
        //输出图片
        ImageIO.write(image, "jpeg", resp.getOutputStream());

**************************jsp************************  

 <script type="text/javascript">
        function reload(){
            var img = document.getElementById("img");
            img.src = "code?id=" + new Date(); 
        }
    </script>

  <body>
      <font color="red">${error}</font>
      <form action="check">
          验证码:<input type="text" name="code"/>
           <img src="code" onclick="this.src='code?r='+Math.random()" id="img"/>
           <a href="javascript:reload()">看不清,换一张</a><br/>
           <input type="submit" value="提交"/>
       </form>
  </body>


********************************生成验证码的工具类***********************

package com.chinasoft.checkcode.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

public class CheckCodeUtil {
    private static String code = null;
    public static String getCodeString(){
        return code;
    }
    private static Font getFont(){
        Random r = new Random();
        Font[] f = new Font[5];
        f[0] = new Font("微软雅黑",Font.PLAIN,20);
        f[1] = new Font("Antique Olive Compact",Font.PLAIN,20);
        f[2] = new Font("Forte",Font.PLAIN,20);
        f[3] = new Font("Wide Latin",Font.PLAIN,20);
        f[4] = new Font("Gill Sans Ultra Bold",Font.PLAIN,20);
        return f[r.nextInt(f.length)];
    }
    public static BufferedImage getImage(){
        code = "";
        Random r = new Random();
        //创建画板
        BufferedImage image = new BufferedImage(100, 30, BufferedImage.TYPE_INT_RGB);
        //创建一支笔
        Graphics g = image.getGraphics();
        //为笔设置颜色,即画板的背景色,值越大颜色越浅
        g.setColor(new Color(180,208,255));
        //创建一张画纸,位置,宽度,长度
        g.fillRect(0, 0, 100, 30);
        String random = "QWERTYUPLKJHGFDSAZXCVBNM98765432";
        for (int i = 0; i < 4; i++) {
            //设置字体
            g.setFont(getFont());
            //为画的字符设置颜色
            g.setColor(new Color(r.nextInt(100),r.nextInt(100),r.nextInt(100)));
            char c = random.charAt(r.nextInt(random.length()));
            g.drawString(c+"", i*20+8, 25);
            code += c;
        }
        //画干扰线
        for (int i = 0; i < 4; i++) {
            g.setColor(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
            g.drawLine(r.nextInt(100), r.nextInt(30), r.nextInt(100), r.nextInt(30));
        }
        //画干扰点
        for (int i = 0; i < 20; i++) {
            g.setColor(new Color(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
            g.drawOval(r.nextInt(100), r.nextInt(30),3, 3);
        }
        return image;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁星***满天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值