请输入您要查询的百科知识:

 

词条 patchca
释义

Patchca是Piotr Piastucki写的一个java验证码,打包成jar文件发布,patchca简单但功能强大

五种内置的效果图

网页中使用

配置web.xml

<servlet>

<servlet-name>patchca</servlet-name>

<servlet-class>com.ssh.servlet.CaptchaServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>patchca</servlet-name>

<url-pattern>/patchca.png</url-pattern>

</servlet-mapping>

创建servlet

一:自定义CaptchaService(可灵活配置效果)

public class CaptchaServlet extends HttpServlet {

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("image/png");

response.setHeader("cache", "no-cache");

HttpSession session = request.getSession(true);

OutputStream os = response.getOutputStream();

String patchca= EncoderHelper.getChallangeAndWriteImage(cs, "png", os);

session.setAttribute("PATCHCA", 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 = "absdekmnowx23456789";

minLength = 5;

maxLength = 4;

}

}

}

二:使用包中提供的配置

(1)ConfigurableCaptchaService

默认配置说明:

FontFactory 字体 默认:RandomFontFactory(Verdana,Tahoma,minSize、maxSize=45)

WordFactory 单词范围和长度 默认:RandomWordFactory 单词6-6

ColorFactory 默认:SingleColorFactory 黑色

BackgroundFactory 默认:SingleColorBackgroundFactory 白色

TextRenderer 文本渲染 默认:BestFitTextRenderer

FilterFactory 样式 默认:CurvesRippleFilterFactory(曲线波纹带干扰线,干扰线颜色为当前colorFactory)

width/height 图片长宽(默认160*70)

(2)SimpleCaptchaService 构造时需要自己输入参数

jsp页面访问servlet

<table><tr>

<!-- patchca.png和servlet中的<url-pattern>对应 -->

<td><img src="patchca.png" alt="验证码"style="cursor:pointer;vertical-align:text-bottom;" onclick="this.src=this.src+'?'+Math.random();"></td>

<td valign="top">

<form method="POST"><br>

验证码:<input type="text" name="patchcafield"><br />

<input type="submit" name="submit">

</form></td></tr></table>

jsp页面取出session中的验证码

<%//"PATCHCA"和servlet中定义的session保存位置对应

String c = (String)session.getAttribute("PATCHCA");

String parm = (String) request.getParameter("patchcafield");

out.println("Parameter: " + parm + " ? Session Key: " + c + " : ");

if (c != null && parm != null) {

if (c.equalsIgnoreCase(parm)) {

out.println("<b>true</b>");

} else {

out.println("<b>false</b>");

}}%>

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2024/11/16 20:59:52