Captcha Implementation in JSP to prevent Spam in 5 minutes : step by step

  

This article gives step-by-step instructions to setup captcha in your JSP page using recaptcha. In total there are 3 steps

  • Sign up at http://recaptcha.net and get the Public and Private Keys
  • Download the reCaptcha4j library
  • Write code in your jsp to implement it


Step 1: Getting ReCaptcha’s public and Private keys


a) Go to http://recaptcha.net, and click the “get reCAPTCHA” link
Gettting recaptch Step 1

b) In the Next Page, select the “Sign-up Now” Button
Captcha in JSP Step2

c) Now Select the “Sign up” link
captcha in jsp step3



d) Now Enter your details and select the sign-up button
Captcha in JSP Step5

e) In the next page, Enter the URL where you will use re-captcha. Don’t worry. while testing you can test this on localhost, recaptcha will allow that
Captcha in JSP step 6

f) That’s it. Now you will get two keys, one called “public-key” and the other “private-key”. Make a note of Both.
Captcha in Jsp Step 7


Downloading the recaptcha4j library


a) Go to http://tanesha.net/maven2/net/tanesha/recaptcha4j/recaptcha4j/ and select the latest version and download the respective jar file
Captcha in JSP Step 8

Captcha in JSP Step 9

b) Place this jar in a location in the class path of your web application, like your webservers server/lib or web-inf/lib directory.


Writing your JSP[captchaTest.jsp]



a) Write code to display the captcha Text box
Here’s the code. All you need to do is create an instance of “ReCaptcha” object and call the “createRecaptchaHtml” method on it. While creating the “ReCaptcha” object make sure you pass in the correct public and private keys

<%@page import="net.tanesha.recaptcha.*"%>
<html>
<head>
   <title>Captcha Test</title>
</head>
<body>
 <%
     //Pass in the right public key and private key here.. 
     ReCaptcha captcha = 
       ReCaptchaFactory.
	newReCaptcha("your-public-key", "your-private-key", false);
 %>
 
    <!--we will submit it to the same page -->
    <form name="test" action="captchaTest.jsp">
 
	<%
        String captchaScript = captcha.
		createRecaptchaHtml(request.getParameter("error"), null);
	out.print(captchaScript);
  	%>
 
	<input type = "submit" value="submit form" />
     </form>
</body>

b) Add code to validate the captcha
Now. let’s add code to validate the captcha to the Same jsp. We will check if the “recaptcha_response_field” is present if so we will validate the captcha.. All you need to do is to call the “checkAnswer” method on the “ReCaptcha” instance that you just created.
Here’s the snipped that validates the captcha.

<%
//Check if the recaptcha_response_field is present
if(request.getParameter("recaptcha_response_field") != null){	
	ReCaptchaResponse response1 = captcha.
		checkAnswer(request.getRemoteAddr(), 
		request.getParameter("recaptcha_challenge_field"), 
		request.getParameter("recaptcha_response_field"));
 
        //Check if the response is valid
	if (response1.isValid()) {
			//Do things when captcha is successful
	   out.print("Captcha Success");
	}
	else {
	  //Do things when captcha is un-successful
  	  out.print("Captcha Unsucessful");
	}
}
%>

That’s it.. Here’s the final JSP ,with the two snippets combined

<%@page import="net.tanesha.recaptcha.*"%>
<html>
<head>
   <title>Captcha Test</title>
</head>
 
<body>
 
<%			
ReCaptcha captcha = 
   ReCaptchaFactory.newReCaptcha("your-public-key", 
    "your-private-key", false);
%>
<%
if(request.getParameter("recaptcha_response_field") != null){	
	ReCaptchaResponse response1 = captcha.checkAnswer
			(request.getRemoteAddr(), 
			request.getParameter("recaptcha_challenge_field"), 
			request.getParameter("recaptcha_response_field"));
 
	if (response1.isValid()) {
		//Do things when captcha is successful
		out.print("Captcha Success");
	}
	else {
		//Do things when captcha is un-successful
		out.print("Captcha Unsucessful");
	}
}
%>
 
<form name="test" action="captchaTest.jsp">
 
<%
	String captchaScript = captcha.
			createRecaptchaHtml(request.getParameter("error"), 
			null);
	out.print(captchaScript);
%>
 
  <input type = "submit" value="submit form" />
</form>
</body>
</html>

Enjoy implementing captcha in your application.







5 Responses to 'Captcha Implementation in JSP to prevent Spam in 5 minutes : step by step'

  1. Anonymous - June 18th, 2008 at 6:13 pm

    Was desperately looking for this! Your step by step approach was very easy to understand. Thanks!

  2. Anonymous - August 22nd, 2008 at 6:07 am

    Thanks, For Publish this Post in step by step approach, That is easy and very cool

  3. Stephon Keller - November 12th, 2008 at 11:21 pm

    tihn578esltovfdp

  4. tashes - December 8th, 2008 at 1:51 pm

    I must say that this was a real simplification of the process. The Re-Captcha project needs this. Gr8 Work!! :)

  5. Prathepan - December 10th, 2008 at 7:24 am

    Hi , Thanks for the step by step procedure…But when i included in my project i always get “Captcha Unsucessful”.What the reason for this ? I gave my domain “localhost”..

    Pls help me


Leave a Reply





Popular Articles

Blog Categories

Monthly Archives

Resources