Jcaptcha 2.0 一个很好看的验证码程序

今天无意中找到一个很好看的验证码程序,向大家分享一下。
下载地址: http://jcaptcha.sourceforge.net/
web.xml如下:
<servlet>
		<servlet-name>jcaptcha</servlet-name>
		<servlet-class>com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet</servlet-class>
	</servlet>
	<servlet>
		<servlet-name>submit</servlet-name>
		<servlet-class>demo.SubmitActionServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>jcaptcha</servlet-name>
		<url-pattern>/jcaptcha.jpg</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>submit</servlet-name>
		<url-pattern>/submit.action</url-pattern>
	</servlet-mapping>

SubmitActionServlet.java
@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String userCaptchaResponse = req.getParameter("jcaptcha");
		boolean captchaPassed = SimpleImageCaptchaServlet.validateResponse(req,
				userCaptchaResponse);
		resp.setContentType("text/html");
		resp.setCharacterEncoding("utf-8");
		PrintWriter writer = resp.getWriter();
		StringBuffer sb = new StringBuffer();
		if (captchaPassed) {
			sb.append("<html><body>验证成功</body></html>");
			System.out.println("验证成功");
		} else {
			sb.append("<html>body><font color='red'>验证失败</font></body></html>");
			System.out.println("验证失败");
		}
		writer.println(sb.toString());
		writer.close();
	}

index.jsp
<form action="submit.action" method="post">
     <img src="jcaptcha.jpg" /> <input type="text" name="jcaptcha" value="" />
     <input type="submit"/>
</form>

效果:

你可能感兴趣的:(java,apache,tomcat,.net,servlet)