java驗證碼組件kaptcha應用辦法。本站提示廣大學習愛好者:(java驗證碼組件kaptcha應用辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是java驗證碼組件kaptcha應用辦法正文
應用辦法:
項目中導入kaptcha-2.3.jar包
在web.xml外面新增:
<!-- 上岸驗證碼Kaptcha -->
<servlet>
<servlet-name>Kaptcha</servlet-name>
<servlet-class>com.谷歌.code.kaptcha.servlet.KaptchaServlet</servlet-class>
<init-param>
<param-name>kaptcha.producer.impl</param-name>
<param-value>com.谷歌.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.谷歌.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.谷歌.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);