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.

Downloaded Location

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

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="botDetect" uri="botDetect"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<f:view>
  <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
      <title>BotDetect CAPTCHA Basic JSF Example</title>
      <link rel="stylesheet" href="stylesheet.css" type="text/css"/>
    </head>
    <body>
      <h:form prependId="false">
        <h1>BotDetect CAPTCHA Basic JSF Example</h1>
        <fieldset>
          <legend>CAPTCHA Validation</legend>
          <h:outputLabel for="captchaCode" 
              value="Retype the code from the picture:"/>
          <!-- Adding BotDetect Captcha to the page -->
          <botDetect:jsfCaptcha id="basicExampleCaptcha"
              userInputClientId="captchaCode"
              binding="#{jsfBasicCaptchaExample.captcha}"/>
          <div class="validationDiv">
            <h:inputText id="captchaCode" 
                value="#{jsfBasicCaptchaExample.captchaCode}"/>
            <h:commandButton action="#{jsfBasicCaptchaExample.validate}" 
                value="Validate"/>
            <h:outputText value="Correct!" styleClass="correct" 
                rendered="#{jsfBasicCaptchaExample.correctLabelVisible}"/>
            <h:outputText value="Incorrect!" styleClass="incorrect" 
                rendered="#{jsfBasicCaptchaExample.incorrectLabelVisible}"/>
          </div>
        </fieldset>
      </h:form>
    </body>
  </html>
</f:view>

BotDetect custom JsfCaptcha tag generates the Html markup required to show the Captcha image and the Captcha sound / reload buttons. Attribute userInputClientId is optional and default (captchaCodeTextBox) value will be used if omitted.

BasicCaptcha.java

package botdetect.examples.jsf.basic_captcha.view;

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

@ManagedBean(name="jsfBasicCaptchaExample")
@RequestScoped
public class BasicCaptcha {

    private String captchaCode;
    private JsfCaptcha captcha;
    private boolean correctLabelVisible, incorrectLabelVisible;
    
    public BasicCaptcha() {
        this.captcha = new JsfCaptcha();
        captcha.setCodeStyle(CodeStyle.NUMERIC);
    }
    
    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 = "";
    }
    
}

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.

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.