How To add BotDetect CAPTCHA protection to Java forms

Important note: since this sample uses a version of BotDetect CAPTCHA implemented as a COM component, both your servers and development machines must use a Windows family OS. At the moment we don't support Java websites running on other operating systems.

Requirements

Other versions should do just fine but these were used in our example. Extract and install them to a folder of your choosing.

Creating the Java CAPTCHA web project

Create a new Dynamic Web Project by clicking "File\New\Project"

How To add BotDetect CAPTCHA protection to Java forms: screenshot 6

In the "Project name", type "captchaWeb", select "Apache Tomcat v6.0" in "Target Runtime" and click "Finish".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 7

At the prompt click "Yes" to switch to Java EE Perspective

How To add BotDetect CAPTCHA protection to Java forms: screenshot 8

In the Project Explorer navigate to "Java Resources: src", right click on it and choose "New\Package". In the "Name" field, type "org.lanapsoft.captcha" and click "Finish".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 9

Right click on the new package and select "New\Class". In the "Name" field type "CaptchaServlet" and click "Finish".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 10

Paste in the following code:

package org.lanapsoft.captcha;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.SafeArray;
import com.jacob.com.Variant;

public class CaptchaServlet extends HttpServlet {

  public void service(HttpServletRequest request, HttpServletResponse 
    response) throws ServletException, IOException {
		
    ActiveXComponent activeXComponent = null;
		
    String command = null;
    String[] params = null;
    String queryString = (String) request.getQueryString();

    if (queryString != null) {
      params = queryString.split("=");
      command = params[0];
    }

    HttpSession session = request.getSession(true);
    activeXComponent = (ActiveXComponent) 
      session.getAttribute("captchaComponent");
		
    if (activeXComponent == null) {
      activeXComponent = new ActiveXComponent("Lanap.BotDetect");
      session.setAttribute("captchaComponent", activeXComponent);
    }

    if ("CreateImage".equals(command)) {
      activeXComponent.setProperty("CodeLength", 5);
      activeXComponent.setProperty("Format", "Jpeg");
      activeXComponent.setProperty("ImageWidth", 300);
      activeXComponent.setProperty("ImageHeight", 100);

      Variant createImage = activeXComponent.invoke("CreateImage");

      SafeArray createImageSafeArray = createImage.toSafeArray();
      byte createImageByteArray[] = createImageSafeArray.toByteArray();
			
      Variant generatedValue = activeXComponent.invoke("GetValue");
      session.setAttribute("generatedValue", generatedValue);
			
      Variant generatedHashValue = 
        activeXComponent.invoke("GetHashValue");
      session.setAttribute("generatedHashValue", generatedHashValue);

      ServletOutputStream servletOutputStream = response
        .getOutputStream();
      servletOutputStream.write(createImageByteArray);
      servletOutputStream.flush();
      servletOutputStream.close();
			
    } else if ("CreateSound".equals(command)) {
      Variant generatedValue = 
        (Variant)session.getAttribute("generatedValue");
      Variant createSound = 
        activeXComponent.invoke("CreateSoundFromCode", 
          generatedValue);

      SafeArray createImageSafeArray = createSound.toSafeArray();
      byte createImageByteArray[] = createImageSafeArray.toByteArray();

      ServletOutputStream servletOutputStream = response
        .getOutputStream();
      servletOutputStream.write(createImageByteArray);
      servletOutputStream.flush();
      servletOutputStream.close();
			
    } else if ("userCode".equals(command)) {
      String userCodeParam = params[1];

      Variant userCode = new Variant();
      userCode.putString(userCodeParam);

      Variant generatedHashValue = (Variant)
        session.getAttribute("generatedHashValue");
      Variant validate = activeXComponent.invoke("Validate", userCode, 
        generatedHashValue);

      boolean success = validate.getBoolean();

      if (success) {
        response.sendRedirect("/captchaWeb/success.htm");
      } else {
        response.sendRedirect("/captchaWeb/failure.htm");
      }
    }
  }
}

To resolve compile errors right click on the project and select "Properties". Select "Java Build Path" and click on "Add Library..."

