全部博文(2065)
分类: Java
2010-03-15 23:38:02
JCaptcha使用指南
[整理人:遥方 整理时间:
相关的LIB包我已上传至:
http://blogimg.chinaunix.net/blog/upfile2/100315232914.rar
将这些JAR包导入到项目中去。然后编写的代码如下
Index.jsp代码:
<body>
<form action="submit.action"
method="post">
<img src="jcaptcha.jpg" />
<input type="text" name="jcaptchas"
value="submit" />
<input type="text" name="jcaptcha"
/>
<input type="submit" name="jcaptcha"
value="submit" />
form>
body>
Web.xml配置
<servlet>
<servlet-name>jcaptchaservlet-name> <servlet-class>com.octo.captcha.module.servlet.image.SimpleImageCaptchaServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>jcaptchaservlet-name>
<url-pattern>/jcaptcha.jpgurl-pattern>
servlet-mapping>
<servlet>
<servlet-name>submitservlet-name>
<servlet-class>cn.image.SubmitServletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>submitservlet-name>
<url-pattern>/submit.actionurl-pattern>
servlet-mapping>
这样的话就会出现验证码图片。
保存的servlets代码如下:
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("3C//DTD
HTML 4.01 Transitional//EN\">");
out.println("");
out.println("
out.println(" ");
String userCaptchaResponse =
request.getParameter("jcaptcha");
boolean
captchaPassed = SimpleImageCaptchaServlet.validateResponse(request,
userCaptchaResponse);
if(captchaPassed){
out.print("right!");
}else{
out.print("error!");
}
out.println("
");
out.println("");
out.flush();
out.close();
}
以后可以使用这个开源的东西制作网站验证码出来!