How To Add BotDetect CAPTCHA Protection to JSF Forms

Protecting your JSF forms with BotDetect Java Captcha slightly differs from JavaServer Pages protection but is still straightforward whether you use standard or Facelets presentation technology.

You can also see how BotDetect Captcha protection has been added to various kinds of JSF forms and projects by running the BotDetect Captcha JSF integration code examples coming with the BotDetect installation. You can also reuse the code example source code that fits your requirements.

First Time Here?

Check the BotDetect JSP Captcha Quickstart for key integration steps.

Here we will discuss only integration steps which differ from JavaServer Pages integration steps since including BotDetect Library in the Classpath and registering CaptchaServlet steps are the same regardless of framework.

Add BotDetect Java CAPTCHA Library Dependency

Here is how to add BotDetect Java CAPTCHA Library dependency in various dependency management scenarios:

To manually add BotDetect Captcha library to classpath, copy the following four jar files from BotDetect Java download package to application's WEB-INF/lib folder:

  • botdetect-4.0.beta1.jar
  • botdetect-servlet-4.0.beta1.jar
  • botdetect-jsp20-4.0.beta1.jar
  • botdetect-jsf20-4.0.beta1.jar

In case your application uses JSF 1.2 you will need to copy the botdetect-jsf12-4.0.beta1.jar file instead of botdetect-jsf20-4.0.beta1.jar file.

To share BotDetect Captcha among multiple applications, these BotDetect jar files above should be copied into 'lib' directory of web container or application server's domain.

Register CaptchaServlet

Update your application configuration (web.xml) file.

  <servlet-name>BotDetect Captcha</servlet-name>
  <servlet-name>BotDetect Captcha</servlet-name>

BotDetect CAPTCHA JavaServer Faces Tag

To protect your JSF form use dedicated jsfCaptcha tag.
Adding jsfCaptcha tag to JSF form is pretty straightforward but there are some differences between standard JSF (.jsp) and Facelets (.xhtml) presentation techologies:

  • declare taglib at the begining of the .jsp file:
  • <%@taglib prefix="botDetect" uri=""%>
  • add attribute prependId="false" to <h:form> opening tag
    this is not mandatory but enables some added functionality to jsfCaptcha tag
  • within the form insert:
  • <h:outputLabel for="captchaCode" 
      value="Retype the characters from the picture:"/>
    <botDetect:jsfCaptcha id="exampleCaptcha" 
    <h:inputText id="captchaCode" value="#{captchaExampleBean.captchaCode}"/>

When you open your form in a browser, the above declarations should render as:

BotDetect CAPTCHA added to an JSP page

If you are adding Captcha protection to multiple JSF forms in the same website, you should take care to give each one a unique name (e.g. "registrationCaptcha", "commentCaptcha", ...) in the Captcha object constructor.

In order to perform CAPTCHA validation jsfCaptcha tag must be bound with the corresponding property of the backing bean. This backing bean property should be of the JsfCaptcha type, and include both getter and setter access:

import botdetect.web.jsf.JsfCaptcha;

private JsfCaptcha captcha;

public JsfCaptcha getCaptcha() {
   return captcha;

public void setCaptcha(JsfCaptcha captcha) {
   this.captcha = captcha;

Check is the Visitor a Human on Form PostBack

Once the Captcha challenge is displayed on your form, the code processing form submissions can check if the Captcha was solved successfully and deny access to bots.

Add CAPTCHA Validation Logic to Backing Bean

When the form is submitted, the Captcha validation result must be checked and the protected action (user registration, comment posting, email sending, ...) only performed if the Captcha test was passed. For example, this code should be part of or invoked from backing bean method declared in <form> action attribute:

boolean isHuman = captcha.validate(captchaCode);
if (isHuman) {
  correctLabelVisible = true;
  incorrectLabelVisible = false;
} else {
  correctLabelVisible = false;
  incorrectLabelVisible = true;

Please Note

BotDetect Java Captcha Library v4.0.Beta1 is an in-progress port of BotDetect 4 Captcha, and we need you to guide our efforts towards a polished product. Please let us know if you encounter any bugs, implementation issues, or a usage scenario you would like to discuss.