表单

<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. 使用angularJS接收json数据并进行数据的显示

    1.引入JS <script type="text/javascript" src="../plugins/angularjs/angular.min.js&quo ...

  2. Visual Studio 2017 软件包及教程

    下载地址:https://files.cnblogs.com/files/yungle/VisualStudio2017.rar 安装教程:https://mp.weixin.qq.com/s?__b ...

  3. git 清空所有commit记录

    说明:例如将代码提交到git仓库,将一些敏感信息提交,所以需要删除提交记录以彻底清除提交信息,以得到一个干净的仓库且代码不变 1.Checkout git checkout --orphan late ...

  4. [LeetCode] 312. Burst Balloons 打气球游戏

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  5. oracle--JOB任务

    1.创建一张测试表 -- Create table create table A8 ( a1 VARCHAR2(500) ) tablespace TT1 pctfree 10 initrans 1 ...

  6. git强制合并另一个分支

    New分支和Old分支都修改了同样的部分,有冲突,但是想在Old分之上合并New分支的内容,并且以New分支为主,就是不自己手动解决冲突,碰到冲突,直接以New分支为主. 参考 https://git ...

  7. C# HTTP系列5 HttpWebResponse.StatusCode属性

    系列目录     [已更新最新开发文章,点击查看详细] HttpWebResponse.StatusCode 属性获取响应的状态.对应 HttpStatusCode 枚举值之一. HttpStatus ...

  8. Adams宏导出

    var set var=ip integer_value=1 var set var=macro_name str="" for variable_name=the_macro o ...

  9. SpringBoot+EventBus使用教程(二)

    简介 继续上篇,本篇文章介绍如何集成spring-boot-starter-guava-eventbus使用EventBus,最新的版本好像已经不叫spring-boot-starter-guava- ...

  10. Windows 配置网络文件夹映射

    mklink /D D:\temp\pythonmxds2 \\192.168.190.186\bigdata\kaoyanmxds