JavaServer Faces Basic CAPTCHA Code Example

First Time Here?

Check the BotDetect JSP Captcha Quickstart for key integration steps.

The JSF Basic Captcha code example shows the most basic source code required to protect a JavaServer Faces form with BotDetect CAPTCHA and validate the user input.

It can be used as a starting point when you are first learning how to use BotDetect.

Download the BotDetect Java library and run this example

JavaServer Faces 2.0+

Downloaded Location

The JSF 2.0+ Basic CAPTCHA code example is included in the
examples/simple-api/bdc4-simple-api-jsf20-basic-captcha-example.war file of the download package. Deploying (unpacking) the file will create a standard JSP directory tree.

Running Example

This example's war file (in BotDetect download package) already embeds all dependencies.

In case you are making this example from scratch in your IDE, you need to ensure botdetect.jar, botdetect-servlet.jar, botdetect-jsp20.jar, botdetect-jsf20.jar, and hsqldb.jar (HyperSQL DB library) are in the classpath.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri=""%>
<%@taglib prefix="h" uri=""%>
<%@taglib prefix="botDetect" uri=""%>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
      <title>BotDetect Java CAPTCHA Validation: JSF Basic CAPTCHA Code Example</title>
      <link rel="stylesheet" href="stylesheet.css" type="text/css"/>
      <h:form prependId="false" styleClass="column">
        <h1>BotDetect Java CAPTCHA Validation:<br> JSF Basic CAPTCHA Code Example</h1>
          <legend>Java CAPTCHA validation</legend>
          <h:outputLabel for="captchaCode" value="Retype the characters from the picture:"/>
          <!-- Adding BotDetect Captcha to the page -->
          <botDetect:simpleJsfCaptcha id="basicExampleCaptcha"
              binding="#{jsfBasicCaptchaExample.captcha}" />
          <div class="validationDiv">
            <h:inputText id="captchaCode" value="#{jsfBasicCaptchaExample.captchaCode}"/>
            <h:commandButton action="#{jsfBasicCaptchaExample.validate}" value="Validate"/>
            <h:outputText value="Correct code" styleClass="correct" rendered="#{jsfBasicCaptchaExample.correctLabelVisible}"/>
            <h:outputText value="Incorrect code" styleClass="incorrect" rendered="#{jsfBasicCaptchaExample.incorrectLabelVisible}"/>

BotDetect custom simpleJsfCaptcha tag generates the Html markup required to show the Captcha image and the Captcha sound / reload buttons.

package com.captcha.botdetect.examples.jsf.basic_captcha.view;

import com.captcha.botdetect.web.jsf.SimpleJsfCaptcha;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

public class BasicCaptcha {

  private String captchaCode;
  private SimpleJsfCaptcha captcha;
  private boolean correctLabelVisible, incorrectLabelVisible;

  public BasicCaptcha() {

  public String getCaptchaCode() {
    return captchaCode;

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

  public SimpleJsfCaptcha getCaptcha() {
    return captcha;

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

  public boolean isCorrectLabelVisible() {
    return correctLabelVisible;

  public boolean isIncorrectLabelVisible() {
    return incorrectLabelVisible;

  public void validate(){
    // validate the Captcha to check we're not dealing with a bot
    boolean isHuman = captcha.validate(captchaCode);
    if (isHuman) {
      correctLabelVisible = true;
      incorrectLabelVisible = false;
    } else {
      correctLabelVisible = false;
      incorrectLabelVisible = true;
    this.captchaCode = "";


Binding JsfCaptcha tag from JSF form with backing bean property allows us to dynamically set Captcha properties and to perform Captcha validation in backing bean's form action method.


<?xml version="1.0" encoding="UTF-8"?>
<botdetect xmlns="" 



In WEB-INF/botdetect.xml, we configure captcha options for the basicExampleCaptcha captcha style name. You can find a full list of available Captcha configuration options and related instructions at the Captcha configuration options page.


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
  id="WebApp_ID" version="2.5">





    <servlet-name>Faces Servlet</servlet-name>

    <servlet-name>Faces Servlet</servlet-name>
    <servlet-name>Faces Servlet</servlet-name>

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

In WEB-INF/web.xml file we register SimpleCaptchaServlet used for BotDetect Captcha requests.

JavaServer Faces 1.2

The JSF 1.2 Basic CAPTCHA code example is included in the
examples/simple-api/bdc4-simple-api-jsf12-basic-captcha-example.war file of the download package.

Please Note

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