Implementing CAPTCHA in JAVA using JCaptcha


In this post we will see how to implement Captcha in a Java based web application. JCaptcha is a third-party open source framework. It stands for ‘Java completely automated public test to tell computers and humans apart’. Here, we have used a simple java servlet to generate the captcha image with an input field. Upon entering the characters as displayed in image and clicking the submit button, validation is performed in the servlet. Based upon the success or failure many operations can be done. Here I am just redirecting back to the home page with the success or failure message.

NOTE : There are so many other APIs available for implementing captcha in JAVA. I have given the tutorial for this as I have found this easy and we need the minimal configurations and less code to implement this.

Resources required:
1) commons-collections-3.2.jar
2) commons-logging-1.0.4.jar
3) filters-2.0.235.jar
4) jcaptcha-2.0-alpha-1-SNAPSHOT.jar
5) jcaptcha-api-1.0.jar
6) jcaptcha-integration-simple-servlet-2.0-alpha-1-SNAPSHOT.jar

These jars need to be placed inside lib folder of the web application. And the below 4 jars only need to be configured in build path of the application.

Folder Structure:
Captcha Folder Structure

This is the deployment descriptor of the application. Here we have to configure the “SimpleImageCaptchaServlet” servlet. This class is responsible for generating the captcha image.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">

This is the home page of the application. The below two lines are compulsory to add in side the form tag for the Captcha to work.

<img src="captchaImage" /><br>
<input type="text" name="captchaText" value="" />

The image is dynamically generated by SimpleImageCaptchaServlet class.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
		<title>Java Captcha Example Application</title>
		<form action="myServlet">
			<img src="captchaImage" /><br>
			<input type="text" name="captchaText" value="" />
			<input type="submit">

This is the servlet of our application. After clicking the submit button this servlet is called to handle the request. In this class we are retrieving the input fields value entered by the user for the captcha image. Then we call the “validateResponse()” method of ‘SimpleImageCaptchaServlet’ class to validate the captcha value entered by user. This method returns a Boolean value, i.e. true if the captcha value is correct else false is returned.

package com.captchaproject.controller;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;

public class MyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public void init(ServletConfig config) throws ServletException {
		System.out.println("servlet initialized");

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("doGet() called");
		String captchaText = request.getParameter("captchaText");
		boolean isCaptchaValid = SimpleImageCaptchaServlet.validateResponse(request, captchaText);
		RequestDispatcher rd = request.getServletContext().getRequestDispatcher("/home.jsp");
		String msg = null;
			msg = "captcha passed";
			msg = "captcha failed";
		request.setAttribute("message", msg);
		rd.forward(request, response);

Leave a comment


Enter your email address to subscribe to this blog and receive notifications of new posts by email.