How To add BotDetect CAPTCHA protection to Java forms: screenshot 11

On the next screen select "User Library" and click "Next".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 12

Click "User Libraries".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 13

Click "New".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 14

Type in "jacob" for "User library name" and press "OK".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 15

Click "Add jars", navigate to the "Jacob" folder and select "jacob.jar".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 16

Click "OK", "Finish" and "OK". There should be no errors in the project.

In the Project Explorer, navigate to "WebContent\WEB-INF", open "web.xml" and switch to the "Source" tab.

How To add BotDetect CAPTCHA protection to Java forms: screenshot 17

Paste in the following code:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 
  xmlns="http://java.sun.com/xml/ns/j2ee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>captchaWeb</display-name>
  <welcome-file-list>
    <welcome-file>index.htm</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>captchaWeb</servlet-name>
    <servlet-class>org.lanapsoft.captcha.CaptchaServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>captchaWeb</servlet-name>
    <url-pattern>/captcha/*</url-pattern>
  </servlet-mapping>
</web-app>

In the Project Explorer, right click on "WebContent" and choose "New\File". Type in "index.htm" and click "Finish".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 18

Repeat the step and create files "success.htm" and "failure.htm".

Paste in the following code:

index.htm

<html>
<body>
  <form name="searchForm" method="get" action="/captchaWeb/captcha">
    <table>
      <tr>
        <td>
          <img src="/captchaWeb/captcha?CreateImage" />
        </td>
      </tr>
      <tr>
        <td>
          <b>Sound:</b>
          <a href="/captchaWeb/captcha?CreateSound">
            <img src="speaker.gif" alt="Speak the code" />
          </a>
        </td>
      </tr>
      <tr>
        <td colspan="2" align="right">
          <input type="text" name="userCode" />
        </td>
      </tr>
      <tr>
        <td colspan="2" align="right">
          <input type="submit" value="Submit" />
        </td>
      </tr>
    </table>
  </form>
</body>
</html>

success.htm

<html>
  <h1>success</h1>
</html>

failiure.htm

<html>
  <h1>failure</h1>
</html>

Navigate to "WebContent\WEB-INF\lib" in the Project Explorer and paste in the "jacob-1.14.3-x86.dll" file.

Right click on the "captchaWeb" project, select "Properties", then "Java EE Module Dependencies" and check the "jacob" library. Press "OK".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 19

Go to "Window\Show View" and select "Servers". In the "Servers" view, right click and select "New\Server".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 20

Select "Tomcat v6.0 Server" and click "Next".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 21

On the "Add and Remove Projects", select "captchaWeb" and click "Add", then "Finish".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 22

In the "Servers" view, right click on the server, select "Start". Now go to "Run\Run Configurations", select "Tomcat v6.0 Server at localhost" and on the tab "Arguments", add VM argument -Djava.library.path=“location_of_your_workspace\ captchaWeb\WebContent\WEB-INF\lib". Click "Apply" and "Close".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 23

Restart the server, open the browser and type the URL http://localhost:8080/captchaWeb/index.htm.

How To add BotDetect CAPTCHA protection to Java forms: screenshot 24

If you enter correct values you are forwarded to success.htm

How To add BotDetect CAPTCHA protection to Java forms: screenshot 25

Otherwise, you are forwarded to failure.htm

How To add BotDetect CAPTCHA protection to Java forms: screenshot 26

Setting up the Java environment

If you don't have Java EE Developer Tools installed in Eclipse, go to Help\Software Updates... In the "Available Software" tab select "Java EE Developer Tools" and click "Install".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 1

In Eclipse, go to "Window\Preferences" and then to "Java\Installed JREs" to check the selected JRE.

How To add BotDetect CAPTCHA protection to Java forms: screenshot 2

If your JRE is not there, click "Add..." and navigate to its folder.

Under "Server\Runtime Environments" click "Add..."

How To add BotDetect CAPTCHA protection to Java forms: screenshot 3

Select "Apache Tomcat v6.0" and click "Next".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 4

Click "Browse" to navigate to the folder where Tomcat is located and click "Finish" and then "OK".

How To add BotDetect CAPTCHA protection to Java forms: screenshot 5