jsp如何自动生成验证码

wolililaile 2009-08-27 01:20:55
我在做一个登录注册需要做一个登录验证码 ,老师说要真的自动生成 不要搞几张图片的 该如何做到啊
...全文
416 29 打赏 收藏 转发到动态 举报
写回复
用AI写文章
29 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangwansheng 2009-08-28
  • 打赏
  • 举报
回复
代码写在jsp里时,那:看不清楚:这个按钮怎么写啊?
wang_zilu 2009-08-28
  • 打赏
  • 举报
回复
<%@ page language="java" import="java.util.*" pageEncoding="GBK"
import="java.awt.*,java.awt.image.*,javax.imageio.*"%>
<%
/*
使用方法:在需要显示验证码的html代码中使用<img p">
在需判断session的时候判断session.getAttribute("vcode")
*/try {
int codeLength = 4;//验证码长度
int mixTimes = 0;//模糊程度参数
Color bgColor = getRandColor(200, 250);//背景颜色
Color bfColor = new Color(0, 0, 0);//字体颜色
boolean ifRandomColor = true;//单个字符是否颜色随机
boolean ifMixColor = true;//模糊线是否颜色随机

//设置页面不缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 在内存中创建图象
int width = 13 * codeLength + 6, height = 20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 设定背景色
g.setColor(bgColor);
g.fillRect(0, 0, width, height);
//设定字体
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
//画边框
g.setColor(new Color(33, 66, 99));
g.drawRect(0, 0, width - 1, height - 1);
// 随机产生干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 200));
for (int i = 0; i < mixTimes * codeLength / 10; i++) {
if (ifMixColor) {
g.setColor(getRandColor(160, 200));
}
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);
}
// 取随机产生的认证码(4位数字)
String sRand = "";
for (int i = 0; i < codeLength; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
// 将认证码显示到图象中
if (ifRandomColor)
g.setColor(getRandColor(20, 110, 0));
else
g.setColor(bfColor);
//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand, 13 * i + 6, 16);
}
// 将认证码存入SESSION
session.setAttribute("vcode", sRand);
// 图象生效
g.dispose();
// 输出图象到页面
ImageIO.write(image, "PNG", response.getOutputStream());
} catch (Exception e) {
}
out.clear();
out=pageContext.pushBody();
%>
<%!//给定范围获得随机颜色
private static Random random = new Random();

private Color getRandColor(int fc, int bc) {
return getRandColor(fc, bc, fc);
}

private Color getRandColor(int fc, int bc, int interval) {
if (fc > 255) {
fc = 255;
}
if (bc > 255) {
bc = 255;
}
int r = fc + random.nextInt(bc - interval);
int g = fc + random.nextInt(bc - interval);
int b = fc + random.nextInt(bc - interval);
return new Color(r, g, b);
}%>
corehu 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhuyx808 的回复:]
http://blog.csdn.net/sxpujs/archive/2009/04/03/4045559.aspx
[/Quote]
学习了
adaikiss 2009-08-28
  • 打赏
  • 举报
回复
jcaptcha这个挺好用的!找实例看看吧
风卷残尘 2009-08-28
  • 打赏
  • 举报
回复
网上很多 随便一搜就是
  • 打赏
  • 举报
回复
http://blog.csdn.net/innolux_08/archive/2009/08/28/4491953.aspx
MJunnnn 2009-08-28
  • 打赏
  • 举报
回复
http://blog.csdn.net/innolux_08/archive/2009/08/28/4491953.aspx
huang3218755 2009-08-28
  • 打赏
  • 举报
回复
很好
liulu525800 2009-08-28
  • 打赏
  • 举报
回复
顶了
rumlee 2009-08-28
  • 打赏
  • 举报
回复
这种问题一搜索就太多了.
xuexijava 2009-08-28
  • 打赏
  • 举报
回复
Mark
Mark杨 2009-08-28
  • 打赏
  • 举报
回复
ImageServlet.java 类
com.mobile.control 包

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.util.Random;

import javax.imageio.ImageIO;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class ImageServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

