文档库 最新最全的文档下载
当前位置:文档库 › JSP验证码大全之数字验证码(源码)

JSP验证码大全之数字验证码(源码)

JSP验证码大全之数字验证码(源码)
JSP验证码大全之数字验证码(源码)

验证码应用在各种场合中,十分广泛,在多种多样的系统或软件中的新用户帐户注册、用户登录、网站统一登陆或者用户在网站发布信息模块发布文章或内容都添加的随机码功能,对用户操作过程进行一种验证,使用验证码的目的就是为了避免网络中的自动注册程序或者自动发布程序的滥用。验证码的原理其实就是随机选择一些字符码以将字符码以图片的形式展现在软件验证界面或验证页面上,当用户在进行提交操作的同时需要将图片上的验证码输入并同时提交,如果提交的字符验证码与服务器session保存的字符码相同,则认为提交信息有效,否则拒绝提交。在使用验证码过程中,为了避免自动分析程序解析图片并获得验证信息,通常需要在图片上随机生成一些干扰线或者将复杂的字符对图片进行扭曲模糊,从而增加了自动识别程序分析验证图片的难度。

JSP验证码源码大全将分别用几个篇幅的内容来介绍在JSP中几种验证码的实现源码以及使用。

一、JSP中产生数字验证码源码

数字验证码是一种最常用的验证字符码形式,以下为数字实现的JSP源码:

Num.jsp

<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*, java.util.*,javax.imageio.*" %>

<%!

Color getRandColor(int cc,int bb)

{

Random random = new Random();

if(fc>255) cc=255;

if(bc>255) bb=255;

int r=cc+random.nextInt(bb-cc);

int g=cc+random.nextInt(bb-cc);

int b=cc+random.nextInt(bb-cc);

returnnew Color(r,g,b);

} //获取随机颜色

%>

<%

response.setHeader("Pragma","No-cache");

response.setHeader("Cache-Control","no-cache");

response.setDateHeader("Expires", 0);

int width=80; //定义验证码图片的长度

int height=30; //定义验证码图片的宽度

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(getRandColor(160,200));

for (int i=0;i<155;i++)

{

int i_x = random.nextInt(width);

int i_y = random.nextInt(height);

int i_xl = random.nextInt(12);

int i_yl = random.nextInt(12);

g.drawLine(i_x,i_y,i_x+i_xl,i_y+i_yl);

}

//用线条画背景

String s_Rand="";

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

{

String rand=String.valueOf(random.nextInt(10));

s_Rand+=rand;

g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(1

10),20+random.nextInt(110)));

g.drawString(rand,13*i+6,16);

}

//产生4位随机码

session.setAttribute("rand",s_Rand);

//将验证码存入Session中

g.dispose();

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

//输出验证图片

out.clear();

out = pageContext.pushBody();

%>

接下来文章将介绍JSP中的中文验证码、其他JAVA验证码的实现以及对JSP中对验证码的调用和使用的有关内容,下一篇文章将介绍有关JSP中文验证码的源码实现。

数字验证码是一种最常用的验证字符码形式,以下为数字实现的JSP源码: Num.jsp

<%@ page contentType="image/jpeg"

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

<%!

Color getRandColor(int cc,int bb)

{

Random random = new Random();

if(fc>255) cc=255;

if(bc>255) bb=255;

int r=cc+random.nextInt(bb-cc);

int g=cc+random.nextInt(bb-cc);

int b=cc+random.nextInt(bb-cc);

returnnew Color(r,g,b);

} //获取随机颜色

%>

<%

response.setHeader("Pragma","No-cache");

response.setHeader("Cache-Control","no-cache");

response.setDateHeader("Expires", 0);

int width=80; //定义验证码图片的长度

int height=30; //定义验证码图片的宽度

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(getRandColor(160,200));

for (int i=0;i<155;i++)

{

int i_x = random.nextInt(width);

int i_y = random.nextInt(height);

int i_xl = random.nextInt(12);

int i_yl = random.nextInt(12);

g.drawLine(i_x,i_y,i_x+i_xl,i_y+i_yl);

}

