kaptcha驗證碼使用

來源:互聯網
上載者:User

官方地址:https://code.google.com/p/kaptcha/w/list

1、把下載的kaptcha-2.3.2.jar添加到lib中

2、配置web.xml增加servlet

<servlet>        <servlet-name>Kaptcha</servlet-name>        <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class></servlet><servlet-mapping>        <servlet-name>Kaptcha</servlet-name>        <url-pattern>/kaptcha.jpg</url-pattern></servlet-mapping>

3、在jsp頁面中

<form action="submit.action" method="post"><img src="kaptcha.jpg" id="kaptchaImage" /> <input type="text"name="kaptcha" value="" /> <input type="submit" name="submit"value="submit" /></form>

其中src="kaptcha.jpg"會被定位到servlet上

4、KaptchaServlet會把驗證碼設定到session中,可以如下方式擷取

String kaptchaExpected = (String)request.getSession()    .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

5、如果是struts2的action,可以如下方式擷取

String kaptchaExpected = (String)ActionContext.getContext().getSession().get(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);

6、如果想設定點擊圖片更換驗證碼,可以加上如下js,需要jquery

<script type="text/javascript">    $(function(){        $('#kaptchaImage').click(function () { $(this).attr('src', '/kaptcha.jpg?' + Math.floor(Math.random()*100) ); })    });</script>

7、或者來點fade效果

<script type="text/javascript">$(function() {$('#kaptchaImage').click(function() {$(this).hide().attr('src','kaptcha.jpg?' + Math.floor(Math.random() * 100)).fadeIn();});});</script>

8、驗證碼圖片還有很多參數設定

設定方法,在web.xml的servlet中

<init-param><param-name>kaptcha.border</param-name><param-value>no</param-value></init-param>
Constant 描述 預設值
kaptcha.border 圖片邊框,合法值:yes , no yes
kaptcha.border.color 邊框顏色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. black
kaptcha.border.thickness 邊框厚度,合法值:>0 1
kaptcha.image.width 圖片寬 200
kaptcha.image.height 圖片高 50
kaptcha.producer.impl 圖片實作類別 com.google.code.kaptcha.impl.DefaultKaptcha
kaptcha.textproducer.impl 文本實作類別 com.google.code.kaptcha.text.impl.DefaultTextCreator
kaptcha.textproducer.char.string 文本集合,驗證碼值從此集合中擷取 abcde2345678gfynmnpwx
kaptcha.textproducer.char.length 驗證碼長度 5
kaptcha.textproducer.font.names 字型 Arial, Courier
kaptcha.textproducer.font.size 字型大小 40px.
kaptcha.textproducer.font.color 字型顏色,合法值: r,g,b  或者 white,black,blue. black
kaptcha.textproducer.char.space 文字間隔 2
kaptcha.noise.impl 幹擾實作類別 com.google.code.kaptcha.impl.DefaultNoise
kaptcha.noise.color 幹擾顏色,合法值: r,g,b 或者 white,black,blue. black
kaptcha.obscurificator.impl 圖片樣式:
水紋com.google.code.kaptcha.impl.WaterRipple
魚眼com.google.code.kaptcha.impl.FishEyeGimpy
陰影com.google.code.kaptcha.impl.ShadowGimpy
com.google.code.kaptcha.impl.WaterRipple
kaptcha.background.impl 背景實作類別 com.google.code.kaptcha.impl.DefaultBackground
kaptcha.background.clear.from 背景色彩坡形,開始顏色 light grey
kaptcha.background.clear.to 背景色彩坡形,結束顏色 white
kaptcha.word.impl 文字渲染器 com.google.code.kaptcha.text.impl.DefaultWordRenderer
kaptcha.session.key session key KAPTCHA_SESSION_KEY
kaptcha.session.date session date KAPTCHA_SESSION_DATE

9、

水紋效果

魚眼效果


陰影製作效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.