1.首先需要导入jar包
2.配置web.xml
<servlet>
<servlet-name>Kaptcha</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>
<init-param>
<param-name>kaptcha.image.width</param-name><!-- 验证图片宽度 -->
<param-value>150</param-value>
</init-param>
<init-param>
<param-name>kaptcha.image.height</param-name><!-- 验证图片高度 -->
<param-value>35</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.size</param-name><!-- 验证码文本字符大小 -->
<param-value>30</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.color</param-name><!-- 验证码噪点颜色 -->
<param-value>GREEN</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Kaptcha</servlet-name>
<url-pattern>/Kaptcha.jpg</url-pattern>
</servlet-mapping>
3.编辑jsp页面
<script type="text/javascript">
function changeR(node){
//用于点击时产生不同的验证码
node.src = "Kaptcha.jpg?time="+new Date().getTime();
}
</script>
<tr>
<td>验证码:</td>
<td><input type="text" id="index_code" name="verifyCode"></td>
<td><img id="imgObj" alt="验证码" src="Kaptcha.jpg" οnclick="changeR(this)" style="cursor:pointer;"/></td>
</tr>
4.jar包地址http://pan.baidu.com/s/1bICWpC