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.

Download the BotDetect Java library and run this example

JavaServer Faces 2.0+

Downloaded Location

The JSF 2.0+ Facelets CAPTCHA code example is included in the
examples/bdc4-jsf20-facelets-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, and botdetect-jsf20.jar are in the classpath.


<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns=""
    <title>BotDetect Java CAPTCHA Validation: Facelets JSF CAPTCHA Code Example</title>
    <h:outputStylesheet name="stylesheet.css" library="css"/>
    <h:form prependId="false" styleClass="column">
      <h1>BotDetect Java CAPTCHA Validation:<br/> Facelets JSF 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:jsfCaptcha id="faceletsExampleCaptcha"
        <div class="validationDiv">
          <h:inputText id="captchaCode"
          <h:commandButton action="#{jsfFaceletsCaptchaExample.validate}" value="Validate"/>
          <h:outputText value="Correct code" styleClass="correct"
          <h:outputText value="Incorrect code" 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 com.captcha.botdetect.examples.jsf.facelets_captcha.view;

import com.captcha.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.


<?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>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 CaptchaServlet used for BotDetect Captcha requests.

JavaServer Faces 1.2

The JSF 1.2 Facelets CAPTCHA code example is included in the
examples/bdc4-jsf12-facelets-captcha-example.war file of the download package.

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.