//用线条画背景

String s_Rand="";

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

{

String rand=String.valueOf(random.nextInt(10));

s_Rand+=rand;

g.setColor(new

Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));

g.drawString(rand,13*i+6,16);

}

//产生4位随机码

session.setAttribute("rand",s_Rand);

//将验证码存入Session中

g.dispose();

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

//输出验证图片

out.clear();

out = pageContext.pushBody();

%>

MD5

中如何实现MD5加密源码/*** 类名:MD5Digest
* 说明:用来进行密码加密的md5公用参数
* 编写日期:2001/03/05
* 修改者:
* 修改信息:
* @author edgarlo edgarlo@https://www.wendangku.net/doc/b710700349.html,* @version 1.0
*/import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;public class MD5Digest{private MessageDigest __md5 = null;private StringBuffer __digestBuffer = null;public MD5Digest()throws NoSuchAlgorithmException{__md5 = MessageDigest.getInstance("MD5");__digestBuffer = new StringBuffer();}public String md5crypt(String s){ __digestBuffer.setLength(0);byte abyte0[] = __md5.digest(s.getBytes()); for(int i = 0; i < abyte0.length; i++) __digestBuffer.append(toHex(abyte0));return __digestBuffer.toString();}public String toHex(byte one){String HEX="0123456789ABCDEF";char[] result=new char[2]; result[0]=HEX.charAt((one & 0xf0) >> 4);result[1]=HEX.charAt(one & 0x0f);String mm=new String(result);return mm;}}--------------------------------------------------------------------------------/******************************************** ****MD5 算法的Java Bean@author:Topcat Tuppin Last Modified:10,Mar,2001*************************************************/package beartool;import

https://www.wendangku.net/doc/b710700349.html,ng.reflect.*;/*************************************************md5 类实现了RSA Data Security, Inc.在提交给IETF的RFC1321中的MD5 message-digest 算法。*************************************************/public class MD5 {/* 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的,这里把它们实现成为static final是表示了只读,切能在同一个进程空间内的多个Instance间共享*/ static final int S11 = 7;static final int S12 = 12;static final int S13 = 17;

static final int S14 = 22;static final int S21 = 5;static final int S22 = 9; static final int S23 = 14;static final int S24 = 20;static final int S31 = 4;static final int S32 = 11;static final int S33 = 16;static final int S34 = 23;static final int S41 = 6;static final int S42 = 10; static final int S43 = 15;static final int S44 = 21;static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };/* 下面的三个成员是MD5计算过程中用到的3个核心数据,在原始的C实现中被定义到MD5_CTX结构中

*/private long[] state = new long[4]; // state (ABCD)private long[] count = new long[2]; // number of bits, modulo 2^64 (lsb first)private byte[] buffer = new byte[64]; // input buffer/* digestHexStr是MD5的唯一一个公共成员,是最新一次计算结果的16进制ASCII表示.*/public String digestHexStr;/* digest,是最新一次计算结果的2进制内部表示,表示128bit的MD5值.*/private byte[] digest = new byte[16]; /*getMD5ofStr是类MD5最主要的公共方法,入口参数是你想要进行MD5变换的字符串返回的是变换完的结果,这个结果是从公共成员digestHexStr取得的.*/public String getMD5ofStr(String inbuf) {md5Init(); md5Update(inbuf.getBytes(), inbuf.length());md5Final(); digestHexStr = "";for (int i = 0; i < 16; i++) { digestHexStr += byteHEX(digest);}return digestHexStr;}// 这是MD5这个类的标准构造函数,JavaBean要求有一个public的并且没有参数的构造函数public MD5() { md5Init();return;}/* md5Init是一个初始化函数,初始化核心变量,装入标准的幻数*/private void md5Init() { count[0] = 0L;count[1] = 0L;///* Load magic initialization constants.state[0] = 0x67452301L; state[1] = 0xefcdab89L;state[2] = 0x98badcfeL; state[3] = 0x10325476L;return;}/* F, G, H ,I 是4个基本的MD5函数,在原始的MD5的C实现中,由于它们是简单的位运算,可能出于效率的考虑把它们实现成了宏,在java中,我们把它们实现成了private方法,名字保持了原来C中的。*/private long F(long x, long y, long z) { return (x & y) | ((~x) & z);}private long G(long x, long y, long z) { return (x & z) | (y & (~z));}private long H(long x, long y, long z) { return x ^ y ^ z;}private long I(long x, long y, long z) { return y ^ (x | (~z));}/* FF,GG,HH和II将调用

