Servlet实现数字字母验证码图片(一):
生成验证码图片主要用到了一个BufferedImage类,如下:
1、引入jquery-1.10.2.js文件,创建verifyCode.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <html> 4 <head> 5 6 <title>Java验证码</title> 7 </head> 8 9 <body> 10 <table width="100%" border="0" cellspacing="0" cellpadding="0"> 11 <tr> 12 <td width="100"></td> 13 <td width="100">验证码:</td> 14 <td width="150"><input id="veryCode" name="veryCode" type="text" 15 class="yzm" value="" onmouseout="isRightCode()" /></td> 16 <td width="135"><img id="imgObj" alt="" src="${pageContext.request.contextPath}/DrawImage" 17 onclick="changeImg()" /></td> 18 <td><a href="javascript:void(0);" onclick="changeImg()">看不清,换一张</a></td> 19 <td></td> 20 </tr> 21 <tr> 22 <td style="line-height: 38px; color: #FF0000; text-align: center;" 23 colspan="4" id="info"></td> 24 <td></td> 25 </tr> 26 </table> 27 </body> 28 <script type="text/javascript" src="js/jquery-1.10.2.js"></script> 29 <script type="text/javascript"> 30 /** 31 * 验证code-js 32 */ 33 34 // 点击换一张验证码 35 function changeImg() { 36 var imgSrc = $("#imgObj"); 37 var src = imgSrc.attr("src"); 38 //时间戳 ,为了使每次生成图片不一致,即不让浏览器读缓存,所以需要加上时间戳 39 var timestamp = (new Date()).valueOf(); 40 src =src +"?timestamp=" + timestamp;; 41 imgSrc.attr("src", src); 42 $("#info").html(""); 43 } 44 // 验证码验证 45 function isRightCode() { 46 var code = $("#veryCode").val(); 47 alert("code="+code); 48 code = "c=" + code; 49 $.ajax({ 50 type : "POST", 51 url : "resultServlet", 52 data : code, 53 success :function callback(data) {// 验证以后处理提交信息或错误信息 54 if (data.toString() == 1) { 55 $("#info").html("提示您:验证成功了!"); 56 return; 57 } else { 58 $("#info").html(data); 59 return; 60 } 61 } 62 }); 63 } 64 </script> 65 </html>
2.创建画图servlet,DrawImage.java
1 package com.utils; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics; 6 import java.awt.Graphics2D; 7 import java.awt.image.BufferedImage; 8 import java.io.IOException; 9 import java.util.Random; 10 11 import javax.imageio.ImageIO; 12 import javax.servlet.ServletException; 13 import javax.servlet.annotation.WebServlet; 14 import javax.servlet.http.HttpServlet; 15 import javax.servlet.http.HttpServletRequest; 16 import javax.servlet.http.HttpServletResponse; 17 18 /** 19 * 生成随机验证码图片 20 * 21 * @author huang 22 * 23 */ 24 @WebServlet("/DrawImage") 25 public class DrawImage extends HttpServlet { 26 private static final long serialVersionUID = 1L; 27 public static final int WIDTH = 120; 28 public static final int HEIGHT = 30; 29 30 // 在报java.lang.NoClassDefFoundError: Could not initialize class 31 // javax.imageio.ImageIO错的时候加上这个很管用。 32 static { 33 ImageIO.scanForPlugins(); 34 } 35 36 public void doGet(HttpServletRequest request, HttpServletResponse response) 37 throws ServletException, IOException { 38 this.doPost(request, response); 39 } 40 41 public void doPost(HttpServletRequest request, HttpServletResponse response) 42 throws ServletException, IOException { 43 request.setCharacterEncoding("utf-8"); 44 response.setContentType("text/html;charset=utf-8"); 45 // 创建缓存 46 BufferedImage bi = new BufferedImage(WIDTH, HEIGHT, 47 BufferedImage.TYPE_INT_RGB); 48 // 获得画布 49 Graphics g = bi.getGraphics(); 50 // 设置背影色 51 setBackGround(g); 52 // 设置边框 53 setBorder(g); 54 // 画干扰线 55 drawRandomLine(g); 56 // 写随机数 57 String random = drawRandomNum((Graphics2D) g); 58 // 将随机汉字存在session中 59 request.getSession().setAttribute("checkcode", random); 60 // 将图形写给浏览器 61 response.setContentType("image/jpeg"); 62 // 发头控制浏览器不要缓存 63 response.setDateHeader("expries", -1); 64 response.setHeader("Cache-Control", "no-cache"); 65 response.setHeader("Pragma", "no-cache"); 66 // 将图片写给浏览器 67 ImageIO.write(bi, "jpg", response.getOutputStream()); 68 } 69 70 /** 71 * 设置背景色 72 * 73 * @param g 74 */ 75 private void setBackGround(Graphics g) { 76 // 设置颜色 77 g.setColor(new Color(249, 249, 249)); 78 // 填充区域 79 g.fillRect(0, 0, WIDTH, HEIGHT); 80 81 } 82 83 /** 84 * 设置边框 85 * 86 * @param g 87 */ 88 private void setBorder(Graphics g) { 89 // 设置边框颜色 90 // RGB 91 g.setColor(new Color(210, 209, 205)); 92 // 边框区域 93 g.drawRect(1, 1, WIDTH - 2, HEIGHT - 2); 94 } 95 96 /** 97 * 画随机线条 98 * 99 * @param g 100 */ 101 private void drawRandomLine(Graphics g) { 102 // 设置颜色 103 g.setColor(Color.GRAY); 104 // 设置线条个数并画线 105 for (int i = 0; i < 5; i++) { 106 int x1 = new Random().nextInt(WIDTH); 107 int y1 = new Random().nextInt(HEIGHT); 108 int x2 = new Random().nextInt(WIDTH); 109 int y2 = new Random().nextInt(HEIGHT); 110 g.drawLine(x1, y1, x2, y2); 111 } 112 } 113 114 /** 115 * 画随机汉字 116 * 117 * @param g 118 * @return 119 */ 120 private String drawRandomNum(Graphics2D g) { 121 StringBuffer sb = new StringBuffer(); 122 // 设置字体 123 g.setFont(new Font("宋体", Font.BOLD, 20)); 124 // 准备常用汉字集 125 // String base = 126 // "u7684u4e00u4e86u662fu6211u4e0du5728u4ebau4eecu6709u6765u4ed6u8fd9u4e0au7740u4e2au5730u5230u5927u91ccu8bf4u5c31u53bbu5b50u5f97u4e5fu548cu90a3u8981u4e0bu770bu5929u65f6u8fc7u51fau5c0fu4e48u8d77u4f60u90fdu628au597du8fd8u591au6ca1u4e3au53c8u53efu5bb6u5b66u53eau4ee5u4e3bu4f1au6837u5e74u60f3u751fu540cu8001u4e2du5341u4eceu81eau9762u524du5934u9053u5b83u540eu7136u8d70u5f88u50cfu89c1u4e24u7528u5979u56fdu52a8u8fdbu6210u56deu4ec0u8fb9u4f5cu5bf9u5f00u800cu5df1u4e9bu73b0u5c71u6c11u5019u7ecfu53d1u5de5u5411u4e8bu547du7ed9u957fu6c34u51e0u4e49u4e09u58f0u4e8eu9ad8u624bu77e5u7406u773cu5fd7u70b9u5fc3u6218u4e8cu95eeu4f46u8eabu65b9u5b9eu5403u505au53ebu5f53u4f4fu542cu9769u6253u5462u771fu5168u624du56dbu5df2u6240u654cu4e4bu6700u5149u4ea7u60c5u8defu5206u603bu6761u767du8bddu4e1cu5e2du6b21u4eb2u5982u88abu82b1u53e3u653eu513fu5e38u6c14u4e94u7b2cu4f7fu5199u519bu5427u6587u8fd0u518du679cu600eu5b9au8bb8u5febu660eu884cu56e0u522bu98deu5916u6811u7269u6d3bu90e8u95e8u65e0u5f80u8239u671bu65b0u5e26u961fu5148u529bu5b8cu5374u7ad9u4ee3u5458u673au66f4u4e5du60a8u6bcfu98ceu7ea7u8ddfu7b11u554au5b69u4e07u5c11u76f4u610fu591cu6bd4u9636u8fdeu8f66u91cdu4fbfu6597u9a6cu54eau5316u592au6307u53d8u793eu4f3cu58ebu8005u5e72u77f3u6ee1u65e5u51b3u767eu539fu62ffu7fa4u7a76u5404u516du672cu601du89e3u7acbu6cb3u6751u516bu96beu65e9u8bbau5417u6839u5171u8ba9u76f8u7814u4ecau5176u4e66u5750u63a5u5e94u5173u4fe1u89c9u6b65u53cdu5904u8bb0u5c06u5343u627eu4e89u9886u6216u5e08u7ed3u5757u8dd1u8c01u8349u8d8au5b57u52a0u811au7d27u7231u7b49u4e60u9635u6015u6708u9752u534au706bu6cd5u9898u5efau8d76u4f4du5531u6d77u4e03u5973u4efbu4ef6u611fu51c6u5f20u56e2u5c4bu79bbu8272u8138u7247u79d1u5012u775bu5229u4e16u521au4e14u7531u9001u5207u661fu5bfcu665au8868u591fu6574u8ba4u54cdu96eau6d41u672au573au8be5u5e76u5e95u6df1u523bu5e73u4f1fu5fd9u63d0u786eu8fd1u4eaeu8f7bu8bb2u519cu53e4u9ed1u544au754cu62c9u540du5440u571fu6e05u9633u7167u529eu53f2u6539u5386u8f6cu753bu9020u5634u6b64u6cbbu5317u5fc5u670du96e8u7a7fu5185u8bc6u9a8cu4f20u4e1au83dcu722cu7761u5174u5f62u91cfu54b1u89c2u82e6u4f53u4f17u901au51b2u5408u7834u53cbu5ea6u672fu996du516cu65c1u623fu6781u5357u67aau8bfbu6c99u5c81u7ebfu91ceu575au7a7au6536u7b97u81f3u653fu57ceu52b3u843du94b1u7279u56f4u5f1fu80dcu6559u70edu5c55u5305u6b4cu7c7bu6e10u5f3au6570u4e61u547cu6027u97f3u7b54u54e5u9645u65e7u795eu5ea7u7ae0u5e2eu5566u53d7u7cfbu4ee4u8df3u975eu4f55u725bu53d6u5165u5cb8u6562u6389u5ffdu79cdu88c5u9876u6025u6797u505cu606fu53e5u533au8863u822cu62a5u53f6u538bu6162u53d4u80ccu7ec6"; 127 // String base = 128 // "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZu7684u4e00u4e86u662fu6211u4e0du5728u4ebau4eecu6709u6765u4ed6u8fd9u4e0au7740u4e2au5730u5230u5927u91ccu8bf4u5c31u53bbu5b50u5f97u4e5fu548cu90a3u8981u4e0bu770bu5929u65f6u8fc7u51fau5c0fu4e48u8d77u4f60u90fdu628au597du8fd8u591au6ca1u4e3au53c8u53efu5bb6u5b66u53eau4ee5u4e3bu4f1au6837u5e74u60f3u751fu540cu8001u4e2du5341u4eceu81eau9762u524du5934u9053u5b83u540eu7136u8d70u5f88u50cfu89c1u4e24u7528u5979u56fdu52a8u8fdbu6210u56deu4ec0u8fb9u4f5cu5bf9u5f00u800cu5df1u4e9bu73b0u5c71u6c11u5019u7ecfu53d1u5de5u5411u4e8bu547du7ed9u957fu6c34u51e0u4e49u4e09u58f0u4e8eu9ad8u624bu77e5u7406u773cu5fd7u70b9u5fc3u6218u4e8cu95eeu4f46u8eabu65b9u5b9eu5403u505au53ebu5f53u4f4fu542cu9769u6253u5462u771fu5168u624du56dbu5df2u6240u654cu4e4bu6700u5149u4ea7u60c5u8defu5206u603bu6761u767du8bddu4e1cu5e2du6b21u4eb2u5982u88abu82b1u53e3u653eu513fu5e38u6c14u4e94u7b2cu4f7fu5199u519bu5427u6587u8fd0u518du679cu600eu5b9au8bb8u5febu660eu884cu56e0u522bu98deu5916u6811u7269u6d3bu90e8u95e8u65e0u5f80u8239u671bu65b0u5e26u961fu5148u529bu5b8cu5374u7ad9u4ee3u5458u673au66f4u4e5du60a8u6bcfu98ceu7ea7u8ddfu7b11u554au5b69u4e07u5c11u76f4u610fu591cu6bd4u9636u8fdeu8f66u91cdu4fbfu6597u9a6cu54eau5316u592au6307u53d8u793eu4f3cu58ebu8005u5e72u77f3u6ee1u65e5u51b3u767eu539fu62ffu7fa4u7a76u5404u516du672cu601du89e3u7acbu6cb3u6751u516bu96beu65e9u8bbau5417u6839u5171u8ba9u76f8u7814u4ecau5176u4e66u5750u63a5u5e94u5173u4fe1u89c9u6b65u53cdu5904u8bb0u5c06u5343u627eu4e89u9886u6216u5e08u7ed3u5757u8dd1u8c01u8349u8d8au5b57u52a0u811au7d27u7231u7b49u4e60u9635u6015u6708u9752u534au706bu6cd5u9898u5efau8d76u4f4du5531u6d77u4e03u5973u4efbu4ef6u611fu51c6u5f20u56e2u5c4bu79bbu8272u8138u7247u79d1u5012u775bu5229u4e16u521au4e14u7531u9001u5207u661fu5bfcu665au8868u591fu6574u8ba4u54cdu96eau6d41u672au573au8be5u5e76u5e95u6df1u523bu5e73u4f1fu5fd9u63d0u786eu8fd1u4eaeu8f7bu8bb2u519cu53e4u9ed1u544au754cu62c9u540du5440u571fu6e05u9633u7167u529eu53f2u6539u5386u8f6cu753bu9020u5634u6b64u6cbbu5317u5fc5u670du96e8u7a7fu5185u8bc6u9a8cu4f20u4e1au83dcu722cu7761u5174u5f62u91cfu54b1u89c2u82e6u4f53u4f17u901au51b2u5408u7834u53cbu5ea6u672fu996du516cu65c1u623fu6781u5357u67aau8bfbu6c99u5c81u7ebfu91ceu575au7a7au6536u7b97u81f3u653fu57ceu52b3u843du94b1u7279u56f4u5f1fu80dcu6559u70edu5c55u5305u6b4cu7c7bu6e10u5f3au6570u4e61u547cu6027u97f3u7b54u54e5u9645u65e7u795eu5ea7u7ae0u5e2eu5566u53d7u7cfbu4ee4u8df3u975eu4f55u725bu53d6u5165u5cb8u6562u6389u5ffdu79cdu88c5u9876u6025u6797u505cu606fu53e5u533au8863u822cu62a5u53f6u538bu6162u53d4u80ccu7ec6"; 129 // 准备常用字符集 130 String base = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 131 int x = 5; 132 // 控制字数 133 for (int i = 0; i < 4; i++) { 134 // 生成0-255的随机数 135 int R = new Random().nextInt(256); 136 int G = new Random().nextInt(256); 137 int B = new Random().nextInt(256); 138 // 设置每个随机数的颜色 139 g.setColor(new Color(R, G, B)); 140 // 设置字体旋转角度 141 int degree = new Random().nextInt() % 30; 142 // 截取汉字 143 String ch = base.charAt(new Random().nextInt(base.length())) + ""; 144 sb.append(ch); 145 // 正向角度 146 g.rotate(degree * Math.PI / 180, x, 20); 147 g.drawString(ch, x, 20); 148 // 反向角度 149 g.rotate(-degree * Math.PI / 180, x, 20); 150 x += 30; 151 } 152 return sb.toString(); 153 } 154 }
3.创建验证结果servlet,ResultServlet.java
1 package com.utils; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 /** 14 * 结果验证Servlet 15 * @author huang 16 * 17 */ 18 @WebServlet("/resultServlet") 19 public class ResultServlet extends HttpServlet { 20 21 /** 22 * 23 */ 24 private static final long serialVersionUID = 1L; 25 26 public void doGet(HttpServletRequest request, HttpServletResponse response) 27 throws ServletException, IOException { 28 29 doPost(request, response); 30 } 31 32 /** 33 * 后台验证输入 34 */ 35 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 36 response.setContentType("text/html;charset=utf-8"); 37 String validateC = (String) request.getSession().getAttribute("checkcode"); 38 //从前台传来的用户输入的验证码 39 String veryCode = request.getParameter("c"); 40 System.out.println("veryCode:"+veryCode); 41 PrintWriter out = response.getWriter(); 42 if (veryCode == null || "".equals(veryCode)) { 43 out.println("请输入验证码!"); 44 } else { 45 // if (validateC.equals(veryCode)) { 46 // 忽略大小写处理: 47 if (validateC.equalsIgnoreCase(veryCode)) { 48 out.println(1); 49 } else { 50 out.println("验证码输入错误!"); 51 } 52 } 53 out.flush(); 54 out.close(); 55 } 56 57 }
4、结果
项目下载:下载工程