本站首页    管理页面    写新日志    退出


«October 2025»
1234
567891011
12131415161718
19202122232425
262728293031


公告
 本博客在此声明所有文章均为转摘,只做资料收集使用。

我的分类(专题)

日志更新

最新评论

留言板

链接

Blog信息
blog名称:
日志总数:1304
评论数量:2242
留言数量:5
访问次数:7628131
建立时间:2006年5月29日




[Java Open Source]用开源组件jcaptcha做jsp彩色验证码
软件技术

lhwork 发表于 2006/8/11 10:17:07

安装Add jcaptcha-all.jar (provided in bin-distribution500)this.width=500'>) and ehcache.jar (not provided see ehcache site500)this.width=500'>) to your application class path, ie in you WEB-INF/lib folder.实例一个jcaptcha服务,注意,必须是单例模式的Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->import com.octo.captcha.service.image.ImageCaptchaService;import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;public class CaptchaServiceSingleton {        private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService();        public static ImageCaptchaService getInstance(){        return instance;    }}注:以上是默认的一个实现,下面是其他更多的实现SimpleListSoundCaptchaEngine   //还可以用声音,真爽哦 SpellerSoundCaptchaEngine SpellerSoundCaptchaEngine DefaultGimpyEngineCaptcha            BaffleListGimpyEngineCaptcha            BasicListGimpyEngineCaptcha            DeformedBaffleListGimpyEngineCaptcha            DoubleRandomListGimpyEngineCaptcha            SimpleListImageCaptchaEngineCaptcha            SimpleFishEyeEngineCaptcha具体请参考官方说明编写一个产生图片的servletCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->import com.octo.captcha.service.CaptchaServiceException;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;import java.io.IOException;public class ImageCaptchaServlet extends HttpServlet {    public void init(ServletConfig servletConfig) throws ServletException {        super.init(servletConfig);    }    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {               byte[] captchaChallengeAsJpeg = null;       // the output stream to render the captcha image as jpeg into        ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();        try {        // get the session id that will identify the generated captcha.         //the same id must be used to validate the response, the session id is a good candidate!        String captchaId = httpServletRequest.getSession().getId();        // call the ImageCaptchaService getChallenge method            BufferedImage challenge =                    CaptchaServiceSingleton.getInstance().getImageChallengeForID(captchaId,                            httpServletRequest.getLocale());                        // a jpeg encoder            JPEGImageEncoder jpegEncoder =                    JPEGCodec.createJPEGEncoder(jpegOutputStream);            jpegEncoder.encode(challenge);        } catch (IllegalArgumentException e) {            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);            return;        } catch (CaptchaServiceException e) {            httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);            return;        }        captchaChallengeAsJpeg = jpegOutputStream.toByteArray();        // flush it in the response        httpServletResponse.setHeader("Cache-Control", "no-store");        httpServletResponse.setHeader("Pragma", "no-cache");        httpServletResponse.setDateHeader("Expires", 0);        httpServletResponse.setContentType("image/jpeg");        ServletOutputStream responseOutputStream =                httpServletResponse.getOutputStream();        responseOutputStream.write(captchaChallengeAsJpeg);        responseOutputStream.flush();        responseOutputStream.close();    }}为servlet修改web.xml配置文件Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><servlet>        <servlet-name>jcaptcha</servlet-name>        <servlet-class>ImageCaptchaServlet</servlet-class>        <load-on-startup>0</load-on-startup>    </servlet><servlet-mapping>        <servlet-name>jcaptcha</servlet-name>        <url-pattern>/jcaptcha</url-pattern></servlet-mapping>编写你的客户端的展示Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><img src="jcaptcha"><input type='text' name='j_captcha_response' value=''>上面的src="jcaptcha"  就是调用了上面的servlet,text里是用户填写的确认验证码后台逻辑验证Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->Boolean isResponseCorrect =Boolean.FALSE;           //remenber that we need an id to validate!           String captchaId = httpServletRequest.getSession().getId();           //retrieve the response           String response = httpServletRequest.getParameter("j_captcha_response");           // Call the Service method            try {                isResponseCorrect = CaptchaServiceSingleton.getInstance().validateResponseForID(captchaId,                        response);            } catch (CaptchaServiceException e) {                 //should not happen, may be thrown if the id is not valid             }OK,大功告成了.


阅读全文(2608) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.066 second(s), page refreshed 144812577 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号