jsp页面上:

<META HTTP-EQUIV="pragma" CONTENT="no-cache">

<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">

<meta http-equiv="expires" content="0">


<img id="vials" src="createPicture" style="height: 30px; width: 100px;">

//createPicture是control中的方法

function createPicture() {

var bigimg = document.getElementById("vials");

var url = "createPicture?m="+new Date();//new Date();防止ie缓存

bigimg.url;< /p>

};


java类产生数字与英文的验证码

package com.chinasofti.ljzs.cigaretteauthenticityquery.util;


import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.p_w_picpath.BufferedImage;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.util.Random;


import javax.p_w_picpathio.ImageIO;

import javax.p_w_picpathio.stream.ImageOutputStream;


public class VerificationCodeUtil {

private ByteArrayInputStream p_w_picpath;// 图像

private String str;// 验证码

private static final int WIDTH = 80;

private static final int HEIGHT = 20;

BufferedImage p_w_picpaths;

public BufferedImage getImages() {

return p_w_picpaths;

}


public void setImages(BufferedImage p_w_picpaths) {

this.p_w_picpaths = p_w_picpaths;

}


public static void main(String[] arg) {

VerificationCodeUtil vcu = VerificationCodeUtil.Instance();

System.err.println(vcu.getVerificationCodeValue());

}


public static VerificationCodeUtil Instance() {

return new VerificationCodeUtil();

}


private VerificationCodeUtil() {

p_w_picpaths = new BufferedImage(WIDTH, HEIGHT,

BufferedImage.TYPE_INT_RGB);

//int randomNum = new Random().nextInt(1);

//if (randomNum == 1) {

initLetterAndNumVerificationCode(p_w_picpaths);

//}

}


public void initLetterAndNumVerificationCode(BufferedImage p_w_picpath) {


Random random = new Random(); // 生成随机类

Graphics g = initImage(p_w_picpath, random);

String[] letter = { "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" };

String sRand = "";

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

String tempRand = "";

if (random.nextBoolean()) {

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

} else {

tempRand = letter[random.nextInt(25)];

if (random.nextBoolean()) {// 随机将该字母变成小写

tempRand = tempRand.toLowerCase();

}

}

sRand += tempRand;

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

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

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

}

this.setStr(sRand);/* 赋值验证码 */

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

this.setImage(drawImage(p_w_picpath));

}


public Graphics initImage(BufferedImage p_w_picpath, Random random) {

Graphics g = p_w_picpath.getGraphics(); // 获取图形上下文

g.setColor(getRandColor(200, 250));// 设定背景色

g.fillRect(0, 0, WIDTH, HEIGHT);

g.setFont(new Font("Times New Roman", Font.PLAIN, 20));// 设定字体

g.setColor(getRandColor(160, 200)); // 随机产生165条干扰线,使图象中的认证码不易被其它程序探测到

for (int i = 0; i < 165; 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);

}

return g;

}


public ByteArrayInputStream drawImage(BufferedImage p_w_picpath) {

ByteArrayInputStream input = null;

ByteArrayOutputStream output = new ByteArrayOutputStream();

try {

ImageOutputStream p_w_picpathOut = ImageIO

.createImageOutputStream(output);

ImageIO.write(p_w_picpath, "JPEG", p_w_picpathOut);

p_w_picpathOut.close();

input = new ByteArrayInputStream(output.toByteArray());

} catch (Exception e) {

System.out.println("验证码图片产生出现错误:" + e.toString());

}

return input;

}


/*

* 功能:给定范围获得随机颜色

*/

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

}


/**

* 功能:获取验证码的字符串值

*

* @return

*/

public String getVerificationCodeValue() {

return this.getStr();

}


/**

* 功能:取得验证码图片

*

* @return

*/

public ByteArrayInputStream getImage() {

return this.p_w_picpath;

}


public String getStr() {

return str;

}


public void setStr(String str) {

this.str = str;

}


public void setImage(ByteArrayInputStream p_w_picpath) {

this.p_w_picpath = p_w_picpath;

}

}


control层中

ImageIO.write(vcu.getImages(), "jpeg",response().getOutputStream());