CAPTCHA Customization 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.

BotDetect 3.0 allows user-defined customization of many Captcha options on the application level through use of <context-param> deployment descriptor in WEB-INF/web.xml config file; some customizations also require page source changes.

Shown customizations include: CAPTCHA image dimensions and style, CAPTCHA code style and length and enabling sound & reload icons.

The WEB-INF/web.xml file used in this code example contains detailed descriptions and explanations of the many customizable options.

You can then use chosen customization options to configure BotDetect to precisely match your application requirements.

Downloaded Location

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


<%@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 Customization Example</title>
    <% Captcha captcha = Captcha.load(request, "customizedCaptcha"); %>
    <%= captcha.getStylesheetMarkup(pageContext.
        getServletContext().getContextPath()) %>
    <%= captcha.getClientScriptMarkup(pageContext.
        getServletContext().getContextPath()) %>
    <link rel="stylesheet" href="stylesheet.css" type="text/css"/>
    <form action="index.jsp" method="post">
      <h1>BotDetect CAPTCHA Customization Example</h1>
        <legend>CAPTCHA Validation</legend>
        <label for="captchaCodeTextBox" class="prompt">
            Retype the code from the picture:</label>
        // Adding BotDetect Captcha to the page
        <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>");

Although adding Captcha to a form follows standard procedure we used this example to show how to "manually" insert Html markup for BotDetect stylesheet and client script into page and how to prevent Captcha object to automatically render these markups.


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns=""
    <servlet-name>BotDetect Captcha</servlet-name>
    <servlet-name>BotDetect Captcha</servlet-name>

  <!-- BotDetect Captcha settings can be configured
    using <context-param> descriptors.

    For individual Captchas in application you can override these settings
    using Captcha component attributes without "LLBD_" prefix.
  <!-- Captcha image size
    You can specify in pixels either image width or height or both
    which will override Captcha image default size of 250px by 50 px -->

  <!-- Captcha image styles
    Set which image styles will be randomly used
    in Captcha image generation. Default is all styles.
    <param-value>ancientMosaic, halo, bullets, chess3D</param-value>

  <!-- Captcha code length
    Overrides default code lenght which is randomly
    selected between 4 and 6 characters.

  <!-- Captcha image format
    Set format of Captcha image

  <!-- Captcha code style
    Whether Captcha code will consist of
    alpha or numeric or alphanumeric characters.
    Default is alphanumeric.

  <!-- Sound enabled
    enable/disable sound icon -->

  <!-- Reload enabled
    enable/disable reload icon -->

  <!-- Help link enabled
    enable/disable help link (which cannot be disabled in free version)

  <!-- Help link mode
    Link to BotDetect Captcha page can be Captcha image ("image") or
    text below Captcha image ("text").


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.