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 :
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
// 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.WordRendererto 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 :
The source files
I included the source code of the 2 classes to be downloaded here.