Topic: JSP彩色验证码

  Print this page

1.JSP彩色验证码 Copy to clipboard
Posted by: chengbd
Posted on: 2004-07-06 15:21

生成有4个随机数字和杂乱背景的图片,数字和背景颜色会改变,服务器端刷新(用history.go(-1)也会变)
原型参考ALIBABA http://china.alibaba.com/member/showimage

------------产生验证码图片的文件-----image.jsp-------------------------------------------

<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
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=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

// 获取图形上下文
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(new Color());
//g.drawRect(0,0,width-1,height-1);

// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
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);
}

// 取随机产生的认证码(4位数字)
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
session.setAttribute("rand",sRand);

// 图象生效
g.dispose();

// 输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());

%>

---------------使用验证码图片的文件---------a.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="check.jsp">
<table>
<tr>
<td align=left>系统产生的认证码:</td>
<td><img border=0 src="image.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>
</form>
</body>
</html>

-----------------验证的页面----------check.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 = (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>
代码:

jspcode.rar (2.22k)

2.Re:JSP彩色验证码 [Re: chengbd] Copy to clipboard
Posted by: why
Posted on: 2004-07-07 04:35

请问大侠这是原创的吗?

3.Re:JSP彩色验证码 [Re: chengbd] Copy to clipboard
Posted by: chengbd
Posted on: 2004-07-07 10:17

不是原创,以前收集的,记不起出处了,感觉功能不错。
能不能加一分呀:)

4.Re:JSP彩色验证码 [Re: chengbd] Copy to clipboard
Posted by: why
Posted on: 2004-07-07 11:19

这样子不够加一分
尤其是我不太喜欢它直接用 JSP
不过我会看看纪录,大侠好像在J2EE版上还有一两个不错但未足以加分的贴子
或者可以集腋成裘的算

请耐心等候一下 Smile

5.Re:JSP彩色验证码 [Re: chengbd] Copy to clipboard
Posted by: chengbd
Posted on: 2004-07-07 12:24

用JSP显示还是相当方便的嘛!
只是想凑够5个积分,就可以看看其它看不到的帖子了。
在这里向版主要分,真是惭愧呀!Embaressed

6.Re:JSP彩色验证码 [Re: chengbd] Copy to clipboard
Posted by: realpal
Posted on: 2004-07-15 20:45

不行啊,我在weblogic812下验证通不过啊,验证码图片始终出不来。请问是怎么回事?没有错误,就是页面图片显示不出来。谢谢!

7.Re:JSP彩色验证码 [Re: realpal] Copy to clipboard
Posted by: realpal
Posted on: 2004-07-16 14:55

修改了一下,在win下没问题,但是在rh linux下有问题。可能是linux的图像支持不好。请问有没有什么解决办法?谢谢
错误代码为:
Error 500--Internal Server Error
java.lang.NoClassDefFoundError
  at java.lang.Class.forName0(Native Method)
  at java.lang.Class.forName(Class.java:140)
  at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:62)
  at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1041)
  at java.awt.image.BufferedImage.getGraphics(BufferedImage.java:1031)
  at com.aspire.misc.infrastruct.image.doGet(image.java:51)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
  at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:971)
  at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:402)
  at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
  at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6350)
  at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
  at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
  at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3635)
  at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2585)
  at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
  at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)


Error 500--Internal Server Error
java.lang.NoClassDefFoundError
  at java.lang.Class.forName0(Native Method)
  at java.lang.Class.forName(Class.java:140)
  at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:62)
  at java.awt.image.BufferedImage.createGraphics(BufferedImage.java:1041)
  at java.awt.image.BufferedImage.getGraphics(BufferedImage.java:1031)
  at com.aspire.misc.infrastruct.image.doGet(image.java:51)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
  at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run(ServletStubImpl.java:971)
  at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:402)
  at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubImpl.java:305)
  at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:6350)
  at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:317)
  at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:118)
  at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppServletContext.java:3635)
  at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2585)
  at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
  at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)

8.Re:JSP彩色验证码 [Re: chengbd] Copy to clipboard
Posted by: kkg
Posted on: 2004-07-25 14:23

测试感觉很好~

9.Re:JSP彩色验证码 [Re: chengbd] Copy to clipboard
Posted by: hohaischooldays
Posted on: 2004-07-31 21:00

请教: //设置页面不缓存 的好处是什么?

10.Re:JSP彩色验证码 [Re: chengbd] Copy to clipboard
Posted by: 周轻舟
Posted on: 2004-08-26 16:57

我的也是这样,在linux下无法显示,到底是什么原因呢?

11.Re:JSP彩色验证码 [Re: chengbd] Copy to clipboard
Posted by: freshlll
Posted on: 2004-09-09 13:13

缓存就会写磁盘吗!这必然会耗时间

12.Re:JSP彩色验证码 [Re: chengbd] Copy to clipboard
Posted by: lnsylt
Posted on: 2004-09-09 17:06

关注!

13.Re:JSP彩色验证码 [Re: chengbd] Copy to clipboard
Posted by: fanxingabc16
Posted on: 2004-09-15 11:44

我看还是在倒数第二行加上个 response.reset();
比较保险


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923