前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用JavaScript刷新验证码

使用JavaScript刷新验证码

作者头像
全栈程序员站长
发布2022-11-01 13:17:49
9560
发布2022-11-01 13:17:49
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

今天学习了验证码的开发,日常生活中经常点验证码,今天自己也来做一个验证码

使用JavaScript刷新验证码
使用JavaScript刷新验证码

首先是用一个文件产生随机验证码:

<%@page import=”java.awt.*”%> <%@page import=”java.util.*”%> <%@page import=”java.awt.Graphics”%> <%@page import=”java.awt.image.BufferedImage”%> <%@page import=”javax.imageio.*” %> <%@ page language=”java” contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″%> <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”> <html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″> <title>Insert title here</title> </head> <body> <% int width = 60, height = 20; BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics();

g.setColor(new Color(200,200,200)); g.fillRect(0, 0, width, height);

Random rnd = new Random(); int rndNum = rnd.nextInt(9000)+1000; String randS = String.valueOf(rndNum);

session.setAttribute(“randS”,randS);

g.setColor(Color.black); g.setFont(new Font(“”,Font.PLAIN,20)); g.drawString(randS, 10, 17);

for(int i = 0;i<=100;i++){ int x = rnd.nextInt(width); int y = rnd.nextInt(height); g.drawOval(x, y, 1, 1); }

ImageIO.write(image, “JPEG”, response.getOutputStream()); out.clear(); out = pageContext.pushBody(); %> </body> </html>

但是验证码单独出现不安全,我们采用验证码和表单组合提交

<%@ page language=”java” contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″%> <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”> <html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″> <title>Insert title here</title> </head> <body> 欢迎来到本系统<br> <form action=”/AJAX1/servlets/Loginservlet” method=”post”> <br> 请您输入账号:<input type=”text” name=”account”/><br> 请您输入密码:<input type = “password” name=”password”/><br> 验证码:<input type=”text” name=”code” size=”10″> <img border=0 src =”validate.jsp”> <input type = “button” value=”登录”> </form>

</body> </html>

验证码的刷新方法很多,做方便的是点击验证码图片:

<%@ page language=”java” contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″%> <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”> <html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″> <title>Insert title here</title> </head> <body> <script type=”text/javascript”> function refresh(){ loginForm.imgValidate.src=”validate.jsp?id=”+Math.random();

}

</script> 欢迎登录本系统<br> <form name=”loginForm” action=”/AJAX1/servlets/Loginservlet” method=”post”> 请你输入账号:<input type=”text” name = “account”><br> 请你输入密码:<input type=”password” name=”password”><br> 请输入验证码:<input type=”text” name=”code” size=”10″> <img name=”imgValidate” src=”validate.jsp” οnclick=”refresh()”><br> <input type=”button” value=”登录”> </form>

</body> </html>

这样我们就可以点击验证码进行刷新了。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/204005.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月23日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档