最近在写一个小项目,用的是Servlet+JSP,下面是其中验证码的实现!
JSP页面代码:
<script type="text/javascript">
function refresh(obj) {
obj.src = "imageServlet.action?dd=" + Math.random();//可以实现点击刷新
}
</script>
<form method="post" name="guestbook" id="guestbook"
onSubmit="return check()">
<dl class="clearfix">
<dt>
您的姓名:
</dt>
<dd>
<input name="Guest_Name" type="text" id="Guest_Name" />
<span>*</span>
</dd>
<dt>
邮件地址:
</dt>
<dd>
<input name="Guest_Email" type="text" id="Guest_Email" />
<span>*</span>
</dd>
<dt>
电话:
</dt>
<dd>
<input name="Guest_TEL" type="text" id="Guest_TEL" />
</dd>
<dt>
传真:
</dt>
<dd>
<input name="Guest_FAX" type="text" id="Guest_FAX" />
</dd>
<dt>
地址:
</dt>
<dd>
<input name="Guest_ADD" type="text" id="Guest_ADD" />
</dd>
<dt>
邮编:
</dt>
<dd>
<input name="Guest_ZIP" type="text" id="Guest_ZIP" />
</dd>
<dt>
留言内容:
</dt>
<dd>
<textarea name="Content" cols="" rows="" class="Content"
id="Content"></textarea>
</dd>
<dt>
验证码:
</dt>
<dd>
<input name="checkcode" type="text" id="checkcode"
maxlength="4" autocomplete="off" />
<img src="imageServlet.action" title="点击刷新" alt="点击刷新"
οnclick="javascript:refresh(this);" align="absmiddle"
style="cursor: pointer;" width="80px" height="27px" />
<span>*</span>
</dd>
</dl>
<p>
<input name="submit" type="submit" id="submit" value="提交信息" />
</p>
</form>
Servlet的代码:(servlet中的实现我用的是一个service方法下可以接受多个请求路径)
if (path.equals("imageServlet")) {// 生成验证码
response.reset();//重新设置响应包头的信息
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control","no-store,no-cache,must-revalidate");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
HttpSession session = request.getSession();
// 生成一个图片
Map<String, BufferedImage> map = ValidateCode.createImage();
// 将图片给imageStream
String code = map.keySet().iterator().next();
//绑定验证码到session中
session.setAttribute("code", code);
BufferedImage image = map.get(code);
ServletOutputStream output = response.getOutputStream();
ImageIO.write(image, "jpg", output);
}
生成验证码的工具类:ValidateCode.java的代码
package com.internate.util;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ValidateCode {
private static final String[] chars = { "1", "2", "3", "4", "5", "6",
"7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "M","N"};
private static final int SIZE = 4;
private static final int LINES = 20;
private static final int WIDTH = 200;
private static final int HEIGHT = 100;
private static final int FONT_SIZE = 40;
public static Map<String,BufferedImage> createImage() {
StringBuffer sb = new StringBuffer();
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics graphic = image.getGraphics();
graphic.setColor(Color.LIGHT_GRAY);
graphic.fillRect(0, 0, WIDTH, HEIGHT);
Random ran = new Random();
//画随机字符
for(int i=1;i<=SIZE;i++){
//0--chars.length-1
int r = ran.nextInt(chars.length);
graphic.setColor(getRandomColor());
graphic.setFont(new Font(null,Font.BOLD+Font.ITALIC,FONT_SIZE));
graphic.drawString(chars[r],(i-1)*WIDTH/SIZE , HEIGHT/2);
sb.append(chars[r]);//将字符保存,存入Session
}
//画干扰线
for(int i=1;i<=LINES;i++){
graphic.setColor(getRandomColor());
graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH),ran.nextInt(HEIGHT));
}
Map<String,BufferedImage> map = new HashMap<String,BufferedImage>();
map.put(sb.toString(), image);
return map;
}
public static InputStream getInputStream(BufferedImage image) throws Exception{
ByteArrayOutputStream bos =
new ByteArrayOutputStream();
JPEGImageEncoder encode =
JPEGCodec.createJPEGEncoder(bos);
encode.encode(image);//将图片编码成JPEG格式
byte[] bts = bos.toByteArray();
ByteArrayInputStream bis =
new ByteArrayInputStream(bts);
return bis;
}
public static Color getRandomColor(){
Random ran = new Random();
Color color = new Color(ran.nextInt(256),ran.nextInt(256),ran.nextInt(256));
return color;
}
}
经验证是可以实现验证码的生成