CAPTCHA in Java using reCAPTCHA

Last modified on August 1st, 2014 by Joe.

reCAPTCHA is the popular CAPTCHA tool that allows to embed a CAPTCHA in UI and validate the input to block automated spammers. reCAPTCHA provides a Java library and a easy way to implement the CAPTCHA. CAPTCHA stands for “Completely Automated Public Turing test to tell Computers and Humans Apart”.

reCAPTCHA uses a vast library of words for CAPTCHA, it will connect to its API at runtime. This is not a standalone tool and it requires Internet access to work. There are many attempts to develop a fool proof CAPTCHA system and reCAPTCHA has proven to be one the best solution that is difficult to break. The best known use for CAPTCHA is to guard against spams and in some cases we can protect lower layers (example: DB layer) from denial of service (DOS) attacks.

Hackers have claimed and proved success of breaking the CAPTCHA algorithm many times. One side CAPTCHA tools are getting better day by day and other side hackers also breaking the CAPTCHA frequently. We can generate CAPTCHA using Java 2D API with custom generated words, but that has not proven to be strong enough.

reCAPTCHA in Java Application

In this how to tutorial, let us see how we can use reCAPTCHA to safeguard our Java application. This is the simplest it can get.

Step 1: Import classes, Load UI and display CAPTCHA

This is the first page where we display the generated CAPTCHA image and get input from the user. We can also do this using JavaScript directly.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

<%@ page import="net.tanesha.recaptcha.ReCaptcha" %>
<%@ page import="net.tanesha.recaptcha.ReCaptchaFactory" %>

<title>CAPTCHA in Java using reCAPTCHA</title>
<h2>CAPTCHA in Java Application using reCAPTCHA</h2>
<form action="./validate.jsp" method="post">
<p>Username: <input type="text" name="user"></p>
<p>Password: <input type="password" name="password"></p>
	ReCaptcha c = ReCaptchaFactory.newReCaptcha(
        		"6LdlHOsSAAAAACe2WYaGCjU2sc95EZqCI9wLcLXY", false);
	out.print(c.createRecaptchaHtml(null, null));
   <input type="submit" value="submit" />

Step 2: Import, get input, checkAnswer

Second page where we get the user input, user’s IP address and other required parameters, to pass on to the reCAPTCHA server for input validation. reCAPTCHA server validates the input and returns true or false.

<%@ page import="net.tanesha.recaptcha.ReCaptchaImpl"%>
<%@ page import="net.tanesha.recaptcha.ReCaptchaResponse"%>

<title>CAPTCHA in Java using reCAPTCHA</title>
<h2>CAPTCHA in Java Application using reCAPTCHA</h2>
		String remoteAddr = request.getRemoteAddr();
		ReCaptchaImpl reCaptcha = new ReCaptchaImpl();

		String challenge = request
		String uresponse = request.getParameter("recaptcha_response_field");
		ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(
				remoteAddr, challenge, uresponse);

		if (reCaptchaResponse.isValid()) {
			String user = request
			out.print("CAPTCHA Validation Success! User "+user+" registered.");
		} else {
			out.print("CAPTCHA Validation Failed! Try Again.");
<a href=".">Home</a>	

reCAPTCHA Application Output



reCAPTCHA result

Download Example reCAPTCHA Java Application

Download Example: Java Web Application using ReCAPTCHA


  1. reCAPTCHA is not stand-alone and need Internet connection.
  2. Should register with reCAPTCHA to get a public and private key to use in the code.
  3. Add Jar file from reCAPTCHA in Java application classpath.
  4. In terms of validation, the input we provide goes back to Google’s (reCAPTCHA) server and it helps to digitize books.
    1. Sometimes there are two words (images) shown in CAPTCHA image. For one image reCAPTCHA knows the exact word form and for another image the exact word is not know.
    2. When the user gives input, if the first word is given correctly, then whatever the user gives for the second image is taken as the right word and stored in DB.
    3. This is repeated with many many users and the frequency of the word input is matched and once the required strength is matched then reCAPTCHA has got the right word for the new image. It is used to digitize books by Google. So when you use reCAPTCHA, you may be helping Google to digitize some old print.

Comments on "CAPTCHA in Java using reCAPTCHA"

  1. Praveen says:

    Good :)

  2. Mahaboob basha says:

    This is what i am searching, it’s simply blast.

  3. Jinu Vijay says:

    Nice article… esp the snippet on digitizing books.

  4. aravind says:

    Good article..

  5. swapna says:

    hi joe,
    I have posted a comment few days ago about this captcha and thanks for explaining in detail.Nice one!!!

  6. Himanshu says:

    Nice one.It will help me out in project

  7. Pratik says:

    Nice Article

  8. yuvaraj says:

    Nice sir…

  9. Vivek says:

    Please provide example to access recaptcha via proxy and also how to fetch the recaptcha image via proxy validation.

  10. Anonymous says:

    how to use captcha in android

  11. Joe says:

    I will write a tutorial for reading captcha in Android soon.

  12. amod maroly says:

    i want to make simple captcha without using recaptcha

  13. Pritesh Gupta says:

    Dear sir,
    you didn’t mansion the name of the second page.
    Second how to use this with java script ?
    I have a signup.jsp form in which I am validating all the fields onsubmit using a signup.js javascript file, now I want to use this CAPTCHA at the end of my jsp page- signup.jsp.
    So how to use this with javascript.

    Thank you

  14. akhil says:

    application us working absolutely fine but .. the image box appearing is very big .can we resize it?
    any suggestions please

  15. vasu says:

    It is working excellently. Could you let me know is there any way of removing “Privacy & Terms” from the widget as needed to our business req.

  16. Anonymous says:

    very nice sir.this is what i search for

  17. Govind Yats says:

    Thanx alot. I was searching for 1-2 days and found this blog.
    This article helped me alot and is working for me correctly. Thnx again!!!!.

Comments are closed for "CAPTCHA in Java using reCAPTCHA".