验证码的实现
1 //js函数 2 function _hyz()//换一张函数JS代码 3 { 4 /* 5 1. 获取<img>元素 6 2. 给它的src指向/tools/VerifyCodeServlet 7 */ 8 var img=document.getElementById("imgVerifyCode"); 9 //需要给出一个参数,这个参数每次都不同,这样才能刷新浏览器缓存! 10 img.src = "/tools/VerifyCodeServlet?a="+ new Date().getTime(); 11 }
12 //jsp中代码 13 验证码:<input type="text" name="verifyCode"/><br> 14 <img src="/tools/VerifyCodeServlet" id="imgVerifyCode"/> 15 <a href="javascript:_hyz()">换一张</a><br> 16
//servlet中使用验证码代码 17 public String login(HttpServletRequest request, HttpServletResponse response) 18 throws ServletException, IOException { 19 /* 20 * 校验验证码 21 * 1.获取表单中的验证码 22 * 2.获取图片上的文字 23 */ 24 String verifyCode =request.getParameter("verifyCode"); 25 //VerifyCodeServlet会把真正的验证码保存到session中 26 String vcode =(String) request.getSession().getAttribute("vCode");// 27 28 System.out.println(vcode+" "+verifyCode); 29 System.out.println(verifyCode.equalsIgnoreCase(vcode));//不区分大小写比较 30 31 return null; 32 }
1 //生成验证码的过程JAVA代码,这个在网上有很多,也可以自己随自己的需求设计。 2 public class VerifyCode { 3 private int w = 70; 4 private int h = 35; 5 private Random r = new Random(); 6 // 验证码中字体、数字、字母和背景颜色 7 private String[] fontNames = {"宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312"}; 8 private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ"; 9 private Color bgColor = new Color(255, 255, 255); 10 private String text ; 11 12 private Color randomColor () { 13 int red = r.nextInt(150); 14 int green = r.nextInt(150); 15 int blue = r.nextInt(150); 16 return new Color(red, green, blue); 17 } 18 19 private Font randomFont () { 20 int index = r.nextInt(fontNames.length); 21 String fontName = fontNames[index]; 22 int style = r.nextInt(4); 23 int size = r.nextInt(5) + 24; 24 return new Font(fontName, style, size); 25 } 26 27 private void drawLine (BufferedImage image) { 28 int num = 3; 29 Graphics2D g2 = (Graphics2D)image.getGraphics(); 30 for(int i = 0; i < num; i++) { 31 int x1 = r.nextInt(w); 32 int y1 = r.nextInt(h); 33 int x2 = r.nextInt(w); 34 int y2 = r.nextInt(h); 35 g2.setStroke(new BasicStroke(1.5F)); 36 g2.setColor(Color.BLUE); 37 g2.drawLine(x1, y1, x2, y2); 38 } 39 } 40 41 private char randomChar () { 42 int index = r.nextInt(codes.length()); 43 return codes.charAt(index); 44 } 45 46 private BufferedImage createImage () { 47 BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 48 Graphics2D g2 = (Graphics2D)image.getGraphics(); 49 g2.setColor(this.bgColor); 50 g2.fillRect(0, 0, w, h); 51 return image; 52 } 53 54 public BufferedImage getImage () { 55 BufferedImage image = createImage(); 56 Graphics2D g2 = (Graphics2D)image.getGraphics(); 57 StringBuilder sb = new StringBuilder(); 58 // 向图片中画4个字符 59 for(int i = 0; i < 4; i++) { 60 String s = randomChar() + ""; 61 sb.append(s); 62 float x = i * 1.0F * w / 4; 63 g2.setFont(randomFont()); 64 g2.setColor(randomColor()); 65 g2.drawString(s, x, h-5); 66 } 67 this.text = sb.toString(); 68 drawLine(image); 69 return image; 70 } 71 72 public String getText () { 73 return text; 74 } 75 76 public static void output (BufferedImage image, OutputStream out) 77 throws IOException { 78 ImageIO.write(image, "JPEG", out); 79 } 80 public void doGet(HttpServletRequest request, HttpServletResponse response) 81 throws ServletException, IOException { 82 VerifyCode vc = new VerifyCode(); 83 BufferedImage image = vc.getImage();//获取一次性验证码图片 84 //该方法必须在getImage()方法之后来调用 85 //System.out.println(vc.getText());//获取图片上的文本 86 VerifyCode.output(image, response.getOutputStream());//把图片写到指定流中 87 // 把文本保存到session中,为LoginServlet验证做准备 88 request.getSession().setAttribute("vCode", vc.getText()); 89 }