作者在 2010-05-15 11:12:42 发布以下内容
public class ValidateServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
// 设置响应格式
response.setContentType("image/jpeg");
// 定义一个BufferedImage对象 其构造函数属性参照API
BufferedImage image = new BufferedImage(60, 20,
BufferedImage.SCALE_SMOOTH);
// 根据上述对象产生一个Graphics2D上下文
Graphics2D graphics = image.createGraphics();
// 将上下文颜色设置为白色
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, 100, 20);
// 设置书写字体
graphics.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 16));
// randomString方法见后面,负责产生由4个字符组成的字符串
String randomStr = randomString(4);
for (int i = 0; i < randomStr.length(); i++) {
// 随机设置上下文颜色
graphics.setColor(new Color(new Random().nextInt(50), new Random()
.nextInt(50), new Random().nextInt(50)));
// 按字符把其画出来
graphics.drawString(randomStr.substring(i, i + 1), 15 * i, 20);
// 产生干扰线
graphics.drawLine(5, 5, 5, 5);
}
/*将验证码保存在Session中备用*/
session.setAttribute("ValidateCode", randomStr);
// 禁止缓冲代码,没有这段的话则不能进行更新显示,删除效果请自行测试
response.setHeader("Prama", "no-cache");
response.setHeader("Coche-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 写入到客户端
ImageIO.write(image, "jpeg", response.getOutputStream());
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
// 设置响应格式
response.setContentType("image/jpeg");
// 定义一个BufferedImage对象 其构造函数属性参照API
BufferedImage image = new BufferedImage(60, 20,
BufferedImage.SCALE_SMOOTH);
// 根据上述对象产生一个Graphics2D上下文
Graphics2D graphics = image.createGraphics();
// 将上下文颜色设置为白色
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, 100, 20);
// 设置书写字体
graphics.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 16));
// randomString方法见后面,负责产生由4个字符组成的字符串
String randomStr = randomString(4);
for (int i = 0; i < randomStr.length(); i++) {
// 随机设置上下文颜色
graphics.setColor(new Color(new Random().nextInt(50), new Random()
.nextInt(50), new Random().nextInt(50)));
// 按字符把其画出来
graphics.drawString(randomStr.substring(i, i + 1), 15 * i, 20);
// 产生干扰线
graphics.drawLine(5, 5, 5, 5);
}
/*将验证码保存在Session中备用*/
session.setAttribute("ValidateCode", randomStr);
// 禁止缓冲代码,没有这段的话则不能进行更新显示,删除效果请自行测试
response.setHeader("Prama", "no-cache");
response.setHeader("Coche-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 写入到客户端
ImageIO.write(image, "jpeg", response.getOutputStream());
}
public String randomString(int number) {
// 字符串的内容组成
String str = "1234567890";
char[] chars = str.toCharArray();
int length = chars.length;
StringBuilder sb = new StringBuilder();
Random random = new Random();
// 随机生成number个字符
for (int i = 0; i < number; i++) {
// 把单个字符转换成字符串然后追加到已经产生的字符串中
sb.append(new String(new Character(chars[random.nextInt(length)])
.toString()));
}
return sb.toString();
}
// 字符串的内容组成
String str = "1234567890";
char[] chars = str.toCharArray();
int length = chars.length;
StringBuilder sb = new StringBuilder();
Random random = new Random();
// 随机生成number个字符
for (int i = 0; i < number; i++) {
// 把单个字符转换成字符串然后追加到已经产生的字符串中
sb.append(new String(new Character(chars[random.nextInt(length)])
.toString()));
}
return sb.toString();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
~~~~~~~~~~~~~~~~
throws ServletException, IOException {
doGet(request, response);
}
}
~~~~~~~~~~~~~~~~
<servlet-mapping>
<servlet-name>ValidateServlet</servlet-name>
<url-pattern>/servlet/ValidateServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet-name>ValidateServlet</servlet-name>
<url-pattern>/servlet/ValidateServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~·
请输入验证码<input size=4><img src="servlet/ValidateServlet">