CAPTCHA Code Filtering Java Code Example

The Java Captcha customization code example shows how to customize BotDetect Java Captcha behavior and appearance.

First Time Here?

Check the BotDetect JSP Captcha Quickstart for key integration steps.

You can define rules about character sequences you want to avoid using in randomly generated CAPTCHA codes and simply pass them to the Captcha library.

In the web.xml standard deployment descriptor file we set <context-param> named LBD_bannedSequences which value consists of two and three characters sequences that should not be generated by Captcha. Additionally, in order to make code filtering effect in this example more apparent we reduced default character set by declaring all characters except 'A', 'B' and 'C' as banned sequences. Sequences must be separated by comma.

Downloaded Location

The CAPTCHA Code Filtering Java Example is included in examples folder of the download package as bdc3-captcha-code-filtering-example.war file. Deploying (unpacking) the file will create a standard JSP directory tree.


<%@page import="botdetect.CodeStyle"%>
<%@page import="botdetect.web.Captcha"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>BotDetect CAPTCHA Code Filtering Example</title>
    <link rel="stylesheet" href="stylesheet.css" type="text/css"/>
    <form action="index.jsp" method="post">
      <h1>BotDetect CAPTCHA Code Filtering Example</h1>
        <legend>CAPTCHA Validation</legend>
        <label for="captchaCodeTextBox" class="prompt">
          Retype the code from the picture:</label>
        // Adding BotDetect Captcha to the page
        Captcha captcha = Captcha.load(request, "filteredCaptcha");
        <div class="validationDiv">
          <input id="captchaCodeTextBox" type="text" 
              name="captchaCodeTextBox" />
          <input type="submit" name="validate" value="Validate" />&nbsp;
            // validate the Captcha to check we're not dealing with a bot
            boolean isHuman = captcha.validate(request, 
            if (isHuman) {
              // Captcha validation passed, perform protected action
              out.print("<span class=\"correct\">Correct.</span>");
            } else {
              // Captcha validation failed, show error message
              out.print("<span class=\"incorrect\">Incorrect!</span>");

In the form source, we follow the standard procedure for adding Captcha protection to a JSP form but we set code style to alpha characters only and reduce code length to 4 characters.


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

<web-app xmlns=""
    <servlet-name>BotDetect Captcha</servlet-name>
    <servlet-name>BotDetect Captcha</servlet-name>

Beside register CaptchaServlet in the WEB-INF/web.xml file we declare <context-param> tag with banned character sequences. This setting is global on application level and, contrary to other context params, cannot be overridden by Captcha custom tag attribute nor dynamically changed for a single Captcha instance.

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.