验证码: jsp
1. 数字型:
<%@ page language="java" contentType="image/png" import="java.util.*,java.awt.*,java.awt.image.*" pageEncoding="GBK"%> <% out.clear(); //在内存中创建图象 int width = 85, height = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 获取图形上下文 Graphics g = image.getGraphics(); // 生成随机类 Random random = new Random(); // 设定背景色 g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); // 设定字体 g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 g.setColor(getRandColor(160, 200)); for (int i = 0; i < 155; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } // 取随机产生的认证码(6位数字) String sRand = ""; for (int i = 0; i < 6; i++) { String rand = String.valueOf(random.nextInt(10)); sRand += rand; // 将认证码显示到图象中 g.setColor(new Color(20 + random.nextInt(110), 20 + random .nextInt(110), 20 + random.nextInt(110))); // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 g.drawString(rand, 13 * i + 6, 16); } // 将认证码存入SESSION session.setAttribute("rand", sRand); //ActionContext.getContext().getSession().put("rand", sRand); // 图象生效 g.dispose(); out.clearBuffer();// 清空缓冲区 javax.imageio.ImageIO.write(image, "png", response.getOutputStream());// 把内存的图片编码到输出流, 参数依次为: 图片对象, 格式(png,jpg), 输出流 response.flushBuffer(); out.clear(); out = pageContext.pushBody(); %> <%! /* * 给定范围获得随机颜色 */ private Color getRandColor(int fc, int bc) { Random random = new Random(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } %>
2. 字母型
<%@ page language="java" contentType="image/png" import="java.util.*,java.awt.*,java.awt.image.*" pageEncoding="GBK"%> <%!Color getRandColor(int fc, int bc) {//给定范围获得随机颜色 Random random = new Random(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); }%> <% //将过期日期设置为一个过去时间 response.setHeader("Expires", "Sat, 6 May 1995 12:00:00 GMT"); // 设置 HTTP/1.1 no-cache 头 response.setHeader("Cache-Control", "no-store,no-cache,must-revalidate"); // 设置 IE 扩展 HTTP/1.1 no-cache headers, 用户自己添加 response.addHeader("Cache-Control", "post-check=0, pre-check=0"); // 设置标准 HTTP/1.0 no-cache header. response.setHeader("Pragma", "no-cache"); out.clear(); //response.reset();// 清空以前缓冲区 //生成随机类 Random random = new Random(); BufferedImage img = new BufferedImage(60, 20, BufferedImage.TYPE_INT_RGB);// 创建彩色缓冲图, 宽 60, 高 20 Graphics g = img.getGraphics();// 画笔对象 // 填充白色的背景 g.setColor(Color.white); g.fillRect(0, 0, 100, 20); //随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 g.setColor(getRandColor(160, 200)); for (int i = 0; i < 155; i++) { int x = random.nextInt(100); int y = random.nextInt(20); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } // 绘制蓝色文字 g.setColor(Color.blue); // 生成 4 位随机数字验证码并存入session, 然后输出到图片中 String code = ""; for(int i = 0; i < 4; i++) { Random rand = new Random(); //code += rand.nextInt(10) + ""; // 随机数字 code += (char) ( rand.nextInt(26) + 'A') ; // 随机字母 } session.setAttribute("regcode", code);// 存放值名字为 regcode //设定字体 g.setFont(new Font("Times New Roman", Font.PLAIN, 20)); g.drawString(code, 2, 16); g.dispose();// 关闭对象, 释放内存, 刷新到图形对象 out.clearBuffer();// 清空缓冲区 javax.imageio.ImageIO.write(img, "png", response.getOutputStream());// 把内存的图片编码到输出流, 参数依次为: 图片对象, 格式(png,jpg), 输出流 out.flush(); // 输出全部内容, 防止 Tomcat 后台报 Socket 错误, IE 6 下会出现问题 //out.close();// 关闭, 防止后台无法写入数据 %>
3. 调用 该jsp 输出验证码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> //载入新的验证码 function loadNewCode() { document.getElementById('regcodeImg').src = '${basePath }test.jsp?d=' + new Date().getTime(); } </script> </head> <body> <img src="${basePath }test.jsp?TTT=<%=Math.random()*100 %>" width="58" height="20" style="CURSOR: pointer" id=regcodeImg title=看不清楚?点击换一张 onclick=loadNewCode(); > </body> </html>