JavaServer Faces Validator CAPTCHA Code Example

First Time Here?

Check the BotDetect JSP Captcha Quickstart for key integration steps.

The JSF Validator Captcha code example shows how to integrate BotDetect CAPTCHA validation with standard JavaServer Faces page validation functionality and other validator controls.

Downloaded Location

The JSF Validator CAPTCHA Example is included in examples folder of the download package as bdc3-jsf-validator-captcha-example.war file. Deploying (unpacking) the file will create a standard JSP directory tree.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri=""%>
<%@taglib prefix="h" uri=""%>
<%@taglib prefix="botDetect" uri="botDetect"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
      <title>BotDetect JSF Validator CAPTCHA Example</title>
      <link rel="stylesheet" href="stylesheet.css" type="text/css"/>
      <h:form prependId="false">
        <h1>BotDetect JSF Validator CAPTCHA Example</h1>
          <legend>CAPTCHA Validation</legend>
          <h:outputLabel for="inputName" value="Name:"/>
          <h:inputText id="inputName" validatorMessage="Missing name!">
            <f:validateLength minimum="2"/>
          <h:message for="inputName" styleClass="incorrect"/>
          <h:outputLabel for="captchaCodeTextBox" 
              value="Retype the code from the picture:"/>
          <!-- Adding BotDetect Captcha to the page -->
          <botDetect:jsfCaptcha id="exampleCaptcha"/>
          <div class="validationDiv">
            <h:inputText id="captchaCodeTextBox" 
                validatorMessage="Incorrect CAPTCHA code!">
              <f:validator validatorId="exampleValidator"/>
              <f:attribute name="captchaId" value="exampleCaptcha"/>
          <h:commandButton action="#{validatorExample.submit}" value="Submit"/>
          <h:message for="captchaCodeTextBox" styleClass="incorrect"/>
          <h:outputText value="Validation passed." styleClass="correct" 

In order to use a custom JSF Validator to validate user Captcha code input, we have to declare a validator of the Captcha code text box (<f:validator validatorId="exampleValidator"/>). Also, we have to add Captcha Id as component's attribute to Captcha code text box (<f:attribute name="captchaId" value="exampleCaptcha"/>).

package botdetect.examples.jsf.validator;

import botdetect.web.Captcha;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.servlet.http.HttpServletRequest;

public class ExampleValidator implements Validator{

  public void validate(FacesContext context, UIComponent component, 
      Object value) throws ValidatorException {
    String captchaId = (String)component.getAttributes().get("captchaId");
    HttpServletRequest request = 
    // validate the Captcha to check we're not dealing with a bot
    Captcha captcha = Captcha.load(request, captchaId);
    boolean isHuman = captcha.validate(request, (String)value);
    if (!isHuman) {
      FacesMessage message = new FacesMessage();
      throw new ValidatorException(message);


In validaton method of the JSF custom validator first we have to obtain Captcha id from component's attributes.

Using Captcha id instantiate Captcha object and validate user input in usual way.

package botdetect.examples.jsf.validator.view;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

public class ValidatorExample {
  private String captchaCode;
  private boolean validated;

  public ValidatorExample() {

  public String getCaptchaCode() {
    return captchaCode;

  public void setCaptchaCode(String captchaCode) {
    this.captchaCode = captchaCode;

  public boolean isValidated() {
    return validated;

  public void submit(){
    captchaCode = null;
    validated = true;

Managed bean has no specific BotDetect Captcha code since all validation is done in custom validator. Form's action method submit() is invoked after Captcha validation is passed so property validated (which is used to display message on the form) is set true.

To run this example, botdetect.jar must be in the classpath.

Please Note

BotDetect Java Captcha Library v4.0.Beta3.7 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.