Struts Basic CAPTCHA Code Example

First Time Here?

Check the BotDetect Struts Captcha Quickstart for key integration steps.

The Struts Basic Captcha code example shows the most basic source code required to protect a Struts form with BotDetect CAPTCHA and validate the user input.

It can be used as a starting point when you are getting started with BotDetect.

Download the BotDetect Java library and run this example

Example Location

The Struts Basic CAPTCHA code example is included in the
examples/simple-api/bdc4-simple-api-struts-basic-captcha-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) already embeds all dependencies.

In case you are making this example from scratch in your IDE, you need to ensure botdetect.jar, botdetect-servlet.jar, botdetect-jsp20.jar, and hsqldb.jar are in the classpath.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="botDetect" uri=""%>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>BotDetect Java CAPTCHA Validation: Struts 2 Basic CAPTCHA Code Example</title>
    <link rel="stylesheet" href="stylesheet.css" type="text/css" />
    <s:form action="captchaAction" method="post"  theme="css_xhtml">
      <h1>BotDetect Java CAPTCHA Validation: <br /> Struts 2 Basic CAPTCHA Code Example</h1>
        <legend>Java CAPTCHA validation</legend>
        <label for="captchaCode">Retype the characters from the picture:</label>
        <!-- Adding BotDetect Captcha to the page -->    
        <botDetect:simpleCaptcha id="exampleCaptcha"/>
        <s:textfield name="captchaCode"/>
        <s:submit name="validateCaptchaButton" label="Validate" id="validateCaptchaButton"/>

We add Captcha protection to the form using the standard procedure.

package com.captcha.botdetect.examples.struts.basic_captcha.actions;

import com.captcha.botdetect.web.servlet.SimpleCaptcha;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

public class BasicCaptchaAction extends ActionSupport {
  private String captchaCode;

  public String getCaptchaCode() {
    return captchaCode;

  public void setCaptchaCode(String captchaCode) {
    this.captchaCode = captchaCode;
  public String execute() {
    return SUCCESS;
  public void validate() {
    SimpleCaptcha captcha = SimpleCaptcha.load(ServletActionContext.getRequest(), "exampleCaptcha");
    boolean isHuman = captcha.validate(captchaCode);
    if (!isHuman) {
      addFieldError("captchaCode", "Incorrect code");
    // reset captcha code textbox
    captchaCode = null;

In the validate method, we check the captcha code when it is submitted. The captcha code is stored in and retrieved from the field of this class via the accesser methods.


<?xml version="1.0" encoding="UTF-8" ?>
  "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

  <constant name="struts.devMode" value="true" />
  <constant name="struts.action.excludePattern" value="/botdetectcaptcha"/>
  <package name="basicCaptcha" extends="struts-default">
    <action name="captchaAction"
        class="com.captcha.botdetect.examples.struts.basic_captcha.actions.BasicCaptchaAction" method="execute">
      <result name="input">/index.jsp</result>
      <result name="success">/success.jsp</result>

In struts.xml configuration file, you also need to add exclude directive for SimpleCaptchaServlet to Struts 2 filter.


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



In WEB-INF/botdetect.xml, we configure captcha options for the exampleCaptcha captcha style name. You can find a full list of available Captcha configuration options and related instructions at the Captcha configuration options page.


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="struts2-jquery-showcase" version="2.4"
  <display-name>Struts 2 Web Application</display-name>


    <servlet-name>BotDetect Captcha</servlet-name>
    <servlet-name>BotDetect Captcha</servlet-name>

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

Please Note

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