首先建一个类生成一个验证码的图片代码如下
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class MakeSecurityPic {
/**
* 生成验证码的字符数字组合
*/
private char[] mapTable = { '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', '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9' };
/**
* 获取验证码图片
*
* @param width
* 图片的宽
* @param height
* 图片的高
* @param os
* 输出流
* @return
*/
public String getSecurityPic(Integer width, Integer height, OutputStream os) {
if (width == 0)
width = 60;
if (height == 0)
height = 20;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
//
Graphics2D g = image.createGraphics();
// 填充背景色
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
// 绘制边框
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1, height - 1);
// 验证码
String security = "";
// 随机生成验证码
Random random = new Random();
for (int i = 0; i < 4; i++) {
security += mapTable[random.nextInt(mapTable.length)];
}
// 绘制验证码
g.setColor(Color.BLACK);
g.setFont(new Font("MS Sans Serif", Font.PLAIN, 18));
String temp = security.substring(0, 1);
g.drawString(temp, 7, 15);
temp = security.substring(1, 2);
g.drawString(temp, 18, 17);
temp = security.substring(2, 3);
g.drawString(temp, 28, 18);
temp = security.substring(3, 4);
g.drawString(temp, 40, 17);
// 绘制干扰点
g.setColor(Color.BLACK);
for (int i = 0; i < 20; i++) {
g.drawOval(random.nextInt(width), random.nextInt(height), 0, 0);
}
// 释放资源
g.dispose();
try {
// 输出图片到缓冲区
ImageIO.write(image, "JPG", os);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return security;
}
}
再建一个 servlet 类型为
response.setContentType("image/jpg");
是一个图片类型的。
代码如下:
package com.security;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MakeSecurityServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpg");
MakeSecurityPic msp = new MakeSecurityPic();
String security = msp.getSecurityPic(0, 0, response.getOutputStream());
request.getSession().setAttribute("security", security);
}
}
显示到 jsp 页面上如下:
security:<input type="text" name="security"/><img id="securityPic" src="servlet/MakeSecurityServlet"><a href="#" onClick="reloadSecurityPic()"> 看不清楚,换一个 </a><br>
<script language="text/javascript">
function reloadSecurityPic(){
var pic=new Image(50,20);
pic.src="servlet/MakeSecurityServlet ?randum="+Math.random();
document.getElementById("securityPic ").src=pic.src;
}
</script>