引入kaptcha-2.3-jdk15.jar包
在web.xml中进行配置
<servlet>
<servlet-name>Kaptcha</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.char.space</param-name>
<param-value>5</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.length</param-name>
<param-value>5</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Kaptcha</servlet-name>
<url-pattern>/captcha.jpg</url-pattern>
</servlet-mapping>
jsp页面中
<img src="${pageContext.request.contextPath}/captcha.jpg" onclick="changeVerifyCode(this)" style="cursor:pointer;width:170px;height:50px;">
就能生成随机验证码
<script type="text/javascript">
function changeVerifyCode(img){
img.src = "captcha.jpg?"+Math.floor(Math.random()*100);
}
</script>点击切换