kaptcha-2.3.2.jar,一个java开源的验证码工具包,kaptcha是一个非常实用的验证码生成工具。
有了它,你可以生成各种样式的验证码,因为它是可配置的。
使用步骤
(0)导包 kaptcha-2.3.2.jar。
(1)在web.xml中配置kaptcha验证码的属性常量。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- ==========================kaptcha验证码配置========================== -->
<servlet>
<!-- 生成图片的Servlet -->
<servlet-name>kaptchaCode</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
<!-- 是否有边框 -->
<init-param>
<param-name>kaptcha.border</param-name>
<param-value>no</param-value>
</init-param>
<!-- 字体颜色 -->
<init-param>
<param-name>kaptcha.textproducer.font.color</param-name>
<param-value>red</param-value>
</init-param>
<!-- 图片宽度 -->
<init-param>
<param-name>kaptcha.image.width</param-name>
<param-value>135</param-value>
</init-param>
<!-- 图片高度 -->
<init-param>
<param-name>kaptcha.image.height</param-name>
<param-value>50</param-value>
</init-param>
<!-- 使用哪些字符生成验证码 -->
<init-param>
<param-name>kaptcha.textproducer.char.string</param-name>
<param-value>ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345679
</param-value>
</init-param>
<!-- 字符个数 -->
<init-param>
<param-name>kaptcha.textproducer.char.length</param-name>
<param-value>4</param-value>
</init-param>
<!-- 字体大小 -->
<init-param>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value>43</param-value>
</init-param>
<!-- 干扰线的颜色 -->
<init-param>
<param-name>kaptcha.noise.color</param-name>
<param-value>black</param-value>
</init-param>
<!-- 使用哪些字体 -->
<init-param>
<param-name>kaptcha.textproducer.font.names</param-name>
<param-value>Arial</param-value>
</init-param>
</servlet>
<!--====================== 映射kaptcha验证码的url ======================== -->
<servlet-mapping>
<servlet-name>kaptchaCode</servlet-name>
<url-pattern>/verifyCode.jpg</url-pattern>
</servlet-mapping>
</web-app>
(2)实现点击更换kaptcha验证码(jsp文件)。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"
+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html>
<head>
<base href="<%=basePath%>">
<title>登录页面</title>
</head>
<body>
<div>
<form action="<%=basePath %>user.do?opr=login" method="post" >
<h2>用户登录</h2>
<p>
验证码:<input type="text" name="verifyCode" />
<img alt="" id="verifyCode" src="<%=path %>/verifyCode.jpg?r=<%=Math.random() %>"
onclick="changeVerifyCode()" style="cursor: pointer;">
<a href="javascript:void(0)" onclick="changeVerifyCode()">看不清,换一张?</a>
</p>
<p>
<input type="submit" value="登录" />
</p>
</form>
</div>
<script type="text/javascript" src="<%=path%>/js/jquery-1.12.4.js"></script>
<script type="text/javascript">
function changeVerifyCode(){
$("#verifyCode").attr("src","<%=path %>/verifyCode.jpg?r="+Math.random());
}
</script>
</body>
</html>
(3)java程序获取kaptcha生成存放在session中的验证码。
String kaptchaVerifyCode = (String) req.getSession()
.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);