Servlet 验证码

http://blog.csdn.net/ayhlay/article/details/13625275

1.验证码及其作用

验证码:就是将一串随机产生的数字或符号,生成一幅图片, 图片里加上一些干扰象素(防止OCR),由用户肉眼识别其中的验证码信息,输入表单提交网站验证,验证成功后才能使用某项功能。

作用:验证码一般是防止有人利用机器人自动批量注册、对特定的注册用户用特定程序暴力破解方式进行不断的登陆、灌水。因为验证码是一个混合了数字或符号的图片,人眼看起来都费劲,机器识别起来就更困难。像百度贴吧未登录发贴要输入验证码大概是防止大规模匿名回帖的发生。

开发步骤

1、开发servletpackage com.controller;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class SafeCode extends HttpServlet {public static final char[] CHARS = { '1', '2', '3', '4', '5', '6', '7','8', '9', 'k', 'A', 'Q', 'x', 'E', 'R', 'T', 'G', 'D', 'S', 'W','G', 'H', 'C', 'B', 'a', 'w', 'e', 'r', 't', 'd', 'F' };// 随机字符的字典public static Random random = new Random();// 随机数public static String getRandomString() {// 字符的缓存StringBuffer buf = new StringBuffer();for (int i = 0; i <4; i++) {// 循环 六次buf.append(CHARS[random.nextInt(CHARS.length)]);}return buf.toString();}public static Color getRandomColor() {return new Color(random.nextInt(255), random.nextInt(255),random.nextInt(255));}public static Color getReverseColor(Color c) {return new Color(255 - c.getRed(), 255 - c.getGreen(),255 - c.getBlue());}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("image/jpeg");// 设置输出的类型String randomString = getRandomString();// 得到返回的字符集request.getSession(true).setAttribute("randomString", randomString);int with = 80;int hight =36;// 生成图片的大小Color color = getRandomColor();// 用于背景色Color reverse = getReverseColor(color);// 用于前景色BufferedImage bi = new BufferedImage(with, hight,BufferedImage.TYPE_INT_RGB);// 创建一个彩色的图片Graphics2D g = bi.createGraphics();// 获取到绘图对象g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 18));// 设置字体g.setColor(color);// 设置颜色g.fillRect(0, 0, with, hight);// 绘制背景g.setColor(reverse);// 设置颜色g.drawString(randomString, 18, 25);// 绘制随机字符for (int i = 0, n = random.nextInt(18); i < n; i++) {// 画最多100个噪音点g.drawRect(random.nextInt(with), random.nextInt(hight), 1, 1);// 随机噪音点}ServletOutputStream out = response.getOutputStream();// 转换图片格式JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(bi);// 对图片进行编码out.flush();// 输出}protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {doGet(req, resp);}}

2、在jsp页面中开发

name="yzm" type="text" class="form-control x164 in">验证码

请填写图片中的字符,不区分大小写看不清楚?换张图片

}3开发servletString jl = request.getParameter("jl");String yzm = request.getParameter("yzm");String randomString = (String)request.getSession().getAttribute("randomString");

if(yzm.equalsIgnoreCase(randomString)){out.print("验证通过:"+user+"

"+jl);}else{out.print("验证非法!!!!");}

你可能感兴趣的