验证码

博客介绍了验证码的相关内容,包括获取4个随机数,将其画到画布上生成随机数图片,再把图片发送到客户端浏览器,验证码不区分大小写,还可进行切换。

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

验证码

获取4个随机数

String s = "0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
  Random random = new Random();
    String captcha = "";
    for(int i=0;i<4;i++)
    {
    	int a = random.nextInt(s.length());
        char c = s.charSet(a);
        captcha = captcha + c;
    }

将随机数画到画布上

BufferedImage image = new BufferedImage(100,40,Buffered.TYPE_INT_BGR);
Graphics g = image.getGraphics();
Font font = new Font("",Font.BOLD,25);
g.setFont(font);
g.drawString(captcha,10,35);

将生成的随机数图片发送到客户端浏览器

OutputStream os = response.getOutputStream();
ImageIO.write(image,"jpg",os);

验证码不区分大小写

String captchaStr = captcha.toLowerCase();.

// or

 String code = request.getParameter("code");
        String rightCode = (String)request.getSession().getAttribute("code");
        PrintWriter out = response.getWriter();
        if(code!=null && code.equalsIgnoreCase(rightCode))//字符串不区分大小写
        {
            out.print(1);
        }
        else
        {
            out.print(0);
        }
        out.close();

切换验证码

$(document).ready(function(){
	$("#changeCaptcha").click(function(){
		var random = Math.random();
		$("#captchaImg").attr("src","${pageContext.request.contextPath}/CaptchaServlet?method=getCaptcha&random="+random);
	})
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值