最后效果图:
[img]https://img-my.csdn.net/uploads/201208/24/1345771701_7946.png[/img]
1.首先下载jcaptcha的jar包,网上一搜,进入官网便可下载,这里不多累赘叙述。
2.把jcaptcha,commons-collections,commons-logging的jar包放入项目中。(此处commons-collections,commons-logging包需要到网上另外下载)
3.建立一个验证码引擎或者说验证码方案类CaptchaEngine,代码贴上
4.建立一个验证码生成类CaptchaServiceSingleton(采用单例模式)
5.创建一个Servlet,用它来向叶面输出一个图片验证码import java.awt.image.BufferedImage;
至此,使用到的3个类全部完成
6.把servlet配置到web.xml中
7.编写JSP页面
OK,所有工作完成,部署项目运行之后就能看到效果图上的效果
另外附上后台验证代码
到此全部结束
最后来一张项目结构图
[img]https://img-my.csdn.net/uploads/201208/24/1345774494_6077.png[/img]
摘自:[url]http://blog.csdn.net/hoking_in/article/details/7902380[/url]
[img]https://img-my.csdn.net/uploads/201208/24/1345771701_7946.png[/img]
1.首先下载jcaptcha的jar包,网上一搜,进入官网便可下载,这里不多累赘叙述。
2.把jcaptcha,commons-collections,commons-logging的jar包放入项目中。(此处commons-collections,commons-logging包需要到网上另外下载)
3.建立一个验证码引擎或者说验证码方案类CaptchaEngine,代码贴上
package hoking.in.jcaptcha;
import java.awt.Color;
import java.awt.Font;
import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.FunkyBackgroundGenerator;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
import com.octo.captcha.component.image.textpaster.RandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.word.wordgenerator.RandomWordGenerator;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.GimpyFactory;
/**
* 验证码引擎-->彩色验证码
*/
public class CaptchaEngine extends ListImageCaptchaEngine {
protected void buildInitialFactories() {
// 随机生成的字符
WordGenerator wordGenerator = new RandomWordGenerator("0123456789");
// 文字显示的个数
TextPaster textPaster = new RandomTextPaster(4, 5, Color.WHITE);
// 图片的大小
BackgroundGenerator backgroundGenerator = new FunkyBackgroundGenerator(
80, 30);
// 字体格式
Font[] fontsList = new Font[] { new Font("Arial", 0, 10),
new Font("Tahoma", 0, 10), new Font("Verdana", 0, 10)};
// 文字的大小
FontGenerator fontGenerator = new RandomFontGenerator(new Integer(16),
new Integer(20), fontsList);
WordToImage wordToImage = new ComposedWordToImage(fontGenerator,
backgroundGenerator, textPaster);
addFactory(new GimpyFactory(wordGenerator, wordToImage));
}
}
4.建立一个验证码生成类CaptchaServiceSingleton(采用单例模式)
package hoking.in.jcaptcha;
import com.octo.captcha.service.captchastore.FastHashMapCaptchaStore;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;
import com.octo.captcha.service.image.ImageCaptchaService;
/**
* 验证码图片生成类--->单例类
*/
public class CaptchaServiceSingleton {
private static ImageCaptchaService instance =
new DefaultManageableImageCaptchaService(new FastHashMapCaptchaStore(),
new CaptchaEngine(),
180,
100000,
75000);
private CaptchaServiceSingleton(){}
public static ImageCaptchaService getInstance(){
return instance;
}
}
5.创建一个Servlet,用它来向叶面输出一个图片验证码import java.awt.image.BufferedImage;
package hoking.in.jcaptcha;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.octo.captcha.service.CaptchaServiceException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 产生一个图片验证码到输出流
*/
public class ImageCaptchaServlet extends HttpServlet {
private static final long serialVersionUID = -3718663907013663827L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
byte[] captchaChallengeAsJpeg = null;
//创建一个字节数组输出流实例
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
//获取当前session ID
String captchaId = request.getSession().getId();
//利用验证码生成类生成验证码的缓存图片实例
BufferedImage challenge = CaptchaServiceSingleton.getInstance()
.getImageChallengeForID(captchaId, request.getLocale());
//JPEG图片编码器
JPEGImageEncoder jpegEncoder = JPEGCodec
.createJPEGEncoder(jpegOutputStream);
jpegEncoder.encode(challenge);
} catch (IllegalArgumentException e) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (CaptchaServiceException e) {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
//获取图片验证码的字节数组
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
// 添加以下响应头字段:要求客户端不缓存响应消息体内容
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
//设置响应内容类型
response.setContentType("image/jpeg");
ServletOutputStream sos = response.getOutputStream();
//把图片验证码的字节数组输出到输出流中
sos.write(captchaChallengeAsJpeg);
sos.flush();
sos.close();
}
}
至此,使用到的3个类全部完成
6.把servlet配置到web.xml中
<servlet>
<servlet-name>ImageValidate</servlet-name>
<servlet-class>hoking.in.jcaptcha.ImageCaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageValidate</servlet-name>
<url-pattern>/jcaptcha</url-pattern>
</servlet-mapping>
7.编写JSP页面
<a href="javascript:void(0);" onclick="document.getElementById('imageValidate').src='${pageContext.request.contextPath }/jcaptcha?' + Math.random();">
<img src="${pageContext.request.contextPath }/jcaptcha" border="0" alt="看不清,请点击图片换一张" id="imageValidate"> 看不清,点这换一张 </a>
OK,所有工作完成,部署项目运行之后就能看到效果图上的效果
另外附上后台验证代码
//获取当前用户的session ID
String sessionId = requset.getSession().getId();
//获取用户提交的验证码
String captcha = requset.getParameter("imageValidate");
boolean flag = false;
//进行验证
flag = CaptchaServiceSingleton.getInstance().validateResponseForID(sessionId, captcha);
到此全部结束
最后来一张项目结构图
[img]https://img-my.csdn.net/uploads/201208/24/1345774494_6077.png[/img]
摘自:[url]http://blog.csdn.net/hoking_in/article/details/7902380[/url]