您的位置:

javajsp生成验证码判断(JAVA验证码生成)

javajsp生成验证码判断(JAVA验证码生成)

更新:

本文目录一览:

jsp中,如何判断验证码正确?

这个功能通常一共3个页面:

index.jsp是用来登录用的,在其中显示验证码,即img src="image.jsp"/

image.jsp是用来生成验证码的,有注释,很详细,如再不行,你复制到百度就有详解了。

result.jsp 是用来判断输入是否正确的。

相信你一定能看明白...别忘了采纳哦,谢谢。。。

【1.index.jsp】

%@ page language="java" import="java.util.*" pageEncoding="GBK"%

htmlbody

form method=post action="result.jsp"

input type=text name=input maxlength=4

img border=0 src="image.jsp"

input type="submit" value="submit"

/form/body/html

【2.image.jsp】

%@ page contentType="image/JPEG"

import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"

pageEncoding="GBK"%

%!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 100; 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("code", sRand);

// 图象生效

g.dispose();

// 输出图象到页面

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

%

【3.result.jsp】

%@ page language="java" import="java.util.*" pageEncoding="GBK"%

htmlbody

%

String input=request.getParameter("input");

String code=(String)session.getAttribute("code");

if(input.equals(code)){

out.println("验证成功!");

}else{

out.println("验证失败!");

}

%

bodyhtml

在java页面上,用jsp,怎样写一个验证码

//验证码生成页面

%@ page language="java" import="java.util.*" pageEncoding="GBK"%

%@ page import = " java.awt.*,java.awt.image.*,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 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 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("vcode" ,sRand);

g.dispose();

ImageIO.write(image, "jpg" ,os);

os.flush();

os.close();

os = null ;

response.flushBuffer();

out.clear();

out = pageContext.pushBody();

}

catch (IllegalStateException e)

{

System.out.println(e.getMessage());

e.printStackTrace();

} %

//验证码使用页面

%@ page language="java" import="java.util.*" pageEncoding="gbk"%

%

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%"

titleMy JSP 'index.jsp' starting page/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"

!--

link rel="stylesheet" type="text/css" href="styles.css"

--

script type="text/javascript"

function refresh(){

var co=document.getElementById("code");

co.src="vcode.jsp?vcode="+Math.random();

}

/script

/head

body

用户名:input type="text" name="name"br/

img id="code" title="验证码" src="vcode.jsp" a href="javascript:refresh();"换一张/a

/body

/html

java中如何在同一个jsp页面中判断图片验证码与输入的是否一致

这个问题很简单的。我想你动态产生的验证码一定是一个单独的页面然后再把这个页面导入到你的输入信息的页面的。你打开你的动态生成验证码的页面。在里面你一定可以找到一个session.setAttribute(""); 这个方法是把你验证码的值保存在session里面。根据你的意思在用js弹出来验证. 首先你在页面中加一小脚本% Stirng vcode= (String)session.getAttribute("验证码Key") %然后在你的提交按钮中加一个onClick事件... 请输入验证码:input type="text" id="txtvcode" name="txtvcode" / input type="button" name="text" id="text" value="提交" onClick="checkVcode('%= vcode%')" /这样就可以把你动态生成的验证码值传入到js的函数中去了。 script function checkVcode(vcode){ var txtvcode= document.getElementById("txtvcode").value; if(vcode==txtvcode){ alert("输入验证码正确"); }else{ alert("输入验证码错误"); } } /script人在网吧。没工具调试。我想这些代码不会出问题的。