亨利·黑兹利特:kaptcha简单的验证码生成工具

来源:百度文库 编辑:偶看新闻 时间:2024/05/05 19:43:16

kaptcha是一个非常实用的验证码生成工具,有了它,你可以生成各种样式的验证码,因为它是可配置的。

kaptcha工作的原理是调用com.google.code.kaptcha.servlet.KaptchaServlet,生成一个图片。同时将生成的验证码字符串放到HttpSession中。

kaptcha可以配置一下信息:

验证码的字体

验证码字体的大小

验证码字体的字体颜色

验证码内容的范围(数字,字母,中文汉字!)

验证码图片的大小,边框,边框粗细,边框颜色

验证码的干扰线(可以自己继承com.google.code.kaptcha.NoiseProducer写一个自定义的干扰线)

验证码的样式(鱼眼样式、3D、普通模糊……当然也可以继承com.google.code.kaptcha.GimpyEngine自定义样式)

……

详细信息请看下面的web.xml文件

下面介绍一下用法:

1.首先去官网下载jar:http://code.google.com/p/kaptcha/

2.建立一个web项目,导入

3.配置web.xml文件

其实就是配置com.google.code.kaptcha.servlet.KaptchaServlet

Java代码:


 xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
 version="2.4">
 
  Kaptcha
  com.google.code.kaptcha.servlet.KaptchaServlet
  
    Border around kaptcha. Legal values are yes or no.
   kaptcha.border
   no
  

  
   Color of the border. Legal values are r,g,b (and
    optional alpha) or white,black,blue.

   kaptcha.border.color
   red
  

  
   Thickness of the border around kaptcha. Legal values are
    > 0.

   kaptcha.border.thickness
   5
  

  
   Width in pixels of the kaptcha image.
   kaptcha.image.width
   80
  

  
   Height in pixels of the kaptcha image.
   kaptcha.image.height
   40
  

  
   The image producer.
   kaptcha.producer.impl
   com.google.code.kaptcha.impl.DefaultKaptcha
  

  
   The text producer.
   kaptcha.textproducer.impl
   com.google.code.kaptcha.text.impl.DefaultTextCreator
  

  
   The characters that will create the kaptcha.
   kaptcha.textproducer.char.string
   abcde2345678gfynmnpwx
  

  
   The number of characters to display.
   kaptcha.textproducer.char.length
   5
  

  
   A list of comma separated font names.
   kaptcha.textproducer.font.names
   Arial, Courier
  

  
   The size of the font to use.
   kaptcha.textproducer.font.size
   23
  

  
   The color to use for the font. Legal values are r,g,b.
   kaptcha.textproducer.font.color
   black
  

  
   The noise producer.
   kaptcha.noise.impl
   com.google.code.kaptcha.impl.NoNoise
  

  
   The noise color. Legal values are r,g,b.
   kaptcha.noise.color
   black
  

  
   The obscurificator implementation.
   kaptcha.obscurificator.impl
   com.google.code.kaptcha.impl.ShadowGimpy
  

  
   The background implementation.
   kaptcha.background.impl
   com.google.code.kaptcha.impl.DefaultBackground
  

  
   Ending background color. Legal values are r,g,b.
   kaptcha.background.clear.to
   white
  

  
   The word renderer implementation.
   kaptcha.word.impl
   com.google.code.kaptcha.text.impl.DefaultWordRenderer
  

  
   The value for the kaptcha is generated and is put into
    the HttpSession. This is the key value for that item in the session.

   kaptcha.session.key
   KAPTCHA_SESSION_KEY
  

  
   The date the kaptcha is generated is put into the
    HttpSession. This is the key value for that item in the session.

   kaptcha.session.date
   KAPTCHA_SESSION_DATE
  

 

 
  Kaptcha
  /Kaptcha.jpg
 

 
  KaptchaExample.jsp
 

4.编写KaptchaExample.jsp

这里用到了jQuery,所以添加了jQuery的库

js代码:



 
  <%@ page language="java" contentType="text/html; charset=UTF-8"%>
  
  Kaptcha Example
  
 
 
  Enter in the
  
     
     

     单击换图片
    
    
     


      

      验证码::
      
      

      
     
    
   
  
  

  

  

  

  <%
   String c = (String) session
     .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
   String parm = (String) request.getParameter("kaptchafield");
   
   System.out.println(c);
   out.println("Parameter: " + parm + " ? Session Key: " + c + " : ");
   if (c != null && parm != null)
   {
    if (c.equals(parm))
    {
     out.println("true");
    } else
    {
     out.println("false");
    }
   }
  %>