1.编写生成验证码的工具类:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;

public class VerifyCode
{
private int w = 70;
private int h = 35;
private Random r = new Random();

private String[] fontNames = { "宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312" };
private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
private Color bgColor = new Color(255, 255, 255);
private String text;

private Color randomColor()
{
int red = this.r.nextInt(150);
int green = this.r.nextInt(150);
int blue = this.r.nextInt(150);
return new Color(red, green, blue);
}

private Font randomFont() {
int index = this.r.nextInt(this.fontNames.length);
String fontName = this.fontNames[index];
int style = this.r.nextInt(4);
int size = this.r.nextInt(5) + 24;
return new Font(fontName, style, size);
}

private void drawLine(BufferedImage image) {
int num = 3;
Graphics2D g2 = (Graphics2D)image.getGraphics();
for (int i = 0; i < num; ++i) {
int x1 = this.r.nextInt(this.w);
int y1 = this.r.nextInt(this.h);
int x2 = this.r.nextInt(this.w);
int y2 = this.r.nextInt(this.h);
g2.setStroke(new BasicStroke(1.5F));
g2.setColor(Color.BLUE);
g2.drawLine(x1, y1, x2, y2);
}
}

private char randomChar() {
int index = this.r.nextInt(this.codes.length());
return this.codes.charAt(index);
}

private BufferedImage createImage() {
BufferedImage image = new BufferedImage(this.w, this.h, 1);
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.setColor(this.bgColor);
g2.fillRect(0, 0, this.w, this.h);
return image;
}

public BufferedImage getImage() {
BufferedImage image = createImage();
Graphics2D g2 = (Graphics2D)image.getGraphics();
StringBuilder sb = new StringBuilder();

for (int i = 0; i < 4; ++i) {
String s = randomChar();
sb.append(s);
float x = i * 1.0F * this.w / 4.0F;
g2.setFont(randomFont());
g2.setColor(randomColor());
g2.drawString(s, x, this.h - 5);
}
this.text = sb.toString();
drawLine(image);
return image;
}

public String getText() {
return this.text;
}

public static void output(BufferedImage image, OutputStream out) throws IOException
{
ImageIO.write(image, "JPEG", out);
}
}

2.调用 验证码的工具类,创建Servlet保存验证码到Session中:

import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class VerifyCodeServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
VerifyCode vc = new VerifyCode();
BufferedImage image = vc.getImage();

VerifyCode.output(image, response.getOutputStream());

request.getSession().setAttribute("vCode", vc.getText());
}
}

3.jsp中需要产生验证码:

<img src="/VerifyCodeServlet " id="yzm" />

<a href="" onclick="javascript:hyz()"></a>

function hyz(){

$("yzm").src="/VerifyCodeServlet ? a = " +new Date.getTime();    //加上时间戳解决IE浏览器的缓存

}

java生成一次性验证码的更多相关文章

  1. Java生成随机验证码

    package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  2. java生成简单验证码图片

    概要 最近项目需要用java实现输出随机验证码图片到前台,正好有机会接触下java的绘图类,完成需求后也有时间做个总结,写篇随笔记录下也希望能帮助到有同样需求的人! 需求流程图 1.生成随机数 在ja ...

  3. 如何用java生成随机验证码

     1.VerifyCode 类:   1 package com.HRuinger.enity;                          ImageIO.write(image, " ...

  4. java生成图形验证码

    效果图 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.Buf ...

  5. java生成汉字验证码

    java实现的汉字输入验证码,主要包含两个类,一个是生成验证码,一个是判断验证码输入是否正确,实现原理非常简单,将汉字和干扰线生成图片并将汉字保存到session,前台获取每次生成验证码图片并用文本框 ...

  6. java生成动态验证码

    import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;impor ...

  7. java生成随机验证码图片

    import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; i ...

  8. java生成前端验证码+验证「kaptcha」

    1.前言 kaptcha是一个非常实用的短信验证码生成工具,通过简单配置即可实现多样化的验证码. 2.引入依赖 <!--第三方验证码--><dependency>    < ...

  9. Java生成验证码原理(jsp)

     验证码的作用: 验证码是Completely Automated Public Turing test to tell Computers and Humans Apart(全自动区分计算机和人类的 ...

随机推荐

  1. dubbo系列一、dubbo背景介绍、微服务拆分

    一.背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 二.传统应用到分布式应用的演进过程 ...

  2. 读SRE Google运维解密有感(四)-聊聊问题排查

    前言 这是读“SRE Google运维解密”有感第四篇,之前的文章可访问www.addops.cn来查看.今天我们来聊聊“问题排查”这个话题,本人到目前为止还在参与一线运维的工作,遇到过很多“稀奇古怪 ...

  3. hostapd 和 wap_supplicant

    hostapd : user space daemon for access points, including, e.g., IEEE 802.1X/WPA/EAP Authenticator fo ...

  4. mongodb数据库安装及常见操作

    客户端和服务端的安装 # rpm -ivh mongo-10gen-2.4.6-mongodb_1.x86_64.rpm mongo-10gen-server-2.4.6-mongodb_1.x86_ ...

  5. 强大的Js树型控件Dtree使用详解

    http://www.lmwlove.com/ac/ID868 在学习文章之前,要学会看官方网站http://destroydrop.com/javascripts/tree.从官方页面你能知道:dt ...

  6. android-----带你一步一步优化ListView(一)

    ListView作为android中最常使用的控件,可以以条目的形式显示大量的数据,经常被用于显示最近联系人列表,对于每一个 Item,均要求adapter的getView方法返回一个View,因此L ...

  7. SpringMVC(4.1):Controller接口控制器详解(1)

    原文出处: 张开涛 4.1.Controller简介 Controller控制器,是MVC中的部分C,为什么是部分呢?因为此处的控制器主要负责功能处理部分: 1.收集.验证请求参数并绑定到命令对象: ...

  8. 测试开发之前端——No6.HTML5中的键盘事件

    键盘事件 由键盘触发的事件. 适用于所有 HTML 5 元素: 属性 值 描述 onkeydown script 当按下按键时运行脚本 onkeypress script 当按下并松开按键时运行脚本 ...

  9. CCF2014032窗口(C语言)

    问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域.窗口的边界上的点也属于该窗口.窗口之间有层次的区别,在多于一个窗口重叠的区域里,只会显示位于顶层的窗口里的 ...

  10. python接口自动化测试二十:函数写接口测试

    # coding:utf-8import requestsimport refrom bs4 import BeautifulSoup # s = requests.session() # 全局的s ...