index.jsp
<img border="0" src="image.jsp" οnclick="this.src='image.jsp'" title="看不清楚,点击图片即可刷新" style="cursor: hand;">
image.jsp
<%@ page contentType="image/jpeg" import="java.io.*,java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!
Random random = new Random();
Color getRandColor(int fc,int bc){//给定范围获得随机颜色
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);
}
String getVerifyCode()
{
String strBase = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
int strBaseLen = strBase.length();
String verifyCode = "";
for (int i = 0; i < 6; i++)
{
int indexOf = random.nextInt(strBaseLen);
verifyCode += strBase.substring(indexOf,indexOf+1);
}
return verifyCode;
}
%>
<%
String fontName[] = {"Arial", "Courier", "Courier New", "Times New Roman"};
int fontSizeMin = 30;
int fontSizeMax = 30;
int imageWidth = 150;
int imageHeight = 35;
int fontSize = 50;
//设置页面不缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
// 在内存中创建图象
BufferedImage gImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics gGraphic = gImage.getGraphics();
//生成随机类
Random random = new Random();
// 设定背景色
gGraphic.setColor(getRandColor(200,250));
gGraphic.fillRect(0, 0, imageWidth, imageHeight);
//画边框
gGraphic.setColor(new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)));
gGraphic.drawRect(0,0,imageWidth-1,imageHeight-1);
//产生300条以内的干扰线,使图象中的认证码不易被其它程序探测到
gGraphic.setColor(getRandColor(160,200));
int intLen = random.nextInt(300);
for (int i=0;i <intLen;i++){
int x = random.nextInt(imageWidth);
int y = random.nextInt(imageHeight);
int xl = random.nextInt(imageWidth);
int yl = random.nextInt(imageHeight);
gGraphic.drawLine(x,y,x+xl,y+yl);
}
// 取随机产生的认证码(4位数字)
String verifyCode = getVerifyCode();
int _style = Font.BOLD;
for (int i=0;i <verifyCode.length();i++){
String rand = verifyCode.substring(i,i+1);
fontSize = fontSizeMin+random.nextInt(fontSizeMax - fontSizeMin+1);
//设定字体
gGraphic.setFont(new Font(fontName[random.nextInt(fontName.length)],Font.PLAIN,fontSize));
if (random.nextInt(100) > 40)
_style ¦= Font.ITALIC;
// 将认证码显示到图象中
gGraphic.setColor(new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)));
//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
gGraphic.drawString(rand,25*i+random.nextInt(4),(fontSize * 15 / 20)+4);
}
// 图象生效
gGraphic.dispose();
//将认证码存入session
out.clear();
out = pageContext.pushBody();
session.setAttribute("veriryCode", verifyCode);
//输出图象到页面
ImageIO.write(gImage, "JPEG", response.getOutputStream());
VerifyCodeTimer timerCode = new VerifyCodeTimer (request.getSession(),"veriryCode");
timerCode.timer.schedule(timerCode, 30 * 1000);//30秒钟
%>
VerifyCodeTimer.java 文件
public class VerifyCodeTimer extends TimerTask
{
private Timer timer;
private HttpSession session;
private String sessionKey;
public VerifyCodeTimer(HttpSession session,String sessionKey)
{
timer = new Timer();
this.session = session;
this.sessionKey = sessionKey;
}
public void run()
{
session.removeAttribute(sessionKey);
//timer.cancel();
}
}
<img border="0" src="image.jsp" οnclick="this.src='image.jsp'" title="看不清楚,点击图片即可刷新" style="cursor: hand;">
虽然你在验证码图片响应头中设置了
//设置页面不缓存 response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0);
但这远远不够,如果你实际测试过就应该发现,有时点击验证码图片它并不会更新。原因在浏览器缓存。浏览器有时不会忠实的遵照你的响应头处理缓存。 改为如下:
<img src="image.jsp" title="看不清楚,点击图片即可刷新" style="border-style:none;cursor:hand;" οnclick="this.src='image.jsp?temp='+(new Date().getTime().toString(36))">