第一范文网 - 专业文章范例文档资料分享平台

java验证码:Patchca(简单而功能强大)

来源:用户分享 时间:2025/6/4 6:08:15 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

Patchca简单而功能强大

Jar包下载:http://patchca.googlecode.com/files/patchca-0.5.0.zip 源代码:http://patchca.googlecode.com/svn/trunk/

一:定义一个servlet生成图片(先引入jar包) (一):配置xml

patchca

com.ssh.servlet.CaptchaServlet

patchca /patchca.png

(二):servlet(自定义CaptchaService)

public class CaptchaServlet extends HttpServlet {

private static final long serialVersionUID = 4968328161261528097L; private static MyCaptchaService cs = null; @Override

public void init() throws ServletException { super.init();

//可直接使用ConfigurableCaptchaService,然后修改配置 cs = new MyCaptchaService(); }

@Override

public void destroy() { cs = null;

super.destroy(); }

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(\); response.setHeader(\, \);

HttpSession session = request.getSession(true); OutputStream os = response.getOutputStream();

String patchca= EncoderHelper.getChallangeAndWriteImage(cs, \, os); session.setAttribute(\, patchca); os.flush(); os.close();

}

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); }

private class MyCaptchaService extends AbstractCaptchaService { public MyCaptchaService() {

//文本内容

wordFactory = new MyWordFactory(); //字体

fontFactory = new RandomFontFactory(); //效果

textRenderer = new BestFitTextRenderer();

//背景

backgroundFactory = new SingleColorBackgroundFactory();

//字体颜色

colorFactory = new SingleColorFactory(new Color(25, 60, 170)); //样式(曲线波纹加干扰线)

filterFactory = new CurvesRippleFilterFactory(colorFactory); //图片长宽 width = 150; height = 50;

} }

private class MyWordFactory extends RandomWordFactory { public MyWordFactory() { }

}

//文本范围和长度

characters = \; minLength = 5; maxLength = 4;

(三)包中提供的配置:

一:ConfigurableCaptchaService: FontFactory 字体

默认:RandomFontFactory(Verdana,Tahoma,minSize、maxSize=45) WordFactory 单词范围和长度

默认:RandomWordFactory 单词6-6

其他:AdaptiveRandomWordFactory 自适应(mw字母宽度加长) ColorFactory

默认:SingleColorFactory 黑色 BackgroundFactory

默认:SingleColorBackgroundFactory 白色 TextRenderer 文本渲染

默认:BestFitTextRenderer FilterFactory 样式

默认:CurvesRippleFilterFactory(曲线波纹带干扰线,干扰线颜色为当前colorFactory) width/height 图片长宽(默认160*70)

使用:

public class CaptchaServlet extends HttpServlet {

private static final long serialVersionUID = 4968328161261528097L; private static ConfigurableCaptchaService cs = null; private static ColorFactory cf = null;

private static RandomWordFactory wf = null; private static Random r = new Random();

private static CurvesRippleFilterFactory crff = null; private static MarbleRippleFilterFactory mrff = null; private static DoubleRippleFilterFactory drff = null; private static WobbleRippleFilterFactory wrff = null; private static DiffuseRippleFilterFactory dirff = null; @Override

public void init() throws ServletException { super.init();

cs = new ConfigurableCaptchaService();

cf = new SingleColorFactory(new Color(25, 60, 170)); wf = new RandomWordFactory();

crff = new CurvesRippleFilterFactory(cs.getColorFactory()); drff = new DoubleRippleFilterFactory(); wrff = new WobbleRippleFilterFactory(); dirff = new DiffuseRippleFilterFactory(); mrff = new MarbleRippleFilterFactory(); cs.setWordFactory(wf); cs.setColorFactory(cf); cs.setWidth(120); cs.setHeight(50); }

@Override

public void destroy() { wf = null; cf = null; cs = null;

super.destroy(); }

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType(\); response.setHeader(\, \); wf.setMaxLength(5); wf.setMinLength(3);

HttpSession session = request.getSession(true); OutputStream os = response.getOutputStream(); //随机产生5种效果(filter) switch (r.nextInt(5)) { case 0:

cs.setFilterFactory(crff); break; case 1:

cs.setFilterFactory(mrff); break; case 2:

cs.setFilterFactory(drff); break; case 3:

cs.setFilterFactory(wrff); break; case 4:

cs.setFilterFactory(dirff); break; }

String patchca= EncoderHelper.getChallangeAndWriteImage(cs, \, os); session.setAttribute(\, patchca); os.flush(); os.close(); }

} }

二:需要自己构造SimpleCaptchaService(int width, int height, Color textColor,

Color backgroundColor, int fontSize, String[]fontNames, FilterFactory ff) {

backgroundFactory = new SingleColorBackgroundFactory(backgroundColor); wordFactory = new AdaptiveRandomWordFactory(); fontFactory = new RandomFontFactory(fontNames); textRenderer = new BestFitTextRenderer();

colorFactory = new SingleColorFactory(textColor); filterFactory = ff; this.width = width; this.height = height;

}

(四)FilterFactory样式

二:jsp页面访问servlet

\验证码\

style=\cursor:pointer;vertical-align:text-bottom;\

onclick=\>


验证码:





三:jsp页面取出session中的验证码

<%

//\和servlet中定义的session保存位置对应

String c = (String)session.getAttribute(\);

String parm = (String) request.getParameter(\);

out.println(\ + parm + \ + c + \ if (c != null && parm != null) { if (c.equalsIgnoreCase(parm)) { out.println(\); } else {

out.println(\); } } %>

);

java验证码:Patchca(简单而功能强大).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c21g4x0r5lz0a0pl1tyz8_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top