一、数字验证码:
1.编写产生验证码的页面,image.jsp
<%@ pageimport="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%><%@ page import="java.io.OutputStream"%><%!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);}%><%try {response.setHeader("Pragma", "No-cache");response.setHeader("Cache-Control", "no-cache");response.setDateHeader("Expires", 0);int width = 60, height = 20;BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);OutputStream os = response.getOutputStream();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));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);}String sRand = "";for (int i = 0; i < 4; 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 +4, 16);}session.setAttribute("rand", sRand);g.dispose();ImageIO.write(image, "JPEG", os);os.flush();os.close();os = null;response.flushBuffer();out.clear();out = pageContext.pushBody();} catch (IllegalStateException e) {System.out.println(e.getMessage());e.printStackTrace();}%>
2.编写放置验证码的页面,使用javascript加载验证码图片:login.jsp
<%@ page contentType="text/html;charset=gb2312"%><%@ page language="java" import="java.sql.*" errorPage=""%><html<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>用户登录</title><mce:script language="javascript"><!--//加载验证码图片function loadimage() {document.getElementById("randImage").src = "image.jsp?" + Math.random();}// --></mce:script></head><body><table width="256" border="0" cellpadding="0" cellspacing="0"><!--DWLayoutTable--><form action="validate.jsp" method="post" name="loginForm"><tr><td width="118" height="22" valign="middle" align="center"><input type="text" name="rand" size="15"></td><td width="138" valign="middle" align="center"><img alt="code..." name="randImage" id="randImage"src="numimg.jsp" width="60" height="20" border="1"align="absmiddle"></td></tr><tr><td height="36" colspan="2" align="center" valign="middle"><a href="javascript:loadimage();" mce_href="javascript:loadimage();"><font class=pt95>看不清点我</font></a></td></tr><tr><td height="36" colspan="2" align="center" valign="middle"><input type="submit" name="login" value="提交"></td></tr></form></table></body></html>
注意:src="numing.jsp"
<img alt="code..." name="randImage" id="randImage" src="numimg.jsp"
width="60" height="20" border="1" align="absmiddle">
3、测试效果:
二、英文与数字验证码
1、编写产生验证码的页面:ening.jsp
<%@ pageimport="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%><%@ page import="java.io.OutputStream"%><%!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);}%><%try {response.setHeader("Pragma", "No-cache");response.setHeader("Cache-Control", "no-cache");response.setDateHeader("Expires", 0);int width = 110, height = 20;BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);OutputStream os = response.getOutputStream();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));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);}String[] s = { "A", "B", "C", "D", "E", "F", "G", "H", "I","J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U","V", "W", "X", "Y", "Z" };String sRand = "";for (int i = 0; i < 4; i++) {String rand = "";if (random.nextBoolean()) {rand = String.valueOf(random.nextInt(10));} else {int index = random.nextInt(25);rand = s[index];}sRand += rand;g.setColor(new Color(20 + random.nextInt(10), 20 + random.nextInt(110), 20 + random.nextInt(110)));g.drawString(rand, 17 * i + 6, 16);}session.setAttribute("rand", sRand);g.dispose();ImageIO.write(image, "JPEG", os);os.flush();os.close();os = null;response.flushBuffer();out.clear();out = pageContext.pushBody();} catch (IllegalStateException e) {System.out.println(e.getMessage());e.printStackTrace();}%>
2.编写放置验证码的页面,使用javascript加载验证码图片:login.jsp
<%@ page contentType="text/html;charset=gb2312"%><%@ page language="java" import="java.sql.*" errorPage=""%><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>用户登录</title><mce:script language="javascript"><!--function loadimage(){ document.getElementById("randImage").src = "enimg.jsp?"+Math.random(); } // --></mce:script></head><body><table width="256" border="0" cellpadding="0" cellspacing="0"><!--DWLayoutTable--><form action="validate.jsp" method="post" name="loginForm"><tr><td width="118" height="22" valign="middle" align="center"><input type="text" name="rand" size="15"></td><td width="138" valign="middle" align="center"><img alt="code..." name="randImage" id="randImage" src="enimg.jsp" mce_src="enimg.jsp"width="60" height="20" border="1" align="absmiddle"></td></tr><tr><td height="36" colspan="2" align="center" valign="middle"><a href="javascript:loadimage();" mce_href="javascript:loadimage();"><font class=pt95>看不清点我</font></a></td></tr><tr><td height="36" colspan="2" align="center" valign="middle"><input type="submit" name="login" value="提交"></td></tr></form></table></body></html>
注意:src="ening.jsp"
<img alt="code..." name="randImage" id="randImage" src="enimg.jsp"
width="60" height="20" border="1" align="absmiddle">
3.测试效果:
三、JSP中文版验证码:
1.编写产生验证码的页面:cimg
<%@page contentType="image/jpeg" pageEncoding="UTF-8"import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%><%!//生成随机颜色Color getRandColor(Random random, int fc, int bc) {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("Pragma", "No-cache");response.setHeader("Cache-Control", "no-cache");response.setDateHeader("Expires", 0);// 设置图片的长宽int width = 106, height = 30;//设置备选汉字,剔除一些不雅的汉字String base = "/u6211/u662f/u5f90/u5f20/u660e/u71d5/u534e/u541b/u5c24/u6731/u7ea2/u7231/u4f20/u534e/u6768/u5510/u536b/u5b8f/u950b/u5f20/u4e2d/u56fd/u5317/u4eac/u4e0a/u6d77/u5929/u6d25/u6e56/u5317";//备选汉字的长度int length = base.length();//创建内存图像BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);// 获取图形上下文Graphics g = image.getGraphics();//创建随机类的实例Random random = new Random();// 设定图像背景色(因为是做背景,所以偏淡)g.setColor(getRandColor(random, 200, 250));g.fillRect(0, 0, width, height);//备选字体String[] fontTypes = { "u5b8bu4f53", "u65b0u5b8bu4f53","u9ed1u4f53", "u6977u4f53", "u96b6u4e66" };int fontTypesLength = fontTypes.length;//在图片背景上增加噪点g.setColor(getRandColor(random, 160, 200));g.setFont(new Font("Times New Roman", Font.PLAIN, 14));for (int i = 0; i < 6; i++) {g.drawString("*********************************************",0, 5 * (i + 2));}//取随机产生的认证码(6个汉字)//保存生成的汉字字符串String sRand = "";for (int i = 0; i < 3; i++) {int start = random.nextInt(length);String rand = base.substring(start, start + 1);sRand += rand;//设置字体的颜色g.setColor(getRandColor(random, 10, 150));//设置字体g.setFont(new Font(fontTypes[random.nextInt(fontTypesLength)],Font.BOLD, 18 + random.nextInt(6)));//将此汉字画到图片上g.drawString(rand, 24 * i + 10 + random.nextInt(8), 24);}//将认证码存入sessionsession.setAttribute("rand", sRand);g.dispose();//输出图象到页面out.clear();out = pageContext.pushBody();ImageIO.write(image, "JPEG", response.getOutputStream());%>
2.编写放置验证码的页面,使用javascript加载验证码图片:login.jsp
<%@ page contentType="text/html;charset=gb2312"%><%@ page language="java" import="java.sql.*" errorPage=""%><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>用户登录</title><mce:script language="javascript"><!--function loadimage(){ document.getElementById("randImage").src = "cimg.jsp?"+Math.random(); } // --></mce:script></head><body><table width="256" border="0" cellpadding="0" cellspacing="0"><!--DWLayoutTable--><form action="validate.jsp" method="post" name="loginForm"><tr><td width="118" height="22" valign="middle" align="center"><input type="text" name="rand" size="15"></td><td width="138" valign="middle" align="center"><img alt="code..." name="randImage" id="randImage" src="cimg.jsp" mce_src="cimg.jsp"width="60" height="20" border="1" align="absmiddle"></td></tr><tr><td height="36" colspan="2" align="center" valign="middle"><a href="javascript:loadimage();" mce_href="javascript:loadimage();"><font class=pt95>看不清点我</font></a></td></tr><tr><td height="36" colspan="2" align="center" valign="middle"><input type="submit" name="login" value="提交"></td></tr></form></table></body></html>
注意:src="cimg.jsp"
<img alt="code..." name="randImage" id="randImage" src="cimg.jsp"
width="60" height="20" border="1" align="absmiddle">
3.测试效果:
四、JSP版表达式验证码
1.编写产生验证码的界面:
<%@ page contentType="text/html;charset=gb2312"%><%@ page language="java" import="java.sql.*" errorPage=""%><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>用户登录</title><mce:script language="javascript"><!--function loadimage(){ document.getElementById("randImage").src = "numimg.jsp?"+Math.random(); } // --></mce:script></head><body><table width="256" border="0" cellpadding="0" cellspacing="0"><!--DWLayoutTable--><form action="validate.jsp" method="post" name="loginForm"><tr><td width="118" height="22" valign="middle" align="center"><input type="text" name="rand" size="15"></td><td width="138" valign="middle" align="center"><img alt="code..." name="randImage" id="randImage" src="numimg.jsp" mce_src="numimg.jsp"width="60" height="20" border="1" align="absmiddle"></td></tr><tr><td height="36" colspan="2" align="center" valign="middle"><a href="javascript:loadimage();" mce_href="javascript:loadimage();"><font class=pt95>看不清点我</font></a></td></tr><tr><td height="36" colspan="2" align="center" valign="middle"><input type="submit" name="login" value="提交"></td></tr></form></table></body></html>
2.编写放置验证码的页面,使用javascript加载验证码图片:login.jsp
<%@ page contentType="text/html;charset=gb2312"%><%@ page language="java" import="java.sql.*" errorPage=""%><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>用户登录</title><mce:script language="javascript"><!--function loadimage(){ document.getElementById("randImage").src = "numimg.jsp?"+Math.random(); } // --></mce:script></head><body><table width="256" border="0" cellpadding="0" cellspacing="0"><!--DWLayoutTable--><form action="validate.jsp" method="post" name="loginForm"><tr><td width="118" height="22" valign="middle" align="center"><input type="text" name="rand" size="15"></td><td width="138" valign="middle" align="center"><img alt="code..." name="randImage" id="randImage" src="numimg.jsp" mce_src="numimg.jsp"width="60" height="20" border="1" align="absmiddle"></td></tr><tr><td height="36" colspan="2" align="center" valign="middle"><a href="javascript:loadimage();" mce_href="javascript:loadimage();"><font class=pt95>看不清点我</font></a></td></tr><tr><td height="36" colspan="2" align="center" valign="middle"><input type="submit" name="login" value="提交"></td></tr></form></table></body></html>
注意:src="numimg.jsp"
<img alt="code..." name="randImage" id="randImage" src="numimg.jsp"
width="60" height="20" border="1" align="absmiddle">
3.测试效果: