java验证码组件kaptcha使用方法
程序员文章站
2024-02-15 09:32:10
使用方法:项目中导入kaptcha-2.3.jar包在web.xml里面新增: 复制代码 代码如下:&nb...
使用方法:
项目中导入kaptcha-2.3.jar包
在web.xml里面新增:
复制代码 代码如下:
<!-- 登陆验证码kaptcha -->
<servlet>
<servlet-name>kaptcha</servlet-name>
<servlet-class>com.google.code.kaptcha.servlet.kaptchaservlet</servlet-class>
<init-param>
<param-name>kaptcha.producer.impl</param-name>
<param-value>com.google.code.kaptcha.impl.defaultkaptcha </param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.color</param-name>
<param-value>black</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.space</param-name>
<param-value>10</param-value>
</init-param>
<init-param>
<param-name>kaptcha.border</param-name>
<param-value>no</param-value>
</init-param>
<init-param>
<param-name>kaptcha.border.thickness</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.size</param-name>
<param-value>35</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.image.width</param-name>
<param-value>150</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.background.clear.from</param-name>
<param-value>255,255,255</param-value>
</init-param>
<init-param>
<param-name>kaptcha.background.clear.to</param-name>
<param-value>255,255,255</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.char.string</param-name>
<param-value>0123456789</param-value>
</init-param>
<init-param>
<param-name>kaptcha.textproducer.font.names</param-name>
<param-value>arial black</param-value>
</init-param>
<init-param>
<param-name>kaptcha.noise.impl</param-name>
<param-value>com.google.code.kaptcha.impl.nonoise</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>kaptcha</servlet-name>
<url-pattern>/kaptcha.jpg</url-pattern>
</servlet-mapping>
在页面上新增
复制代码 代码如下:
<img id="img" src="kaptcha.jpg" width="90" height="25" onclick="refresh();" alt="点击更换验证码"/>
function refresh() {
document.getelementbyid('img').src="kaptcha.jpg?"+math.random();
}
在action里获取验证码id:
复制代码 代码如下:
request.getsession().getattribute(com.google.code.kaptcha.constants.kaptcha_session_key);