BotDetect Simple CAPTCHA Options: Server Side Persistence in Redis Database Code Example

The Java Simple Captcha options: Server side persistence in Redis database code example shows how to use Redis to store persist Captcha data instead of using default BotDetect HyperSQL Database persistence provider.

First Time Here?

Check the BotDetect Developer Crash Course for key integration steps.

As you may know Redis is an open source, in-memory data structure store used to improve data performance. Therefore Captcha data storage via Redis is neccessary in real world. This combination makes your application have better performance.

To use Redis to store persist Captcha data, BotDetect Java Simple Captcha provides you a simple way by declaring some Redis configuration info in WEB-INF/botdetect.xml file, such as: redis host, port, password, ssl.

BotDetect Java Simple Captcha uses Jedis as Redis java client, so you need to ensure Jedis jar library is in the classpath.

Download the BotDetect Java CAPTCHA Library and run this example

Downloaded Location

The Java Captcha options: Application config settings code example is included in the examples/simple-api/bdc4-simple-api-captcha-server-side-persistence-in-redis-database-example.war file of the download package. Deploying (unpacking) the file will create a standard JSP directory tree

Running Example

This example's war file (in BotDetect download package) has already embedded all dependencies.

In case you are making this example from scratch in your IDE, you need to be sure that botdetect.jar, botdetect-servlet.jar, jedis.jar, commons-pool2.jar are in the classpath.


<%@page import="com.captcha.botdetect.web.servlet.SimpleCaptcha"%>
<%@page trimDirectiveWhitespaces="true"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>BotDetect Java Simple CAPTCHA Options: Server Side Persistence in Redis Database Code Example</title>
  <link type="text/css" rel="stylesheet" href="stylesheet.css" />
  <form method="post" action="" class="column" id="form1">

    <h1>BotDetect Java Simple CAPTCHA Options: <br /> Server Side Persistence in Redis Database Code Example</h1>

      <legend>Java CAPTCHA validation</legend>
      <label for="captchaCode">Retype the characters from the picture:</label>

        // Adding BotDetect Captcha to the page
        SimpleCaptcha captcha = SimpleCaptcha.load(request, "redisCaptchaExample");
        String captchaHtml = captcha.getHtml();

      <div class="validationDiv">
        <input name="captchaCode" type="text" id="captchaCode" />
        <input type="submit" name="ValidateCaptchaButton" id="validateCaptchaButton" value="Validate" />

          // when the form is submitted
          if ("POST".equalsIgnoreCase(request.getMethod())) {
            // validate the Captcha to check we're not dealing with a bot
            boolean isHuman = captcha.validate(request.getParameter("captchaCode"));
            if (isHuman) {
              // Captcha validation passed, perform protected action
              out.print("<span class=\"correct\">Correct code</span>");
            } else {
              // Captcha validation failed, show error message
              out.print("<span class=\"incorrect\">Incorrect code</span>");

After including the BotDetect Captcha library, adding Captcha protection to the form is as simple as creating a SimpleCaptcha object instance and printing the getHtml(ServletContext context) method result. The same object instance provides easy Captcha validation using the validate(String userInput) method.


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


In WEB-INF/botdetect.xml configuration file, we tell BotDetect to use Redis as the BotDetect persistence provider instead of the default one (HyperSQL Database). You can find the detailed explanation about this at Captcha Redis persistence provider documentation.


<?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>

The example WEB-INF/web.xml file we register SimpleCaptchaServlet used for BotDetect Captcha requests..

Please Note

BotDetect Java Captcha Library v4.0.Beta3 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.