1. 打开javaEE,新建一个Dynamic Web Project,然后在工程下的Java Resources—src新建一个Servlet
@WebServlet("/ImgResponse")
publicclass ImgResponseextends HttpServlet {
privatestaticfinallongserialVersionUID = 1L;
public ImgResponse() {
super();
//TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequestrequest,HttpServletResponseresponse)throws ServletException, IOException {
this.doPost(request,response);
}
protected void doPost(HttpServletRequestrequest,HttpServletResponseresponse)throws ServletException, IOException {
//设置响应类型
.setContentType("image/jpeg");
//阻止页面被缓存,保证每次生成新的验证码
.setHeader("Pragma","No-Cache");
.setHeader("Cache-Control","no-cache");
.setIntHeader("Expires", 0);
//设置验证码图形的大小
intwidth = 100;
intheight = 18;
//生成一张新图片
image =new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//绘图
g =image.getGraphics();
g.setFont(new Font("ArialBlack", Font.PLAIN, 16));
g.setXORMode(Color.BLACK);
g.setColor(getRandomColor(200,250));
width - 1,height - 1);
g.setColor(new Color(102, 102, 102));
width - 1,height - 1);
//绘制一些随机线条,让画面显得更凌乱
r =new Random();
g.setColor(getRandomColor(160, 200));
for (inti = 0;i < 155; i++) {
intx0 =r.nextInt(width) - 1;
inty0 =r.nextInt(height) - 1;
intx1 =r.nextInt(12) + 1;
inty1 =r.nextInt(6) + 1;
g.drawLine(x0,y0,x0 +x1,y0 +y1);
}
for (inti = 0;i < 70; i++) {
intx0 =r.nextInt(width) - 1;
inty0 =r.nextInt(height) - 1;
intx1 =r.nextInt(12) + 1;
inty1 =r.nextInt(6) + 1;
g.drawLine(x0,y0,x0 -x1,y0 -y1);
}
//绘制验证码
intsize = 6;
//取得一个6位的随机验证码
randomString = getRandomString(size);
//绘制验证码
for (inti = 0;i < size;i++) {
g.setColor(new Color(100 +r.nextInt(110), 100 +r.nextInt(110),
r.nextInt(110)));
g.drawString(randomString.substring(i,i + 1), i * 15 + 10, 15);
}
g.dispose();
//将验证码发给浏览器
ImageIO.write(image,"JPEG",response.getOutputStream());
}
/**
* 产生一个随机的颜色
* 注意这个颜色不能漫无边际的产生,否则验证码一点也看不清
*/
private Color getRandomColor(intfc,intbc) {
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
r =new Random();
intred =r.nextInt(bc -fc);
intgreen =r.nextInt(bc -fc);
intblue =r.nextInt(bc -fc);
returnnew Color(red,green,blue);
}
/**
* @param size
* 随机字符串的长度
* @return随机字符串
*/
private String getRandomString(intsize) {
builder =new StringBuilder();
r =new Random();
intcount = 0;
while (count <size) {
inttemp =r.nextInt('z' + 1);
if ((temp >='A' && temp <= 'Z') || (temp >='a' &&temp <= 'z')
temp >='0' &&temp <= '9')) {
builder.append((char)temp);
count++;
}
}
returnbuilder.toString();
}
2、在WebContent下新建一个jsp文件
<%@ page language="java"contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<!DOCTYPE htmlPUBLIC"-//W3C//DTDHTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Insert titlehere</title>
<script type="text/javascript">
function change(){
"image").src="ImgResponse?now="+new Date();
}
</script>
</head>
<body>
<form>
用户名:<inputtype="text"name="username"><br>
密码:<inputtype="password"name="password"><br>
验证码:<inputtype="text"name="code"><imgid="image"src="ImgResponse"/>
<a href="#" onclick="change()">看不清,换一张</a><br/>
<input type="submit"value="登录">
</form>
</body>
</html>