java 制作验证码并进行验证实例详解
作者:lqh 时间:2023-03-18 11:27:56
java 制作验证码并进行验证实例详解
在注册、登录的页面上经常会出现验证码,为了防止频繁的注册或登录行为。下面是我用java制作的一个验证码,供初学者参考,做完验证码之后,我们可以用ajax进行验证码验证。
功能一:验证码制作的代码,点击图片,验证码进行更换
/**
* 显示验证码图片
*/
public void showCheckCode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 调用业务逻辑
String checkCode = getCheckCode();
//将验证码字符放入session域对象中
req.getSession().setAttribute("checkCode", checkCode);
//图片宽
int width = 80;
//图片高
int height = 30;
//在内存中创建一个图片
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//获取一个画笔
Graphics g = image.getGraphics();
//设置画笔颜色,用灰色做背景
g.setColor(Color.GRAY);
//向Image中填充灰色
g.fillRect(0,0,width,height);
Random r = new Random();
//设置3条干扰线
for (int i = 0; i < 3; i++) {
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
g.drawLine(r.nextInt(80), r.nextInt(30), r.nextInt(80), r.nextInt(80));
}
//设置验证码字符串的颜色
g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));
//设置字符的大小
g.setFont(new Font("黑体",Font.BOLD,24));
//在图片中写入验证码字符串
g.drawString(checkCode,15,20);
//将Image对象以PNG格式输出给所有的客户端
ImageIO.write(image,"PNG",resp.getOutputStream());
}
/**
* 获取4位验证码中的4位随机字符串
*/
public static String getCheckCode(){
//验证码中的字符由数字和大小写字母组成
String code = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
Random r = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 4; i++) {
sb.append(code.charAt(r.nextInt(code.length())));
}
return sb.toString();
}
jsp页面
<script type="text/javascript">
function changeCodeImage(img){
img.src = "${pageContext.request.contextPath}/UserServlet?method=showCheckCode&time="+new Date().getTime();
}
</script>
<div class="form-group">
<label for="date" class="col-sm-2 control-label">验证码</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="writeCode" onkeyup="checkCodeMethod(this.value)" >
</div>
<div class="col-sm-2">
<img src="${pageContext.request.contextPath}/UserServlet?method=showCheckCode" id="checkCodeImage" title="点击换一张" onclick="changeCodeImage(this)" />
</div>
<span id="checkCodeSpan"></span>
</div>
功能二:ajax动态验证验证码
/**
* 验证验证码
*/
public void checkCode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取从页面中接收到的验证码参数
String checkCode = req.getParameter("checkCode");
//从session域对象中获取验证码
String sessionCode = (String) req.getSession().getAttribute("checkCode");
//判断验证码是否相同
if (checkCode.equalsIgnoreCase(sessionCode)) {
resp.getWriter().print(true);
}else {
resp.getWriter().print(false);
}
jsp页面
<script type="text/javascript">
function changeCodeImage(img){
img.src = "${pageContext.request.contextPath}/UserServlet?method=showCheckCode&time="+new Date().getTime();
}
function checkCodeMethod(code){
$.get("${pageContext.request.contextPath}/UserServlet?method=checkCode",
{ checkCode: code},
function(data){
if (data == 'true') {
document.getElementById("checkCodeSpan").innerHTML = "<font>验证码正确!</font>";
}else {
document.getElementById("checkCodeSpan").innerHTML = "<font>验证码错误!</font>";
}
}
);
}
</script>
<div class="form-group">
<label for="date" class="col-sm-2 control-label">验证码</label>
<div class="col-sm-3">
<input type="text" class="form-control" id="writeCode" onkeyup="checkCodeMethod(this.value)" >
</div>
<div class="col-sm-2">
<img src="${pageContext.request.contextPath}/UserServlet?method=showCheckCode" id="checkCodeImage" title="点击换一张" onclick="changeCodeImage(this)" />
</div>
<span id="checkCodeSpan"></span>
</div>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://blog.csdn.net/shuaicihai/article/details/54730590
标签:java,验证码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringMVC编程使用Controller接口实现控制器实例代码
2023-02-01 02:03:36
Android12 蓝牙适配的实现步骤
2021-08-25 08:36:34
![](https://img.aspxhome.com/file/2023/4/119094_0s.gif)
Android使用音频信息绘制动态波纹
2022-11-05 00:51:50
![](https://img.aspxhome.com/file/2023/7/138877_0s.png)
Spring实战之方法级别缓存用法示例
2022-10-20 02:00:32
Android实现状态栏和虚拟按键背景颜色的变化实例代码详解
2023-02-07 00:17:15
![](https://img.aspxhome.com/file/2023/4/123244_0s.jpg)
Mybatis如何通过接口实现sql执行原理解析
2022-11-30 11:31:26
![](https://img.aspxhome.com/file/2023/0/60480_0s.jpg)
java面向对象设计原则之里氏替换原则示例详解
2023-02-02 05:04:41
java实现动态 代理方法浅析
2023-11-28 23:33:59
Android开发之针对联系人的封装
2023-12-24 05:56:25
![](https://img.aspxhome.com/file/2023/6/137506_0s.png)
Java安全之Tomcat6 Filter内存马问题
2022-11-20 07:29:23
![](https://img.aspxhome.com/file/2023/7/61027_0s.jpg)
IDEA类存在但找不到的解决办法
2021-10-22 07:24:43
![](https://img.aspxhome.com/file/2023/0/71190_0s.png)
android开发之关闭所有的activity的方法
2022-01-16 18:21:29
基于java实现人机猜拳游戏
2021-06-09 08:50:32
![](https://img.aspxhome.com/file/2023/3/80203_0s.png)
C#图表算法之无向图
2021-08-08 22:50:20
![](https://img.aspxhome.com/file/2023/4/113854_0s.jpg)
java控制台输出图书馆管理系统
2022-06-13 01:29:29
![](https://img.aspxhome.com/file/2023/4/61284_0s.jpg)
Java抛出异常与自定义异常类应用示例
2022-10-23 01:58:52
![](https://img.aspxhome.com/file/2023/7/60327_0s.png)
C# log4net 日志输出的实现示例
2023-04-27 06:50:12
![](https://img.aspxhome.com/file/2023/8/85698_0s.png)
被kafka-client和springkafka版本坑到自闭及解决
2023-08-23 15:07:36
![](https://img.aspxhome.com/file/2023/2/58452_0s.png)
Java泛型与数据库应用实例详解
2023-08-14 09:37:15
![](https://img.aspxhome.com/file/2023/4/64014_0s.png)
C#泛型委托的用法实例分析
2021-09-16 23:29:52
![](https://img.aspxhome.com/file/2023/9/92909_0s.jpg)