javaweb 验证码
谷歌验证码jar
kaptcha-2.3.2.jar
servlet
//获取验证码
String token= (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
String code = req.getParameter("code");
//2、检查 验证码是否正确
if (token.equalsIgnoreCase(code)) { }
} else { }
xml
<servlet>
<servlet-name>KaptchaServlet</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>KaptchaServlet</servlet-name>
<url-pattern>/kaptcha.jpg</url-pattern>
</servlet-mapping>
jsp
<script type="text/javascript">
$(function () {
//点击图片更换
$("#code_img").click(function () {
//this.src="${basePath}kaptcha.jpg";
this.src="${basePath}kaptcha.jpg?d="+new Date() ;
});
}
<label>验证码:</label>
<input type="text" name="code"/>
<img id="code_img" alt="" src="kaptcha.jpg">