JavaServer Pages Basic CAPTCHA Code Example

First Time Here?

Check the BotDetect JSP Captcha Quickstart for key integration steps.

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

It can be used as a starting point when you are first learning how to use BotDetect.

Downloaded Location

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

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="botdetect.web.Captcha"%>
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>BotDetect CAPTCHA Basic JSP Example</title>
    <link rel="stylesheet" href="stylesheet.css" type="text/css"/>
  </head>
  <body>
    <form action="index.jsp" method="post">
      <h1>BotDetect CAPTCHA Basic JSP Example</h1>
      <fieldset>
        <legend>CAPTCHA Validation</legend>
        <label for="captchaCode" class="prompt">
        Retype the code from the picture:</label>
        <%
        // Adding BotDetect Captcha to the page
        Captcha captcha = Captcha.load(request, "basicCaptcha");
        captcha.setUserInputClientId("captchaCode");
        captcha.renderCaptchaMarkup(pageContext.getServletContext(), 
        pageContext.getOut());
        %>
        <div class="validationDiv">
          <input id="captchaCode" type="text" name="captchaCode" />
          <input type="submit" name="validate" value="Validate" />&nbsp;
          <%
          // 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,
                  request.getParameter("captchaCode"));
            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>

After including the BotDetect Captcha library, adding Captcha protection to the form is as simple as creating a Captcha object instance and printing the Html function result. The same object instance provides easy Captcha validation using the Validate function.

To run this example, botdetect.jar must be in the classpath.

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.