F,G,H,I进行近一步变换FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.Rotation is separate from addition to prevent recomputation.*/ private long FF(long a, long b, long c, long d, long x, long s, long ac) { a += F (b, c, d) + x + ac; a = ((int) a << s) | ((int) a >>> (32 - s)); a += b;return a;} private long GG(long a, long b, long c, long d, long x, long s,long ac) { a += G (b, c, d) + x + ac; a = ((int) a << s) | ((int) a >>> (32 - s)); a += b;return a;} private long HH(long a, long b, long c, long d, long x, long s,long ac) { a += H (b, c, d) + x + ac; a = ((int) a << s) | ((int) a >>> (32 - s)); a += b;return a;} private long II(long a, long b, long c, long d, long x, long s,long ac) { a += I (b, c, d) + x + ac; a = ((int) a << s) | ((int) a >>> (32 - s));

a += b;return a;}/*md5Update是MD5的主计算过程,inbuf是要变换的字节串,inputlen是长度,这个函数由getMD5ofStr 调用,调用之前需要调用md5init,因此把它设计成private的*/private void md5Update(byte[] inbuf, int inputLen) {int i, index, partLen; byte[] block = new byte[64];index = (int)(count[0] >>> 3) & 0x3F; // /* Update number of bits */if ((count[0] += (inputLen << 3)) < (inputLen << 3))count[1]++;count[1] += (inputLen >>> 29);partLen = 64 - index;// Transform as many times as possible.if (inputLen >= partLen) { md5Memcpy(buffer, inbuf, index, 0, partLen); md5Transform(buffer);for (i = partLen; i + 63 < inputLen; i += 64) {md5Memcpy(block, inbuf, 0, i, 64); md5Transform (block);}index = 0;} else i = 0;///* Buffer remaining input */md5Memcpy(buffer, inbuf, index, i, inputLen - i);}/*md5Final整理和填写输出结果*/private void md5Final () {byte[] bits = new byte[8]; int index, padLen;///* Save number of bits */ Encode (bits, count, 8);///* Pad out to 56 mod 64. index = (int)(count[0] >>> 3) & 0x3f;padLen = (index < 56) ? (56 - index) : (120 - index);md5Update (PADDING, padLen);

///* Append length (before padding) */md5Update(bits, 8);

///* Store state in digest */Encode (digest, state, 16);}

/* md5Memcpy是一个内部使用的byte数组的块拷贝函数,从input的inpos开始把len长度的字节拷贝到output的outpos位置开始*/private void md5Memcpy (byte[] output, byte[] input,int outpos, int inpos, int len){int i;for (i = 0; i < len; i++) output[outpos + i] = input[inpos + i];}/* md5Transform是MD5核心变换程序,有md5Update调用,block是分块的原始字节*/private void md5Transform (byte block[]) {long a = state[0], b = state[1], c = state[2], d = state[3];long[] x = new long[16];Decode (x, block, 64);/* Round 1 */ a = FF (a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */ d = FF (d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */ c = FF (c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */ b = FF (b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */ a = FF (a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */ d = FF (d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */ c = FF (c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */ b = FF (b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */ a = FF (a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */ d = FF (d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */ c = FF (c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */ b = FF (b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */ a = FF (a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */ d = FF (d, a, b, c, x[13], S12, 0xfd987193L); /*

14 */ c = FF (c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */

b = FF (b, c, d, a, x[15], S14, 0x49b40821L); /* 16 *//* Round 2 */ a = GG (a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */ d = GG (d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */

c = GG (c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */ b = GG (b, c, d, a, x[0], S24, 0xe9b6c7aaL); /*

20 */ a = GG (a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */

d = GG (d, a, b, c, x[10], S22, 0x2441453L); /* 22 */ c = GG (c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */ b = GG (b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */ a = GG (a, b, c, d, x[9], S21, 0x21e1cde6L); /*

25 */ d = GG (d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */

c = GG (c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */ b = GG (b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */ a = GG (a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */

d = GG (d, a, b, c, x[2], S22, 0xfcefa3f8L); /*

30 */ c = GG (c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */

b = GG (b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 *//* Round 3 */ a = HH (a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */ d = HH (d, a, b, c,

x[8], S32, 0x8771f681L); /* 34 */ c = HH (c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */ b = HH (b, c, d, a, x[14], S34, 0xfde5380cL); /*

36 */ a = HH (a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */

d = HH (d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */ c = HH (c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */ b = HH (b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */ a = HH (a, b, c, d, x[13], S31, 0x289b7ec6L); /*

41 */ d = HH (d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */

c = HH (c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */ b = HH (b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */ a = HH (a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */

d = HH (d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */ c = HH (c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */

b = HH (b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 *//* Round 4 */

a = II (a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */ d = II (d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */ c = II (c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */

b = II (b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */

a = II (a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */ d = II (d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */ c = II (c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */

b = II (b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */ a = II (a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */

d = II (d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */ c = II (c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */ b = II (b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */ a = II (a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */ d = II (d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */

c = II (c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */ b = II (b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */state[0] += a; state[1] += b;state[2] += c;state[3] += d;}/*Encode把long数组按顺序拆成byte数组,因为java 的long类型是64bit的,只拆低32bit,以适应原始C实现的用途*/ private voi

d Encod

e (byte[] output, long[] input, int len) {int i, j;

for (i = 0, j = 0; j < len; i++, j += 4) {output[j] = (byte)(input & 0xffL);output[j + 1] = (byte)((input >>> 8) & 0xffL); output[j + 2] = (byte)((input >>> 16) & 0xffL);output[j + 3] = (byte)((input >>> 24) & 0xffL);}}/*Decode把byte 数组按顺序合成成long数组,因为java的long类型是64bit的,只合成低32bit,高32bit清零,以适应原始C实现的用途*/private void Decode (long[] output, byte[] input, int len) {int i, j;for (i = 0, j = 0;

j < len; i++, j += 4)output = b2iu(input[j]) | (b2iu(input[j + 1]) << 8) |(b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);return;}/*

b2iu是我写的一个把byte按照不考虑正负号的原则的”升位”程序,因为java没有unsigned 运算*/public static long b2iu(byte b) {return b < 0 ?

b & 0x7F + 128 : b;}/*byteHEX(),用来把一个byte类型的数转换成十六进制的ASCII表示,因为java中的byte的toString无法实现这一点,我们又没有C语言中的sprintf(outbuf,"%02X",ib)*/publi

c static String byteHEX(byte ib) { char[] Digit = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; char [] ob = new char[2];ob[0] = Digit[(ib >>> 4) & 0X0F];

ob[1] = Digit[ib & 0X0F];String s = new String(ob); return s;}public static void main(String args[]) {

MD5 m = new MD5();if (Array.getLength(args) == 0) { //如果没有参数,执行标准的Test Suite

System.out.println("MD5 Test suite:"); System.out.println("MD5(\"\"):"+m.getMD5ofStr(""));

System.out.println("MD5(\"a\"):"+m.getMD5ofStr("a"));

System.out.println("MD5(\"abc\"):"+m.getMD5ofStr("abc"));

System.out.println("MD5(\"message digest\"):"+m.getMD5ofStr("message digest")); System.out.println("MD5(\"abcdefghijklmnopqrstuvwxyz\"):"+

m.getMD5ofStr("abcdefghijklmnopqrstuvwxyz"));

System.out.println("MD5(\"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz0123456789\"):"+

m.getMD5ofStr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234 56789"));}else System.out.println("MD5(" + args[0] + ")=" + m.getMD5ofStr(args[0]));

}}JSP中的使用方法----------------------------------------------------------------

---------------<%@ page language='java' %><%@ page import='java.util.*'%><%@ page import='java.sql.*'%><%String userid = request.getParameter("UserID"); //获取用户输入UserID String password = request.getParameter("Password"); //获取用户输入的Password String pwdmd5 = oMD5.getMD5ofStr(password); //计算MD5的值PrintWriter rp = response.getWriter();Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:community", "", "");

Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from users where userID ='"+userid+"' and pwdmd5= '" + pwdmd5+"'" );if (rs.next()) {rp.print("Login OK");}else{ rp.print("Login Fail");}stmt.close();con.close();%>

如何在JSP中使用MYSQL中的登录界面(内涵完整代码)

如何在JSP中使用MYSQL中的登录界面 1.和创建登录的JSP界面,action指向servlet中的方法 2.创建一个新的字符用于存储新的数字和字符String msg=""; 3.获取JSP中输入的数据。String account = request.getParameter("name"); 4.Try语句调用后面的方法。先写方法 方法: 连接数据库和MYSQL中方法一样 try{ String driverName="com.mysql.jdbc.Driver"; //数据库用户名 String userName="root"; //密码 String userPasswd="123456"; //数据库名 String dbName="xueshenbiao"; //表名 String tableName="nima"; //联结字符串 String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+u serPasswd; Class.forName("com.mysql.jdbc.Driver").newInstance(); java.sql.Connection connection=DriverManager.getConnection(url); Statement statement = connection.createStatement(); 重点如下: 之前习惯了用select去巡查SQL中数据,现在因为是验证登录所以只要比对是否有一样的数据即可,现在采用SELECT count(*)用于计数,如果返回是0说明不存在登录的用户,返回1说明存在,返回>1说明不止一个。 5.接下来寻找个数用while(rs.next())寻找个数 6.和删除一样用result返回参数. 7.此时的result有了自己的判断力用于前面的判断。 跳回前面的try语句 用If()else语句来判断登录的界面

JSP生成验证码_源代码

jsp验证码实现源代码 (2011-10-16 11:11:40) 验证码原理:①首先随机生成数字或字母;②把生成的数字或字母保存在Session这次会话中;③最后,在登陆页面上把接收用户输入的验证码与生成的验证码相匹配,成功返回TRUE,失败则返回FALSE; 演示程序包括三个文件: 1.login.jsp:登录页面 2.code.jsp:生成验证码图片页面 3.check.jsp:验证结果 code.jsp <%@ 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 =""+ (Math.random()*10000); 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; }

jsp验证码生成以及刷新

关于jsp验证码生成,显示,验证和刷新: 1:四位数据的验证图片生成,用servlet实现 2:在web.xmlli配置servlet 3:页面上显示servlet生成的图片 4:关于验证码输入数据的验证 5:验证码的刷新 ——————————————————————– 1:四位数据的验证图片生成,用servlet实现,servlet代码如下: import java.io.*; import javax.servlet.http.*; import javax.servlet.*; import java.util.*; import java.awt.*; import java.awt.image.*; import javax.imageio.*; public class vImage extends HttpServlet { public void init(ServletConfig conf) throws ServletException { super.init(conf); } public void doGet(HttpServletR equest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType(‖image/jpeg‖); res.setHeader(‖Pragma‖, ―No-cache‖); res.setHeader(‖Cache-Control‖, ―no-cache‖); res.setDateHeader(‖Expires‖, 0); HttpSession session = req.getSession(); // 在内存中创建图象 int width = 60, height = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 获取图形上下文 Graphics g = image.getGraphics(); // 生成随机类 Random random = new Random();

自动生成验证码图片的工具类,收藏起来备用。

[原]自动生成验证码图片的工具类,收藏起来备用。用法:直接调用其静态方法即可。 Map map = ImageUtil.getImage();//获取图片,将图片转换成InputStream流 String key = map.keySet().iterator().next();//获取图片上的字符 BufferedImage image = map.get(key);//获取BufferedImage,赋值给imageStream其中,String为验证码的值,BufferedImage为验证码的图片。 该工具类的代码如下:ImageUtil.java package action; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.HashMap; import java.util.Map; import java.util.Random; public final class ImageUtil { private static final String[] chars = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "东", "南", "西", "北", "中", "发", "白" }; private static final int SIZE = 5;//验证码长度 private static final int LINES = 20;//干扰线条数 private static final int WIDTH = 200;//图片宽度 private static final int HEIGHT = 100;//图片高度 private static final int FONT_SIZE = 60;//字体高度 public static Map getImage() { StringBuffer sb = new StringBuffer(); BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics graphic = image.getGraphics(); graphic.setColor(Color.LIGHT_GRAY); graphic.fillRect(0, 0, WIDTH, HEIGHT); Random ran = new Random(); //画随机字符 for(int i=1;i<=SIZE;i++){ int r = ran.nextInt(chars.length); graphic.setColor(getRandomColor()); graphic.setFont(new Font(null,Font.BOLD+Font.ITALIC,FONT_SIZE));

jsp页面验证码源代码

jsp页面验证码源代码 在java后台中生成验证码的ImageIO传到前端页面显示,同时把验证码的v alue值传入session 中用于与用户输入的验证码进行匹配,在用户验证中使用ajax技术,在不刷新页面的同时进行验证码验证。 程序结构图: VerifyCodeUtils程序主要内容为通过Java生成验证码的图片,以及验证码的value值,程序如下: package utils; import java.awt.Color; import java.awt.Font;

import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.util.HashMap; import java.util.Map; import java.util.Random; publicclass VerifyCodeUtils { privatestatic BufferedImage image = null; privatestatic Random random = new Random(); //在自己定义的一些数中,生成4位随机数 publicstatic String getVerifyCode() { String str = ""; char[] code = newchar[]{'A','B','C','D','E','F','G','H','I ','J','K','L','M','N','P','Q','R','S','T','U', 'V','W','X','Y','Z','a','b','c','d','e','f','g','h','i',' j','k','m','n','p','q','r','s','t', 'u','v','w','x','y','z','2','3','4','5','6','7','8','9'}; Random random = new Random(); for(int i = 0; i <4; i++) { str += String.valueOf(code[random.nextInt(code.length)]); } return str; }

php用户登录页面代码源代码

//登入页面 $conn=mysql_connect('127.0.0.1','root','')or die("连接失败"); mysql_select_db('tujian',$conn)or die("未找到该数据库"); define(ALL_PS,"vivid");mysql_query("set names GBK"); if($_POST[submit]){ $postcode=strtolower($_POST["code"]); $postcode=strtoupper($_POST["code"]); $uid=str_replace(" ","",$_POST[uid]); $sql="select * from users where `uid`='$_POST[uid]'"; $query=mysql_query($sql); $user=is_array($row=mysql_fetch_array($query)); $mi=$user?md5($_POST[pass].ALL_PS)==$row[pass]:FALSE; if($mi){ $_SESSION[uid]=$row[uid]; $_SESSION[name]=$row[name]; $_SESSION[id]=$row[id]; $_SESSION[user_shell]=md5($row[uid].$row[pass].ALL_PS); if( $_SESSION["code"]==$postcode){ echo""; }else{ echo"

验证码输入错误,请重新输入!
"; } } else{ echo"
用户名或密码输入错误
"; session_destroy(); } } ?>