Java Arabic Captcha using SimpleCaptcha Library

Introduction

In this post, I will introduce how to use SimpleCaptcha to generate Arabic Captcha images to include in Arabic Portals.

How SimpleCaptcha works

The open source library introduced by SimpleCaptcha, is based on a servlet that generates the captcha image object, set it as attribute in the request(so it can be accessible to validate the user’s input) then writes it to the response as an PNG image.

The servlet could be mapped to a context that looks similar to image URL on the web like :

<servlet-mapping>
<servlet-name>SimpleCaptchaServlet</servlet-name>
<url-pattern>/captcha.jpg</url-pattern>
</servlet-mapping>

Then in the servlet/struts action or whatever place you need to validate the user’s input against the generated Captcha object, process the request or not. You get the captcha object from the request and validate it like this :

String userInputCaptcha = request.getParameter("userInputCaptcha");
nl.captcha.Captcha captchaObj = (Captcha) session.getAttribute("simpleCaptcha");
if (captcha != null && !captcha.isCorrect(userInputCaptcha.toLowerCase()))
{
// Process the request
}
else
{
// display captcha input error

}

Generating the Arabic Captcha

In order to generate Arabic Captcha, We need to develop three classes :

  • WordRenderer : We need to develop a class that implements the interface nl.captcha.text.renderer.WordRenderer to override the generation of the word to reverse it, as the Arabic words are read right-to-left not left-to-right as English 🙂
  • CaptchaServlet : We need to develop the captcha servlet that will use our developed ArabicWordRenderer class and passes the 28 Arabic Alphabet characters to the Default DefaultTextProducer.

The generated Arabic captcha looks like this screenshot :

Arabic Captcha

The source files

I included the source code of the 2 classes to be downloaded here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s