JSP中各种验证码的实现
- 1.数字验证码
- 2.英文+数字验证码
- 3.中文验证码
- 4.表达式验证码
在如下的登录页面中可以应用以上四种不同的验证码,分别使用如下四个代码,作为image.jsp的代码即可。
0.原始登录页面代码
<%@ 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>
<script language="javascript">
function loadimage(){
document.getElementById("randImage").src ="image.jsp?"
+ Math.random();
}
</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="image.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();">
<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>
1. 数字验证码
代码如下,文件名为image.jsp:
<%@ page
import="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对象。指定图片的长度宽度和色彩
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
OutputStream os = response.getOutputStream();
//取得Graphics对象,用来绘制图片
Graphics g = image.getGraphics();
//绘制图片背景和文字,释放Graphics对象所占用的资源
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+6, 16);
}
session.setAttribute("rand", sRand);
g.dispose();
//通过ImageIO对象的write静态方法将图片输出。
ImageIO.write(image, "JPEG", os);
//知道了图片的生成方法,剩下的问题就是如何将随机数生成到页面上了。要显示图片,只要
//将生成的图片流返回给response对象,这样用户请求的时候就可以得到图片。而一个JSP
//页面的page参数的contentType属性可以指定返回的response对象的形式,大家平时
//的JSP页面中设定的contentType是text/html,所以会被以HTML文件的形式读取和
//分析。如果设定为image/jpeg,就会被以图片的形式读取和分析
os.flush();
os.close();
os = null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
} catch (IllegalStateException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
%>
2. 英文+数字验证码
代码如下,文件名为image.jsp:
<%@ page
import="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();
}
%>
3. 中文验证码
代码如下,文件名为image.jsp:
<%@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);
}
//将认证码存入session
session.setAttribute("rand", sRand);
g.dispose();
//输出图像到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
%>
4. 表达式验证码
代码如下,文件名为image.jsp:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page
import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%>
<%@ page import="java.io.OutputStream"%>
<%
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(new Color(251, 244, 166));
//填充指定的矩形
g.fillRect(0, 0, width, height);
//设置文字的样式
g.setFont(new Font("Times New Roman", Font.BOLD, 18));
//设置文字的颜色
g.setColor(new Color(198, 39, 60));
//设置运算符号
String[] s = { "+", "-" };
String sRand = "";
//设置运算因子
int num1 = random.nextInt(100);
int num2 = random.nextInt(100);
int index = random.nextInt(2);
String rand = s[index];
//设置运算结果
int end = 0;
//得到运算表达式
sRand = num1 + rand + num2;
//绘制运算表达式
g.drawString(sRand, 13, 16);
if (rand.equals("+")) {
end = num1 + num2;
} else {
end = num1 - num2;
}
session.setAttribute("rand", end);
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();
}
%>