表单

<form action="loginServlet" method="post">
请输入验证码:<input type="text" name="code" />
<img src="getCodeServlet" /><br />
<button type="submit">提交</button>
</form>

载入页面时,会自动请求getCodeServlet,获取图片(验证码)。

getCodeServlet,产生验证码

 @WebServlet("/getCodeServlet")
public class GetCodeServlet extends HttpServlet {
//验证码的宽、高
private static int WIDTH=80;
private static int HEIGHT=25; //绘制背景
private void drawBg(Graphics g){
//rgb
g.setColor(new Color(128, 128, 128));
//绘制矩形。x,y,wigth,height
g.fillRect(0,0,WIDTH,HEIGHT);
//随机绘制100个干扰点
Random random=new Random();
for (int i=0;i<100;i++){
//产生(0,1)上的小数,*WIDTH|HEIGHT,再取整也行
int x=random.nextInt(WIDTH);
int y=random.nextInt(HEIGHT);
g.drawOval(x,y,1,1); //干扰点的颜色也可以随机,随机产生red,green,blue即可
//g.setColor(new Color(red,green,blue));
}
} //绘制验证码
private void drawCode(Graphics g,char[] code){
g.setColor(Color.BLACK);
//字体、样式(多个时竖线分隔)、字号
g.setFont(new Font("serif",Font.ITALIC|Font.BOLD,18));
//在不同位置绘制验证码字符,参数:要绘制的String、横、纵坐标。+""是为了char转String。
g.drawString(code[0]+"",1,17);
g.drawString(code[1]+"",16,15);
g.drawString(code[2]+"",31,18);
g.drawString(code[3]+"",46,16);
} //随机产生4位验证码
private char[] getCode(){
String chars="0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
char[] code=new char[4];
Random random=new Random();
for (int i=0;i<4;i++){
//[0,62)
int index= random.nextInt(62);
code[i]=chars.charAt(index);
}
return code;
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
ServletOutputStream sos = response.getOutputStream();
response.setContentType("image/jpeg"); //设置浏览器不缓存此图片
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0); //创建内存图片
BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, TYPE_INT_RGB);
Graphics g= bufferedImage.getGraphics();
char[] code=getCode();
//将验证码放到session域中。session对象要在提交响应之前获得
session.setAttribute("code",new String(code));
drawBg(g);
drawCode(g,code);
g.dispose(); //将图片输出到浏览器
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage,"JPEG",baos);
baos.writeTo(sos);
baos.close();
sos.close();
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
doPost(request,response);
}
}

loginServlet,处理表单

 @WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
HttpSession session = request.getSession();
String trueCode= (String) session.getAttribute("code");
String code=request.getParameter("code"); if (code.equals(trueCode)){
response.getWriter().write("验证码正确");
}
else {
response.getWriter().write("验证码错误");
}
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}

上面的处理方式要区分验证码的大小写。

不区分大小写:

//先转换为全大写|全小写,再判断
trueCode=trueCode.toLowerCase();
code=code.toLowerCase(); //trueCode=trueCode.toUpperCase();
//code=trueCode.toUpperCase();

JavaWeb 使用Session实现一次性验证码的更多相关文章

  1. 使用session实现一次性验证码

    在登录页面和各种页面,会看到有验证码输入,这样做的目的是为了防止密码猜测工具破解密码,保护了用户密码安全,验证码只能使用一次,这样就给密码猜测工具带来了很大的困难,基本上阻断了密码猜测工具的使用. 可 ...

  2. 简单的Session案例 —— 一次性验证码

    一次性验证码的主要目的就是为了限制人们利用工具软件来暴力猜测密码,其原理与利用Session防止表单重复提交的原理基本一样,只是将表单标识号变成了验证码的形式,并且要求用户将提示的验证码手工填写进一个 ...

  3. Java Web(四) 一次性验证码的代码实现

    其实实现代码的逻辑非常简单,真的超级超级简单. 1.在登录页面上login.jsp将验证码图片使用标签<img src="xxx">将绘制验证码图片的url给它 2.在 ...

  4. JavaWeb开发之普通图片验证码生成技术与算术表达式验证码生成技术

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6134649.html    另:算术验证码生成的JSP.Servlet实现均已移植github:https:/ ...

  5. javaweb(九)—— 通过Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  6. javaweb基础(9)_Servlet生成验证码图片

    一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下:

  7. Session中短信验证码设置有效时间

    Session中短信验证码设置有效时间 package com.mozq.boot.kuayu01.controller; import org.springframework.web.bind.an ...

  8. web开发(四) 一次性验证码的代码实现

    在网上看见一篇不错的文章,写的详细. 以下内容引用那篇博文.转载于<http://www.cnblogs.com/whgk/p/6426072.html>,在此仅供学习参考之用. 其实实现 ...

  9. javaweb笔记09—(session会话及验证码问题)

    第一部分+++++++++++1.session会话 定义:session会话——对某个web应用程序的一次整体访问的过程. 由来:无连接的http协议是无状态的,不能保存每个客户端私有信息 a用户和 ...

随机推荐

  1. django之分页器、多对多关系、form校验组件

    批量插入数据 bulk_create # 1.往书籍表中插入数据 1000 # for i in range(1000): # 这种插入方式 效率极低 # models.Book.objects.cr ...

  2. Redis笔记1-Redis介绍及数据类型使用场景

    Redis介绍:C语言开发.单线程操作.高性能.键值对.可持久化的数据库.Redis采用redisObject结构来统一五种数据类型,redisObject是五种类型的父类,可以在函数间传递时隐藏具体 ...

  3. MySQL中连接超时自动断开的解决方案

    前言: MySQL数据库一般默认的连接超时时间为3600s(1小时),但是在进行大规模的线程事务操作时,一个连接会一直等待执行,这时候如果数据库的超时时间设置的过短,就可能会出现Mysql数据连接自动 ...

  4. 4.Vue双向绑定

    1.什么是双向数据绑定 Vue.js 是一个 MVVM 框架,即数据双向绑定,即当数据发生变化的时候,视图也就发生变化,当视图发生变化的时候,数据也会跟着同步变化.这也算是 Vue.js 的精髓之处了 ...

  5. 【2019.7.16 NOIP模拟赛 T2】折叠(fold)(动态规划)

    暴力\(DP\) 考虑暴力\(DP\),我们设\(f_{i,j}\)表示当前覆盖长度为\(i\),上一次折叠长度为\(j\)的方案数. 转移时需要再枚举这次的折叠长度\(k\)(\(k\ge j\)) ...

  6. [LeetCode] 897. Increasing Order Search Tree 递增顺序查找树

    Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...

  7. asp.net core ModelState 模型状态验证扩展类

    using DMS.Common.BaseResult; using Microsoft.AspNetCore.Mvc.ModelBinding; using System; using System ...

  8. Loj #3044. 「ZJOI2019」Minimax 搜索

    Loj #3044. 「ZJOI2019」Minimax 搜索 题目描述 九条可怜是一个喜欢玩游戏的女孩子.为了增强自己的游戏水平,她想要用理论的武器武装自己.这道题和著名的 Minimax 搜索有关 ...

  9. 使用numpy处理数组

    def fun_ndarray(): a = [[1,2,7], [-6,-2,-3], [-4,-8,-55] ] b = np.array(a) b = np.abs(b)#取数组的绝对值 pri ...

  10. VisualHull && association4D 观摩记录

    简单记录一下自己对VisualHull 和 association4D 两个程序的理解,没有别的意思(当然是真的 由于进度还很慢,暂时只是简单记录一下发现的点和踩过的坑,做完再把资料汇总. Visua ...