this.doPost(request, response);

}

// 生成数字和字母的验证码

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

BufferedImage img = new BufferedImage(68, 22,

BufferedImage.TYPE_INT_RGB);

// 得到该图片的绘图对象

Graphics g = img.getGraphics();

Random r = new Random();

Color c = new Color(200, 150, 255);

g.setColor(c);

// 填充整个图片的颜色

g.fillRect(0, 0, 68, 22);

// 向图片中输出数字和字母

StringBuffer sb = new StringBuffer();

char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();

int index, len = ch.length;

for (int i = 0; i < 4; i ++) {

index = r.nextInt(len);

g.setColor(new Color(r.nextInt(88), r.nextInt(188), r.nextInt

(255)));

g.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 22));// 输出的

字体和大小

g.drawString("" + ch[index], (i * 15) + 3, 18);//写什么数字,在图片

的什么位置画

sb.append(ch[index]);

}

request.getSession().setAttribute("piccode", sb.toString());

ImageIO.write(img, "JPG", response.getOutputStream());

}

}


web.xml 配置
<servlet>

<servlet-name>ImageServlet</servlet-name>

<servlet-class>com.mobile.control.ImageServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>ImageServlet</servlet-name>

<url-pattern>/servlet/ImageServlet</url-pattern>

</servlet-mapping>


页面
<html>
<head>
<title>验证码</title>
<script type="text/javascript">
function changeImage()
{
document.getElementById("safeCode").src="servlet/ImageServlet?date="+new Date().getTime();
}
</script>
</head>
<body>
<img id="safeCode" src="servlet/ImageServlet"/><a href="#" onclick="changeImage()">看不清楚</a>
</body>
</html>
tk5210364 2009-08-28
  • 打赏
  • 举报
回复
<%@ page contentType="image/jpeg" import="java.awt.*,
java.awt.image.*,java.util.*,javax.imageio.*" %>
<% // 在内存中创建图象
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 设定背景色
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
//画边框
g.setColor(Color.black);
g.drawRect(0,0,width-1,height-1);
// 取随机产生的认证码(4位数字)
String rand = request.getParameter("rand");
rand = rand.substring(0,rand.indexOf("."));
switch(rand.length()) {
case 1: rand = "000"+rand; break;
case 2: rand = "00"+rand; break;
case 3: rand = "0"+rand; break;
default: rand = rand.substring(0,4); break;
}
// 将认证码存入
SESSION session.setAttribute("rand",rand);
// 将认证码显示到图象中
g.setColor(Color.black);
Integer tempNumber = new Integer(rand);
String numberStr = tempNumber.toString();
g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));
String Str = numberStr.substring(0,1);
g.drawString(Str,8,17);
Str = numberStr.substring(1,2);
g.drawString(Str,20,15);
Str = numberStr.substring(2,3);
g.drawString(Str,35,18);
Str = numberStr.substring(3,4);
g.drawString(Str,45,15);
// 随机产生88个干扰点,使图象中的认证码不易被其它程序探测到
Random random = new Random();
for (int i=0;i<20;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawOval(x,y,0,0);
}
// 图象生效
g.dispose();
// 输出图象到页面

ImageIO.write(image, "JPEG", response.getOutputStream());
%>

就 OK 了
lcj_up 2009-08-27
  • 打赏
  • 举报
回复
学习啦
百足之虫 2009-08-27
  • 打赏
  • 举报
回复
3楼提供的很实用,支援!
  • 打赏
  • 举报
回复
简单的可以在服务自己写代码生成验证码。

如果需要 Captcha Test,Java 中有个 JCaptcha 工具:

http://jcaptcha.sourceforge.net/
wolililaile 2009-08-27
  • 打赏
  • 举报
回复
有几个回帖的很好 等我结贴吧
工程师WWW 2009-08-27
  • 打赏
  • 举报
回复
顶顶顶
doubleXiao 2009-08-27
  • 打赏
  • 举报
回复
注释
doubleXiao 2009-08-27
  • 打赏
  • 举报
回复
写哈注册!
加载更多回复(9)

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