当前位置:首页 > 代码 > 正文

jsp验证码代码(html验证码代码)

admin 发布:2022-12-19 11:40 113


今天给各位分享jsp验证码代码的知识,其中也会对html验证码代码进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

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

jsp 验证码不区分大小写代码

验证码字符.toLowerCase() 转换成小写和后台验证数据进行比较 即可无视大小写.

jsp中显示验证码的代码怎么写?

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.io.OutputStream;

import java.util.Random;

import javax.imageio.ImageIO;

/*生成验证码图片

*/

public class MakeCertPic {

 //验证码图片中可以出现的字符集,可以根据需要修改

 private char mapTable[]={

   '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','0','1','2','3',

   '4','5','6','7','8','9'

 };

/* 功能:生成彩色验证码图片

 参数wedth为生成图片的宽度,参数height为生成图片的高度,参数os为页面的输出流

*/

 public String getCertPic(int width,int height,OutputStream os){

  if(width=0)

   width=60;

  if(height=0)

   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);

  //随机产生的验证码

  String strEnsure = "";

  //4代表4为验证码,如果要产生更多位的验证码,则加大数值

  for(int i = 0;i4;++i){

   strEnsure += mapTable[(int) (mapTable.length*Math.random())];

  }

  //将认证码显示到图像中,如果要生成更多位的验证码,增加drawString语句

  g.setColor(Color.black);

  g.setFont(new Font("Atlantic Inline",Font.PLAIN,18));

  String str = strEnsure.substring(0,1);

  g.drawString(str,8,17);

  str = strEnsure.substring(1,2);

  g.drawString(str, 20, 15);

  str = strEnsure.substring(2,3);

  g.drawString(str, 35, 18);

  str = strEnsure.substring(3,4);

  g.drawString(str, 45, 15);

  //随机产生15个干扰点

  Random rand = new Random();

  for(int i=0; i10; i++){

   int x = rand.nextInt(width);

   int y = rand.nextInt(height);

   g.drawOval(x,y,1,1);

  }

  //释放图形上下文

  g.dispose();

  try{

   //输出图形到页面

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

   

  }catch (IOException e){

   return "";

  }

  return strEnsure;

 }

}

makeCertPic.jsp页面用于调用生成验证码图片的JavaBean,并在客户端显示,源代码如下:

%@page contentType="image/jpeg" %%@page language="java" pageEncoding="utf-8"%jsp:useBean id="image" scope="page" class="securityCode.pic.MakeCertPic"/%

 String str = image.getCertPic(0,0,response.getOutputStream());

 //将验证码存入session中

 session.setAttribute("certCode",str);

%

下边是登录页面:

%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""

html

head

meta http-equiv="Content-Type" content="text/html; charset=UTF-8"

title验证码测试登录页面/title

  script type="text/javascript"

function changeimg()

{

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

now = new Date(); 

myimg.src="makeCertPic.jsp?code="+now.getTime();

/script

/head

body

center

 form action="loginCheck.jsp" method="post" /

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

 密nbsp;nbsp;码:input type="password" name="password"/br

 nbsp;验证码:input type="text" name="certCode"/

 img id="code" src="makeCertPic.jsp"a href="javascript:changeimg()"看不清,换一张 /abr

 input type="submit" value="登录"/

 /form

/center

/body

/html

JSP无法显示验证码~(懂的入)答对了我再加分!

匿名的提问,真不放心

function riwrite(){

var s = "img src=/200909/login/imgcheck/img.jsp";

document.getElementById("image").innerHTML=s;

}

----》》》

function riwrite(){

var s = "img src=/200909/login/imgcheck/img.jsp?"+Math.random()+"";

document.getElementById("image").innerHTML=s;

}

关于jsp验证码代码和html验证码代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/7533.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载