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.

index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:botDetect="botDetectFacelets">
  <h:head>
    <title>BotDetect CAPTCHA Facelets JSF Example</title>
    <h:outputStylesheet name="stylesheet.css" library="css"/>
  </h:head>
  <h:body>
    <h:form prependId="false">
      <h1>BotDetect CAPTCHA Facelets JSF Example</h1>
      <fieldset>
        <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" 
            binding="#{jsfFaceletsCaptchaExample.captcha}"/>
        <div class="validationDiv">
          <h:inputText id="captchaCodeTextBox" 
              value="#{jsfFaceletsCaptchaExample.captchaCode}"/>
          <h:commandButton action="#{jsfFaceletsCaptchaExample.validate}" 
              value="Validate"/>
          <h:outputText value="Correct!" styleClass="correct" 
              rendered="#{jsfFaceletsCaptchaExample.correctLabelVisible}"/>
          <h:outputText value="Incorrect!" styleClass="incorrect" 
              rendered="#{jsfFaceletsCaptchaExample.incorrectLabelVisible}"/>
        </div>
      </fieldset>
    </h:form>
  </h:body>
</html>

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.

FaceletsCaptcha.java

package botdetect.examples.jsf.facelets_captcha.view;

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

@ManagedBean(name="jsfFaceletsCaptchaExample")
@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.