分享一个Java开源项目—验证码kaptcha

1.下载Kaptcha项目
官网:http://code.google.com/p/kaptcha/
我现在下载的是:kaptcha-2.3.2.zip
2.把kaptcha-2.3.2.jar添加的web project 里的lib目录下
3.把kaptcha配置到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.font.color</param-name>
			<param-value>black</param-value>
		</init-param>

		<init-param>
			<param-name>kaptcha.textproducer.char.space</param-name>
			<param-value>5</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>Kaptcha</servlet-name>
		<url-pattern>/kaptcha.jpg</url-pattern>
	</servlet-mapping>


4.编写jsp页面
<img src="kaptcha.jpg" width="200" id="kaptchaImage" />
<form method="POST">
						<br>
						sec code:
						<input type="text" name="kaptcha">
						<br />
						<input type="submit" name="submit">
					</form>
<%
			String c = (String) session
					.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
			String parm = (String) request.getParameter("kaptcha");

			out.println("Parameter: " + parm + " ? Session Key: " + c + " : ");

			if (c != null && parm != null) {
				if (c.equals(parm)) {
					out.println("<b>true</b>");
				} else {
					out.println("<b>false</b>");
				}
			}
		%>

5.点击图片刷新验证码
<script type="text/javascript">
					$(function() {
						$('#kaptchaImage').click(function() {
							$(this).attr('src', 'kaptcha.jpg?' + Math.floor(Math.random() * 100));
						})
					});
					</script>
										<br />
Can't read the image? Click it to get a new one.

你可能感兴趣的:(java,C++,c,Google,C#)