21.12.09

Java Captcha

เพื่อนติดปัญหาช่วยแก้เกี่ยวกับ captcha ลองหาดูพบว่า jcaptcha ใช้งานได้ค่อนข้างดี และ implement ไม่ยากนำไปผูกกับ framework ได้อย่างลงตัว

เริ่มแรกไปโหลด library jCaptcha และ library ที่เกี่ยวข้องมาให้เรียบร้อย
  • commons-collections-3.2
  • commons-logging-1.0.4
  • filters-2.0.235
  • jcaptcha-2.0-alpha-1-SNAPSHOT
  • jcaptcha-api-1.0
  • jcaptcha-integration-simple-servlet-2.0-alpha-1-SNAPSHOT

web.xml ระบุว่า jcaptcha ที่จะทำหน้าที่เป็นตัวสร้างภาพ captcha ตัว class ใด และจะเรียกใช้ผ่าน url อย่างไร


jcaptcha
com.octo.captcha.servlet.image.SimpleImageCaptchaServlet


jcaptcha
/jcaptcha.jpg


ตัวอย่างการเรียกใช้งาน






ฝั่ง server รับไปประมวลผล

PrintWriter pw = response.getWriter();
String userCaptchaResponse = request.getParameter("jcaptcha");
boolean captchaPassed = SimpleImageCaptchaServlet.validateResponse(request, userCaptchaResponse);
if(captchaPassed){
pw.println("good job");
}else{
pw.println("fail");
}

เสร็จเรียบร้อย run ได้เลย

No comments:

Post a Comment