<%@ page language="java" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"
contentType="image/jpeg" pageEncoding="UTF-8"%>
<% //設(shè)置頁面不緩存
response.setHeader("Pragma","No-cache");
response.setHeader("Cahce-Control","no-cache");
response.setDateHeader("Expires",0);
//在內(nèi)存中創(chuàng)建圖片
int width=60,height=20;
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//獲取圖形上下文
Graphics g= image.getGraphics();
//生成隨機(jī)類
Random random= new Random();
//設(shè)置背景顏色
g.setColor(new Color(160,200,100));
g.fillRect(0,0,width,height);
//設(shè)置字體
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
//隨機(jī)產(chǎn)生50條干擾線,使圖形中的驗(yàn)證碼不易被其他的程序探測到
g.setColor(new Color(160,200,200));
for(int i=0;i<50;i++)
{
int x=random.nextInt(width);
int y=random.nextInt(height);
int x1=random.nextInt(width);
int y1=random.nextInt(height);
g.drawLine(x,y,x+x1,y+y1);
}
//隨機(jī)產(chǎn)生驗(yàn)證碼(4為數(shù)字)
String sRand="";
for(int i=0;i<4;i++)
{
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
//將驗(yàn)證碼顯示到圖象
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("rand",sRand); //////將產(chǎn)生的驗(yàn)證碼存儲到sesson中
g.dispose();
ImageIO.write(image,"JPEG",response.getOutputStream());
out.clear(); //***********
out=pageContext.pushBody();//**********
%>
*********************************************************************
<img src="yanzhengma.jsp" id="CheckCodeimg" title="看不清請點(diǎn)擊圖片!"/><html:errors property="yanzhengma"/>
<a href="#" onclick="document.getElementById('CheckCodeimg').src='yanzhengma.jsp';">看不清?</a><br/>