kaptcha一个很好用的验证码插件,java版的,很不错的一个插件,只用简单的在web.xml中设置几个属性,一个很漂亮的验证码就出来了。其他的参数都可以自己设置,最牛的就是提供了接口,可以自己定义哦。
[代码] html代码
1 | < form action = "submit.action" > |
2 | < img src = "kaptcha.jpg" /> < input type = "text" name = "kaptcha" value = "" /> |
[代码] web.xml
2 | < servlet-name >Kaptcha</ servlet-name > |
3 | < servlet-class >com.google.code.kaptcha.servlet.KaptchaServlet</ servlet-class > |
6 | < servlet-name >Kaptcha</ servlet-name > |
7 | < url-pattern >/kaptcha.jpg</ url-pattern > |
[代码] java代码
1 | String kaptchaExpected = (String)request.getSession() |
2 | .getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); |
3 | String kaptchaReceived = request.getParameter( "kaptcha" ); |
5 | if (kaptchaReceived == null || !kaptchaReceived.equalsIgnoreCase(kaptchaExpected)) |
7 | setError( "kaptcha" , "Invalid validation code." ); |
[代码] 根据点击显示验证码
1 | < img src = "/kaptcha" width = "200" id = "kaptchaImage" /> |
2 | < script type = "text/javascript" > |
4 | $('#kaptchaImage').click(function () { |
5 | $(this).attr('src', '/kaptcha.jpg?' + Math.floor(Math.random()*100) ); |