|
|
This article gives step-by-step instructions to setup captcha in your JSP page using recaptcha. In total there are 3 steps
c) Now Select the “Sign up” link
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
f) That’s it. Now you will get two keys, one called “public-key” and the other “private-key”. Make a note of Both.
<%@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 <% //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'Leave a Reply |
Popular Articles
Blog Categories
Monthly Archives
Resources
|
Was desperately looking for this! Your step by step approach was very easy to understand. Thanks!
Thanks, For Publish this Post in step by step approach, That is easy and very cool
tihn578esltovfdp
I must say that this was a real simplification of the process. The Re-Captcha project needs this. Gr8 Work!!
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