Jsp⽹站登录数字+字母验证码实现image.jsp创建验证码图⽚、CreateImage.jsp输⼊验证码界⾯把值传给验证的页⾯checkImage.jsp.
我只修改了下if (rand.equalsIgnoreCase(input)) 不然验证的时候区分⼤⼩写.总的来说还⾏很实⽤!
验证码图⽚的⽂件image.jsp
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding="UTF-8" %>
<%!Color getRandColor(int fc, int bc) {//给定范围获得随机颜⾊
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + Int(bc - fc);
int g = fc + Int(bc - fc);
int b = fc + Int(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 = 80, height = 20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下⽂
Graphics g = Graphics();
//⽣成随机类
Random random = new Random();
// 设定背景⾊
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
//设定字体
g.setFont(new Font("Times New Roman", Font.BOLD, 18));
//画边框
g.setColor(new Color(0, 0, 0));
g.drawRect(0, 0, width - 1, height - 1);
// 随机产⽣155条⼲扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = Int(width);
int y = Int(height);
int xl = Int(12);
jsp创建int yl = Int(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 取随机产⽣的认证码(4位数字)
String sRand = "";
for (int i = 0; i < 4; i++) {
String rand = null;
//随机⽣成数字或者字母
if (Int(10) > 5) {
rand = String.valueOf((char)(random
.nextInt(10) + 48));
} else {
rand = String.valueOf((char)(random
.nextInt(26) + 65));
}
sRand += rand;
// 将认证码显⽰到图象中
g.setColor(new Int(80), random
.nextInt(80), Int(80)));
//调⽤函数出来的颜⾊相同,可能是因为种⼦太接近,所以只能直接⽣成
g.drawString(rand, 15 * i + 10, 16);
}
// 将认证码存⼊SESSION
session.setAttribute("rand", sRand);
// 图象⽣效
g.dispose();
// 输出图象到页⾯
ImageIO.write(image, "JPEG", OutputStream());
%>
使⽤验证码图⽚的⽂件CreateImage.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>认证码输⼊页⾯</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
</head>
<body>
<form method=post action="checkImage.jsp">
<table>
<tr>
<td align=left>系统产⽣的认证码:</td>
<td><img id="code" border=0 src="image.jsp"><a href="#" onClick="javascript:var dt=new Date();ElementById('code').src='image.jsp?dt='+dt;">看不清验证码请单击这⾥</a></td> <!--这⾥的image.jsp?dt='+dt;很重要,如果只是改为image.jsp则不会有图⽚改变的效果,因为浏览器认为SRC没有改变,所以⼀定要⽣成⼀个随时在改变的dt来实现这⼀功能-->
</tr>
<tr>
<td align=left>输⼊上⾯的认证码:</td>
<td><input type=text name=rand maxlength=4 value=""></td>
</tr>
<tr>
<td colspan=2 align=center><input type=submit value="提交检测"></td>
</tr>
</table>
</form>
</body>
</html>
验证的页⾯checkImage.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<title>认证码验证页⾯</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
</head>
<body>
<%
String rand = (Attribute("rand");
String input = Parameter("rand");
%>
系统产⽣的认证码为: <%= rand %><br>
您输⼊的认证码为: <%= input %><br>
<br>
<%
if (rand.equalsIgnoreCase(input)) {
%>
<font color=green>输⼊相同,认证成功!</font>
<%
} else {
%>
<font color=red>输⼊不同,认证失败!</font>
<%
}
%>
</body>
</html>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论