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.

index.jsp

<%@page import="botdetect.web.Captcha"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
  <head>
    <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"/>
  </head>
  <body>
    <form action="index.jsp" method="post">
      <h1>BotDetect CAPTCHA Customization Example</h1>
      <fieldset>
        <legend>CAPTCHA Validation</legend>
        <label for="captchaCodeTextBox" class="prompt">
            Retype the code from the picture:</label>
        <%
        // Adding BotDetect Captcha to the page
        captcha.setIncludeStyle(false);
        captcha.setIncludeClientScript(false);
        captcha.renderCaptchaMarkup(pageContext.getServletContext(), 
            pageContext.getOut());
        %>
        <div class="validationDiv">
          <input id="captchaCodeTextBox" type="text" 
              name="captchaCodeTextBox" />
          <input type="submit" name="validate" value="Validate" />&nbsp;
          <%
          if("POST".equalsIgnoreCase(request.getMethod())){
            // validate the Captcha to check we're not dealing with a bot
            boolean isHuman = captcha.validate(request, 
                request.getParameter("captchaCodeTextBox"));
            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>");
            }
          }
          %>
        </div>
      </fieldset>
    </form>
  </body>
</html>

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.

WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <session-config>
    <session-timeout>
      30
    </session-timeout>
  </session-config>
  <servlet>
    <servlet-name>BotDetect Captcha</servlet-name>
    <servlet-class>botdetect.web.http.CaptchaServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>BotDetect Captcha</servlet-name>
    <url-pattern>/botdetectcaptcha</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>CaptchaCustomizationAction</servlet-name>
    <servlet-class>
      botdetect.examples.jsp.customization.action.CaptchaCustomizationAction
    </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CaptchaCustomizationAction</servlet-name>
    <url-pattern>/captchaCustomizationAction</url-pattern>
  </servlet-mapping>

  <!-- 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 -->
  <context-param>
    <param-name>LBD_imageWidth</param-name>
    <param-value>350</param-value>
  </context-param>
  <context-param>
    <param-name>LBD_imageHeight</param-name>
    <param-value>100</param-value>
  </context-param>

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

  <!-- Captcha code length
    Overrides default code lenght which is randomly
    selected between 4 and 6 characters.
  -->
  <context-param>
    <param-name>LBD_codeLength</param-name>
    <param-value>8</param-value>
  </context-param>

  <!-- Captcha image format
    Set format of Captcha image
  -->
  <context-param>
    <param-name>LBD_imageFormat</param-name>
    <param-value>jpeg</param-value>
  </context-param>

  <!-- Captcha code style
    Whether Captcha code will consist of
    alpha or numeric or alphanumeric characters.
    Default is alphanumeric.
  -->
  <context-param>
    <param-name>LBD_codeStyle</param-name>
    <param-value>alpha</param-value>
  </context-param>

  <!-- Sound enabled
    enable/disable sound icon -->
  <context-param>
    <param-name>LBD_soundEnabled</param-name>
    <param-value>false</param-value>
  </context-param>

  <!-- Reload enabled
    enable/disable reload icon -->
  <context-param>
    <param-name>LBD_reloadEnabled</param-name>
    <param-value>true</param-value>
  </context-param>

  <!-- Help link enabled
    enable/disable help link (which cannot be disabled in free version)
  -->
  <context-param>
    <param-name>LBD_helpLinkEnabled</param-name>
    <param-value>true</param-value>
  </context-param>


  <!-- Help link mode
    Link to BotDetect Captcha page can be Captcha image ("image") or
    text below Captcha image ("text").
  -->
  <context-param>
    <param-name>LBD_helpLinkMode</param-name>
    <param-value>image</param-value>
  </context-param>


  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

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.