读书人

前台惯用的验证码的生成方式

发布时间: 2013-04-20 19:43:01 作者: rapoo

前台常用的验证码的生成方式
今天整理一下做Web前台常用的验证码的示例,用于登陆提交,以预防程序自动注册提交,可根据实际业务,利用框架技术,异步提交方式处理;主要生成方法类同
处理的servlet如下:

/** * @author laker */public class PicServlet extends HttpServlet {private static final long serialVersionUID = 1L;/** * 图片中出现的随机字符数组 */private static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8','9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z' };/** * 产生随机字符对象实例 */public static Random random = new Random();private String randomString = null;/** * 获取字符窜方法 *  * @return 返回随机字符窜 */public static String getRandomString() {StringBuffer sBuffer = new StringBuffer();for (int i = 0; i < 6; i++) {sBuffer.append(CHARS[random.nextInt(CHARS.length)]);}return sBuffer.toString();}/** * 获取随机的着色方法 *  * @return 返回随机的背景着色 */public static Color getRandomColor() {return new Color(random.nextInt(255), random.nextInt(255),random.nextInt(255));}/** * 获取对应的前景着色方法,以例识别 *  * @return 获取对应的前景着色 */public static Color getReverseColor(Color c) {return new Color(255 - c.getRed(), 255 - c.getGreen(),255 - c.getBlue());}/** * get方式调用 */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doPost(request, response);}/** * post方式调用 */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {System.out.println("更换图片" + request.getParameter("inputString"));// 响应设置为图片响应类型response.setContentType("image/jpeg");if (null != request.getParameter("inputString")&& request.getParameter("inputString").toLowerCase().equals(randomString.toLowerCase())) {System.out.println("验证码输入确");//跳转到成功页面String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";response.sendRedirect(basePath + "success.jsp");return;} else {System.out.println("验证码输入有误");// 随机字符窜实例randomString = getRandomString();}// 获取当前session实例,没胡即创建之request.getSession(true).setAttribute("randomString", randomString);// 定义图片长高int width = 100;int height = 30;// 获取随机着色Color color = getRandomColor();// 获取对应着色Color reverse = getReverseColor(color);// 创建图片BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_BGR);Graphics2D gf = bi.createGraphics();// 图片样式gf.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 16));// 着色gf.setColor(color);// 填充gf.fillRect(0, 0, width, height);// 前景色gf.setColor(reverse);// 绘制图片gf.drawString(randomString, 18, 20);// 按高度随机绘制.出现的位置for (int i = 0, n = random.nextInt(100); i < n; i++) {gf.drawRect(random.nextInt(width), random.nextInt(width), 1, 1);}// 输出流ServletOutputStream out = response.getOutputStream();// 以图片格试编码JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(bi);// 刷新进行显示out.flush();}}


表单提交项:
<form id="frm" action="servlet/PicServlet"><input type="text" name="inputString"><img alt="前台惯用的验证码的生成方式" src="servlet/PicServlet" id="picture"onload="btn.disabled=false;"><input type="button" value="换一个" onclick="reloadImage()" id="btn"><br><input type="submit" value="提交"></form>


JS:
function reloadImage() {document.getElementById('btn').disabled = true;document.getElementById('picture').src = 'servlet/PicServlet?ts=' + new Date().getTime();}

读书人网 >行业软件

热点推荐