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
- JDK 1.6.0_07 – http://java.sun.com
- Eclipse 3.4 IDE for Java EE Developers – http://www.eclipse.org/downloads/
- Tomcat 6.0.14 – http://jakarta.apache.org/tomcat
- JACOB – Java COM Bridge – http://sourceforge.net/projects/jacob-project
- BotDetect 2.0 CAPTCHA – free BotDetect CAPTCHA trial
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"
In the "Project name", type "captchaWeb", select "Apache Tomcat v6.0" in "Target Runtime" and click "Finish".
At the prompt click "Yes" to switch to Java EE Perspective
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".
Right click on the new package and select "New\Class". In the "Name" field type "CaptchaServlet" and click "Finish".
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..."
On the next screen select "User Library" and click "Next".
Click "User Libraries".
Click "New".
Type in "jacob" for "User library name" and press "OK".
Click "Add jars", navigate to the "Jacob" folder and select "jacob.jar".
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.
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".
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".
Go to "Window\Show View" and select "Servers". In the "Servers" view, right click and select "New\Server".
Select "Tomcat v6.0 Server" and click "Next".
On the "Add and Remove Projects", select "captchaWeb" and click "Add", then "Finish".
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".
Restart the server, open the browser and type the URL http://localhost:8080/captchaWeb/index.htm.
If you enter correct values you are forwarded to success.htm
Otherwise, you are forwarded to failure.htm
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".
In Eclipse, go to "Window\Preferences" and then to "Java\Installed JREs" to check the selected JRE.
If your JRE is not there, click "Add..." and navigate to its folder.
Under "Server\Runtime Environments" click "Add..."
Select "Apache Tomcat v6.0" and click "Next".
Click "Browse" to navigate to the folder where Tomcat is located and click "Finish" and then "OK".




























