JavaServer Faces Facelets CAPTCHA Code Example

First Time Here?

Check the BotDetect JSP Captcha Quickstart for key integration steps.

The JSF Facelets Captcha code example shows how to protect a JavaServer Faces form with Facelets presentation technology using BotDetect CAPTCHA and validate the user input.

This example is very similar to JSF Basic Captcha Example.

Downloaded Location

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


<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
<html xmlns=""
    <title>BotDetect CAPTCHA Facelets JSF Example</title>
    <h:outputStylesheet name="stylesheet.css" library="css"/>
    <h:form prependId="false">
      <h1>BotDetect CAPTCHA Facelets JSF Example</h1>
        <legend>CAPTCHA Validation</legend>
        <h:outputLabel for="captchaCodeTextBox" 
            value="Retype the code from the picture:"/>
        <!-- Adding BotDetect Captcha to the page -->
        <botDetect:jsfCaptcha id="faceletsExampleCaptcha" 
        <div class="validationDiv">
          <h:inputText id="captchaCodeTextBox" 
          <h:commandButton action="#{jsfFaceletsCaptchaExample.validate}" 
          <h:outputText value="Correct!" styleClass="correct" 
          <h:outputText value="Incorrect!" styleClass="incorrect" 

Instead of a taglib declaration, when using Facelets we have to declare the appropriate xmlns attribute of the opening <html> tag at the top of form source.

package botdetect.examples.jsf.facelets_captcha.view;

import botdetect.web.jsf.JsfCaptcha;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

public class FaceletsCaptcha {

    private String captchaCode;
    private JsfCaptcha captcha;
    private boolean correctLabelVisible, incorrectLabelVisible;
    public FaceletsCaptcha() {
    public String getCaptchaCode() {
        return captchaCode;

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

    public JsfCaptcha getCaptcha() {
        return captcha;

    public void setCaptcha(JsfCaptcha 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 = "";

As you can see the backing bean code is exactly the same as in JSF Basic Captcha Example.

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.