表单

<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. 【Linux】没有网的情况下如何安装GCC

    一:GCC的概述 GCC是编译器集合 GCC是根据文件后缀分类编译的编译器集合 参考文档: https://blog.csdn.net/zl1zl2zl3/article/details/833739 ...

  2. 切换node版本

    首先将原来的安装包删了,在控制面板中删除然后在https://nodejs.org/dist/找到想要的版本号 再找到msi文件

  3. 前端css命名规范----BEM

    一.什么是BEM BEM就是块(block).元素(element).修饰符(modifier),是由Yandex团队提出的一种前端命名方法论.这种巧妙的命名方法可以使css类对其他开发者来说更加透明 ...

  4. DNS用的是TCP协议还是UDP协议

    DNS占用53号端口,同时使用TCP和UDP协议.那么DNS在什么情况下使用这两种协议? DNS在区域传输的时候使用TCP协议,其他时候使用UDP协议. DNS区域传输的时候使用TCP协议: 1.辅域 ...

  5. [LeetCode] 508. Most Frequent Subtree Sum 出现频率最高的子树和

    Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a ...

  6. [LeetCode] 130. Surrounded Regions 包围区域

    Given a 2D board containing 'X' and 'O'(the letter O), capture all regions surrounded by 'X'. A regi ...

  7. [LeetCode] 50. Pow(x, n) 求x的n次方

    Implement pow(x, n), which calculates x raised to the power n(xn). Example 1: Input: 2.00000, 10 Out ...

  8. oracle--状态查询

  9. Elasticsearch由浅入深(十一)内核原理

    倒排索引组成结构以及索引不可变原因 对于倒排索引是非常适合用来进行搜索的它的结构:(1)包含这个关键词的document list(2)包含这个关键词的所有document的数量:IDF(invers ...

  10. Python 中 如何将两个列表元素一一对应成字典

    使用python的 zip函数 和强大的集合操作可以方便的将两个list元素一一对应转换为dict,如下示例代码: names = ['n1','n2','n3'] values = [1,2,3] ...