JSP生成彩色验证码
来源:互联网 发布:北京联通网络测速 编辑:程序博客网 时间:2024/04/30 19:28
程序代码ValidateCode.jsp
<%@ page language="java" contentType="image/jpeg" pageEncoding="utf-8"%>
<%@ page import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%
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>生成彩色验证码</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">
</head>
<%! //给定范围获得随机颜色
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);
}
%>
<% //设置页面不缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
//在内存中创建图象
int width=69, height=26;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获取图形上下文
Graphics g = image.getGraphics();
//生成随机类
Random random = new Random();
// 设定背景色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
//设定字体
g.setFont(new Font("隶书",Font.ITALIC,27));
//画边框
g.setColor(Color.BLUE);
g.drawRect(0,0,width-1,height-1);
//随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(0,50));
for (int i=0;i<15;i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(10);
int yl = random.nextInt(10);
g.drawLine(x,y,x+xl,y+yl);
}
//取随机产生的认证码(4位数字)
String sRand = "";
char[] rand={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9'};
for (int i=0;i<4;i++) {
String s = String.valueOf(rand[random.nextInt(60)]);
// 将认证码显示到图象中
g.setColor(new Color(20 + random.nextInt(100),60 + random.nextInt(140),80 + random.nextInt(160)));
//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(s,15*i+6,21);
sRand += s;
}
//将认证码存入SESSION
session.setAttribute("rand",sRand);
//图象生效
g.dispose();
//输出图象到页面
out.clear();
out = pageContext.pushBody();
ImageIO.write(image, "JPEG", response.getOutputStream());
%>
</html>
使用验证码图片的文件a.jsp
<%@ page contentType="text/html;charset=gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<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">
<html>
<head>
<title>认证码输入页面</title>
</head>
<body> <form method=post action="check.jsp"> <table> <tr> <td align=left>系统产生的认证码:</td> <td><img border=0 src="ValidateCode.jsp"></td> </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>
验证的页面check.jsp
<%@ page language="java" import="java.sql.*" errorPage="" %>
<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">
<html>
<head>
<title>认证码验证页面</title>
</head>
<body>
<%
String rand = (String)session.getAttribute("rand");
String input = request.getParameter("rand");
%>
系统产生的认证码为:<%= rand %><br>
您输入的认证码为:<%= input %><br><br>
<%if(rand.equals(input)){ %>
<font color=green>输入相同,认证成功!</font>
<%}else{%>
<font color=red>输入不同,认证失败!</font>
<%}%>
</body>
</html>
- JSP生成彩色验证码
- JSP生成彩色验证码
- JSP生成彩色验证码
- JSP生成彩色验证码
- JSP生成彩色验证码
- jsp生成彩色验证码
- JSP生成彩色验证码
- JSP生成彩色验证码
- JSP生成彩色验证码
- JSP生成的彩色验证码
- 使用jsp生成彩色汉字验证码
- 使用jsp生成彩色汉字验证码
- 使用jsp生成彩色汉字验证码
- 使用jsp生成彩色汉字验证码
- 使用jsp生成彩色汉字验证码
- 使用jsp生成彩色汉字验证码
- 用JSP来生成彩色验证码
- 使用JSP生成彩色汉字验证码
- GCC基础
- SQL数据库置疑修复
- 常见JAVA基础面试题3
- 年假計算方式
- tomcat启动时的两个批处理脚本详解
- JSP生成彩色验证码
- Log4j日志文件相对路径
- Android学习札记二:数据存储之SharedPreferences
- 宏、内联函数和普通函数的区别
- 路由器与交换机的区别
- MTK 报错集锦
- 在网页上显示html代码
- ssh分页模版
- First programs in PyQt4 toolkit