jsp:::
package com.zw.fantasticblog.util;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.imageio.*;
public class ImageServlet extends HttpServlet {
public void init() throws ServletException
{
// Put your code here
}
public ImageServlet()
{
super();
}
static final int WIDTH = 100, HEIGHT = 30;
final Random rnd = new Random();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String num;
String s;
String snew;
String num2=null;
num = String.valueOf(rnd.nextInt(9000)+1000);
//既可以数字也可以是字符
for(int i=0;i<4;i++)
{
int n=rnd.nextInt(3);
s=num.substring(n,n+1);
char[] ch={
'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'
};
int j=(int)(Math.random()*26);
char c=ch[j];
snew=String.valueOf(c);
num2=num.replace(s,snew);
}
//把验证码设到session中
HttpSession session=request.getSession();
session.setAttribute("validateCode",num2);
// System.out.println(session.getId());
//System.out.println(num);
//以图像形式发送给客户端
response.setContentType("image/jpeg");
this.createImage(response.getOutputStream(),num2);
}
private void createImage(OutputStream out,String num2) {
//构造一幅图像
BufferedImage bi = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_USHORT_555_RGB);
Graphics2D g = bi.createGraphics();
// 背景
g.setColor(new Color(200, 200, 200));
g.fillRect(0, 0, WIDTH, HEIGHT);
// 加入随机背景
randomBackground(g, 25);
// 显示验证码
//g.setColor(Color.WHITE);
Font t = new Font("黑体", Font.BOLD, 30);
g.setFont(t);
Color c;
for (int i = 0; i < num2.length(); i++) {
c = getRandomColor();
g.setColor(c);
g.drawString(num2.substring(i, i + 1), 10 + 20 * i, 25);
}
try {
//放到输出流中
ImageIO.write(bi, "JPEG", out);
out.flush();
out.close();
} catch (IOException ex) {
//此处不用处理
}
}
//画背景的圆
private void randomBackground(Graphics2D g, int count) {
int x1, y1, r;
Color c;
//画小椭圆
for (int i = 0; i < count; i++) {
x1 = rnd.nextInt(WIDTH);
y1 = rnd.nextInt(HEIGHT);
r = rnd.nextInt(5)+1;
c = getRandomColor();
g.setColor(c);
g.fillOval(x1, y1, r, r);
}
//又画了很多斜线
g.setColor(getRandomColor());
for(int i=0;i<155;i++)
{
int x = rnd.nextInt(WIDTH);
int y = rnd.nextInt(HEIGHT);
int xl = rnd.nextInt(12);
int yl = rnd.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
}
private Color getRandomColor()
{
return new Color(rnd.nextInt(200), rnd.nextInt(200), rnd.nextInt(200));
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
this.doGet(request, response);
}
public void destroy()
{
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
}
html:
<script language="javascript">
function changeImage(img)
{
img.src="/Fantastic blog/imageservlet?=newid"+Math.random();
}
</script>
<body οnlοad="changeImage(loginForm.vc)">
<tr>
<td><div align="right" class="style1"> 验 证 码:</div></td>
<td>
<html:text property="validateCode" styleClass="style2"/> <img alt="请刷新" name="vc" src="/Fantastic blog/imageservlet?newid=a" />
<input type="button" οnclick="changeImage(vc);" value="看不清楚?"><br><html:errors property="validateCode"/></td>
</tr>
//paging
document.getElementById('selects').value=${currentPage};