Mzelle Biscotte
Photo credit: Mzelle Biscotte
del.icio.us Digg DZone Reddit StumbleUpon
1 | 2 | Next »
Software Development

How to reCAPTCHA Your Java Application

reCAPTCHA is a novel CAPTCHA service that helps digitize books. Learn how to add reCAPTCHA to your Java app.

ReCAPTCHA is a novel CAPTCHA system developed by the School of Computer Science at my alma mater, Carnegie Mellon University. I won't explain its coolness here since they do a good job of explaining that coolness themselves. What I will do here, though, is explain how to get your Java app reCAPTCHAed very quickly. Note however that reCAPTCHA is not tied specifically to Java.

In this tutorial I'm using Spring 2.5 MVC with annotations, and Commons Validator, but you'll be able to follow this whether or not you're using Spring and Validator.

These instructions are based on the instructions from the reCAPTCHA site, but I'm focusing specifically on Java integration whereas the site makes you dig around a bit to get the information. Not too bad, but enough that there's value in my writing a Java-specific tutorial. :-)

Step 1. Get your account and key pair

First, go to the reCAPTCHA web site and create an account. As part of that account creation process you'll have to specify the domain your reCAPTCHA will be protecting. The reCAPTCHA site will will give you a key pair for that domain. The key pair allows you to authenticate your reCAPTCHA requests to the reCAPTCHA servers, as we'll see.

Step 2. Put the reCAPTCHA JavaScript in your app's form

Here's the JavaScript you need to put in your form, meaning in between the <form> and </form> tags. Put it wherever you would have normally put a CAPTCHA text box. This JavaScript will generate the reCAPTCHA box when users request the page:

<script type="text/javascript"
    src="http://api.recaptcha.net/challenge?k=<your_public_key>">
</script>
<noscript>
    <iframe src="http://api.recaptcha.net/noscript?k=<your_public_key>"
        height="300" width="500" frameborder="0"></iframe><br>
    <textarea name="recaptcha_challenge_field" rows="3" cols="40">
    </textarea>
    <input type="hidden" name="recaptcha_response_field" 
        value="manual_challenge">
</noscript>

It probably goes without saying, but I'll say it anyway: you need to replace the two instances of <your_public_key> with the public key that you received during the account creation process. Be careful that you don't use your private key by mistake. If you do that then everybody will be able to see your private key and act like they're you.

Step 3. Run your app and make sure the reCAPTCHA is showing up

You should see it there in your form. It's OK if you are coming from localhost or 127.0.0.1 instead of the domain that you specified in the account creation step; reCAPTCHA will allow that. You should be able to click the buttons on the reCAPTCHA box and they should work.

After you goof around with that a bit, you'll need to update your app itself so that it actually uses the reCAPTCHA box to validate the form submission.

Let's turn now to the Java piece, where we validate the form and reCAPTCHA.

Social bookmarks: del.icio.us Digg DZone Reddit StumbleUpon
1 | 2 | Next »

Comments (78)

Hi,
I was doing fine until I got to step 4.

I have no idea what to do with the codes in that step, could you please help me.

Thank you
By René Leclerc on Apr 12, 2008 at 5:26 PM PDT
Hi Rene. The step 4 code is an example of how to integrate reCAPTCHA with Spring MVC + Commons Validator. If you are not using those, then you will need to adapt the code accordingly. The main part that is relevant no matter what is this:

// Validate the reCAPTCHA
String remoteAddr = req.getRemoteAddr();
ReCaptchaImpl reCaptcha = new ReCaptchaImpl();

// Probably don't want to hardcode your private key here but
// just to get it working is OK...
reCaptcha.setPrivateKey("<your_private_key>");

ReCaptchaResponse reCaptchaResponse =
reCaptcha.checkAnswer(remoteAddr, challenge, response);

Hope that helps.
By Willie Wheeler on Apr 12, 2008 at 9:01 PM PDT
I also am confused on step 4 like Rene?? Where in my code does step 4 go?

Roy
By Roy Bingham on May 1, 2008 at 8:48 AM PDT
Sorry about the confusion. The code in question goes wherever you are processing the reCAPTCHA form data. So if you are using a servlet, for example, it would go there. If you're using a Struts action, it would go there. Or if you're using Spring MVC (as I'm doing in the example), it would go there. See my comment above (the one dated 04/12/08) for the essential part of the code. I'm pulling the remote address out of the HttpServletRequest and also using some request parameters. You can get at those from any of the environments I just mentioned. If you're in a servlet, for example, then you could do this:

public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {

String challenge = req.getParameter("recaptcha_challenge_field");
String response = req.getParameter("recaptcha_response_field");
String remoteAddr = req.getRemoteAddr();
ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
...etc...
}

Hopefully that helps to clarify. Again the idea is that this code processes the form data coming from the reCAPTCHA form and so you put the code wherever it is you're going to capture that data.
By Willie Wheeler on May 4, 2008 at 8:15 PM PDT
I'm totally lost on the integration of this thing. Do you drop the recaptcha script in the same form as your fields. The thing that im filling out right now to send this question is EXACTLY what I want to do. Sorry, im a little slow i guess.
By Sam on May 27, 2008 at 5:36 PM PDT
Heh, I can see that I need to provide a download or something. I'll try to get one up this weekend. :-)
By Willie Wheeler on May 27, 2008 at 9:06 PM PDT
OK, I've created a download for this. It's at

http://wheelersoftware.s3.amazonaws.com/articles/recaptcha-java/recaptcha-example.zip

Hope this helps.
By Willie Wheeler on Jun 1, 2008 at 11:54 AM PDT
Thanks for posting that example. It helped cleared a lot of confusion. I have a few more problems though. When I fill out the example form it tries to find "recaptcha-example\web\postcomment" but there is no such file. Why is this? What should I do to fix it? Thanks!
By Ben Chevoor on Jul 2, 2008 at 9:23 AM PDT
Hi,
I followed all the instruction you were giving in your post [http://wheelersoftware.com/articles/recaptcha-java.html]
about "How to reCAPTCHA Your Java Application"
It seems like it working up to the minute you submit and then I get this message:
"<?php
$request_method = $_SERVER["REQUEST_METHOD"];
if($request_method == "GET"){
$query_vars = $_GET;
} elseif ($request_method == "POST"){
$query_vars = $_POST;
}
reset($query_vars);
$t = date("U");

$file = $_SERVER['DOCUMENT_ROOT'] . "/../data/gdform_" . $t;
$fp = fopen($file,"w");
while (list ($key, $val) = each ($query_vars)) {
fputs($fp,"<GDFORM_VARIABLE NAME=$key START>\n");
fputs($fp,"$val\n");
fputs($fp,"<GDFORM_VARIABLE NAME=$key END>\n");
if ($key == "redirect") { $landing_page = $val;}
}
fclose($fp);
if ($landing_page != ""){
header("Location: http://".$_SERVER["HTTP_HOST"]."/$landing_page");
} else {
header("Location: http://".$_SERVER["HTTP_HOST"]."/");
}


?>"

Please, can you let me know what is wrong?
Thanks in advance!
I will appreciate any help.

lana.
By Lana on Jul 3, 2008 at 1:26 PM PDT
@Ben: You're welcome. Not sure what's going on with that--it shouldn't be trying to find a file. If you look at web.xml there is a servlet mapping with the URL pattern /postcomment. That should send the request to CommentServlet. Without seeing what you have it's hard for me to know what's happening. :-| What servlet container are you running?

@Lana: Same question I had for Ben--what servlet container and web server are you using? And can you provide any insight into why it (I assume the web server?) might be returning PHP?
By Willie Wheeler on Jul 3, 2008 at 5:39 PM PDT
I am getting "java.lang.NoSuchMethodError" while trying to check answer.
ReCaptchaResponse objCaptchaResponse = objCaptcha.checkAnswer(strRemoteAddr, strChallengeField, strResponseField);
By Joe on Jul 10, 2008 at 7:21 AM PDT
To add to my previous comment posted. The .jar is downloaded and class path is set. Stack dump says the error occured at net.tanesha.recaptcha.ReCaptchaImpl.checkAnswer(ReCaptchaImpl.java:67) . No such method.
By joe on Jul 10, 2008 at 7:25 AM PDT
Hi Joe. I opened up the ReCAPTCHA source and looked at the line in question. It is a call to String.split(), which the Javadoc says was introduced in Java 1.4. So I'm guessing you're using pre-1.4. If so you would need to upgrade to a newer version of Java to use the ReCAPTCHA client.
By Willie Wheeler on Jul 10, 2008 at 8:14 AM PDT
Thanks for clarifying "No Such method" error. JDK used was older version.
By joe on Jul 11, 2008 at 4:52 AM PDT
Hi Willie...
Everything works fine to me with JSP, Struts and JSTL... but... if I put the <html:submit button (or any other thing) below the rechapta then the button (or whatever I put there) doesn't show... any idea??? I tried for hours thinking it could be a CSS problem..
By maria on Jul 15, 2008 at 12:25 PM PDT
Hey Maria. Did you remember to add the taglib declarations to the top of the JSP?
By Willie Wheeler on Jul 15, 2008 at 5:24 PM PDT
Hey Willie! I have all the taglibs I need it before using the reCaptcha.. an I didn't read in any recaptcha page I need any taglib more for showing the recaptha isnt it???.. The button shows up perfectly if I put it over the recaptcha but not below... It happends the same with words, more divs or whatever...
Yesterday I also tried the example you gave, just your html in my application... and it happened just the same... button before recaptcha ok, button below not showing..
Thanks for your help willie!! ;)
By Maria on Jul 16, 2008 at 3:00 AM PDT
I am guessing the issue is a missing quote or something like that. Go ahead and send me the file and I'll be happy to take a quick look.
By Willie Wheeler on Jul 16, 2008 at 7:40 AM PDT
Hello Willie,

I truly admire your patience to answer each and every question with the same degree of enthusiasm.

Thanks a lot for this post.

I could get my reCaptcha working in just an hour after going thru post.
By Rohith on Jul 17, 2008 at 3:56 AM PDT
hey guys, i've been reading the post, but im lost. I know java and i understand the code. I just don't know how to link it to my form. My client does not want to use php....don't ask...im mad about that. I Have this on my html page. I can see the recaptcha,but i don't know how to verify it.

thanks in advance.

<form action="../asp/contact.asp" method="post" name="form">

<script type="text/javascript"
src="http://api.recaptcha.net/challenge?k=<6LfiGQMAAAAAAMatqfHPhTtHHlwSxAiw0kcqDOz3>">
</script>

<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=<6LfiGQMAAAAAAMatqfHPhTtHHlwSxAiw0kcqDOz3>"
height="300" width="500" frameborder="0"></iframe><br>
<textarea name="recaptcha_challenge_field" rows="3" cols="40">
</textarea>
<input type="hidden" name="recaptcha_response_field"
value="manual_challenge">
</noscript>
By Francisco Gomez on Sep 11, 2008 at 3:53 PM PDT
@Francisco: Take the angle brackets out of the public key. I.e., it should be

k=6LfiGQMAAAAAAMatqfHPhTtHHlwSxAiw0kcqDOz3

not

k=<6LfiGQMAAAAAAMatqfHPhTtHHlwSxAiw0kcqDOz3>
By Willie Wheeler on Sep 11, 2008 at 4:04 PM PDT
ok, i have made the change. How do i verify it now?
By Francisco Gomez on Sep 15, 2008 at 8:51 AM PDT
Well, if the reCAPTCHA shows up on the page then it's working. If not then not. :-)
By Willie Wheeler on Sep 15, 2008 at 9:55 AM PDT
yes it does display, but it doesn't verify.
By Francisco Gomez on Sep 15, 2008 at 7:03 PM PDT
Well, the article is my attempt to answer your question. If you can provide a little more information on what you are doing and what isn't working I might be able to assist.
By Willie Wheeler on Sep 15, 2008 at 7:30 PM PDT
Hi Willie.. I tried your sample zip file and just like Ben (7/2/2008) post, I am having the same error once I submit the form.

Once i hit submit, it finds its way to going to "recaptcha-example\web\postcomment", are you re-directing the form here and should have a file here?

All i did is download the file, upload it to my hosting and put in my public key, then tested to submit, then got this error. Any idea?

Also my form is plain HTML.. thanks!
By jason on Sep 21, 2008 at 6:37 PM PDT
Hey Willie, any input or help?
By Jason on Sep 27, 2008 at 3:17 PM PDT
Hi Jason. Like I said to Ben, I'm not really sure what's going on. I just re-downloaded the example to try it out and it still works on my end. I'm using Tomcat 6 if that helps. Basically the form should be posting to recaptcha-example/postcomment, and then that URL should be mapped to the CommentServlet in the web.xml file. The first thing I'd try is adding debug logging to the doPost() method to see whether your controller is even getting the request. You might also try changing the URL to postcomment.do instead of postcomment. (For example if your host has Apache in front of your servlet container then maybe the Apache config isn't passing extensionless URLs to your servlet container.) If you do this, make sure you update both the form action and the web.xml file. Anyway let me know if that works; if it doesn't, I can try to help if you provide more information on your debugging efforts.
By Willie Wheeler on Sep 27, 2008 at 4:07 PM PDT
Hi Willie, thanks for posting this article, got a question, I am able to display the captcha, but not able to get the values for the fields, when i try to retrieve the parameters from the servlet request, I get null values, any ideas?
Many thanks.
By Rafa on Sep 30, 2008 at 4:21 AM PDT
Hi Rafa. Can you post the relevant part of your code?
By Willie Wheeler on Sep 30, 2008 at 8:13 AM PDT
Hi, thanks, sorted out that one, was not putting the code btw the form tags :(, but now I am getting this error when testing locally,
invalid-site-private-key
should not the private key work for localhost as well?
By Rafa on Oct 1, 2008 at 3:32 AM PDT
Actually, my mistake again, a typo, now everything is working fine.
Thank you!
By Rafa on Oct 1, 2008 at 3:40 AM PDT
I am trying with the sample code and replaced the index.html with the public key (twice) and the CommentServlet with the private key. It started the captcha but when i submit it to verify the captcha it always returns me false. Any idea?
By Sayem on Oct 7, 2008 at 10:12 PM PDT
@Sayem: Is your reCAPTCHA'ed app behind a proxy server?
By Willie Wheeler on Oct 7, 2008 at 10:31 PM PDT
@Willie: Thanks for you reply. I found the reason. It's an issue with the IPV6. So when it was trying to get the remote address it was returning 0.0.0.0.0.1 something like that. I replaced them with 127.0.0.1 and it is working now.
By Sayem on Oct 8, 2008 at 8:02 AM PDT
the checkAnswer() method only checks the first word. When i entered a correct first word and whatever i enter for the second word it shows successfull.Am i missing something?
By Sayem on Oct 9, 2008 at 7:59 AM PDT
Hi Sayem. The way reCAPTCHA works is that they serve two words, one of which is known to reCAPTCHA and one of which is not. So you only need to get one word right, since reCAPTCHA doesn't even know what the other word is. (You can read more about this on their website. There's a logic behind it.) I'd be surprised if it turned out that the known word is always the first one but for all I know that could be the case.
By Willie Wheeler on Oct 9, 2008 at 7:29 PM PDT
@Willie: Thanks for your response. I was wondering why it was showing two letters. It might have some reason but most of the client might not understand them.Thanks again.
By Sayem on Oct 10, 2008 at 2:11 AM PDT
Hi Sayem. Yeah, it's actually one of the weaknesses of reCAPTCHA--the fact that they show two words. From the end user's point of view CAPTCHAs are pure overhead; they don't benefit the end user directly at all. (There's an indirect benefit of course in that the forum or whatever doesn't have as much spam.) And so by having two words we've essentially doubled the end user's pain. Like you say, most users won't understand why there are two words. Frankly even if they did understand why, it wouldn't matter. It's still a bother to fill it out.
By Willie Wheeler on Oct 11, 2008 at 1:24 AM PDT

Hi,
Would like to know if there is a way I could customize the display of reCAPTCHA. I would just require the image and the textbox to be displayed, also I just require one CAPTCHA word to be displayed.

regards
Praveen

By Praveen on Feb 5, 2009 at 2:46 AM PST

@Praveen: You can indeed customize the display. You'll need to go to the reCAPTCHA wiki site to read about that.

Regarding having just one word—that is not possible with reCAPTCHA in particular, though with other CAPTCHA widgets it is. It has to do with the way reCAPTCHA works and what they are trying to accomplish with it. Basically they are trying to use reCAPTCHA as a way to digitize books. When they come across words in books that their optical character recognition (OCR) software can't read, they offer them up as CAPTCHA words so people can help. But since the word isn't known, they have to offer the unknown word simultaneously with a word that is known, or else there's no way to tell whether the CAPTCHA was actually solved or not. You can read about that on the reCAPTCHA site as well.

By Willie Wheeler on Feb 5, 2009 at 7:04 AM PST

Hey Willie,

Thanks a lot for this article. It really helped me setup recaptcha in my application super fast. Appreciate your time on this.

-Anand

By Anand on Feb 17, 2009 at 7:53 AM PST

String challenge = req.getParameter("recaptcha challengefield"); String response = req.getParameter("recaptcha responsefield");

Both of those are null when I call them in my servlet? Does anyone know why? First off I am using the customized version of the recaptcha and I don't even have a challenge field except for the in the tag and the response filled is for sure getting filled in. Any ideas? Thanks!

By Amir on Feb 26, 2009 at 9:27 PM PST

I've been trying to figure out this who captcha thing. I'm using a plain html to do this website. I'm not that advanced in html codes so i'm having some trouble. I've been able to display the captcha on my form page. I just can't figure out what codes i need to enter in to make the captcha verify. Can anyone help me?

By JJ Gloster on Mar 23, 2009 at 9:05 AM PDT

hi can anyone help me about how to put this re chaptcha in my site on www.friendster.com, on comment? pls advice me! tnx! more power! ASAP!

By elaine on Mar 24, 2009 at 5:09 AM PDT

been a while, guess he's not providing any more support.

which sucks because i have yet to figure out step 4. i can get the widget to appear on the form, but after i download the zip file, unzip it, stare at its contents, and wonder what goes where on my server, i'm in limbo.

thanks for the help thus far, anyway.

By joeblow on Mar 24, 2009 at 9:09 AM PDT

@joeblow: Naw, I'm still around, and even still happy to provide support when it's pretty clear to me what's going on. But a lot of the answers are already here. For example (@JJ), reCAPTCHA requires server-side support; it won't work with just a plain HTML site. In other cases I simply don't know the answer (e.g. @elaine, how to integrate reCAPTCHA with Friendster).

The thing I think that some readers might be missing is that article specifically addresses reCAPTCHA/Java integration. ReCAPTCHA can be made to work on other platforms but here it's just Java. And the article assumes that you already know your way around the Java platform, including servlets. I probably need to clarify up front.

By Willie Wheeler on Mar 24, 2009 at 10:14 AM PDT

Has anybody tried to integrate reCaptcha with JSF or Trinidad?

I'm sorting through this looking for the best approach but I wanted to see if anybody has done this before I go too much further.

By Jennifer on Mar 26, 2009 at 11:51 AM PDT

Yes very good

By wwww on Mar 31, 2009 at 6:34 AM PDT

Hi, I'm trying to implement this on my site using Struts and Tiles. Everything shows up ok, but the fields recaptcha challengefield and recaptcha responsefield aren't in the request when I submit the form and try to retrieve them in my ActionServlet. I've enumerated the parameters in the request to check naming and there's absolutely no params to do with reCaptcha (I can see my other form fields). If I set it up to use the noscript version it works. Any ideas please?

By Rob on Apr 3, 2009 at 10:40 AM PDT

Hi,

First of all, i am not a web developer. But with my knowledge i have created a website and contact form, where website visitors can submit their comments and queries. The form which i am using is getting free service from "mailmyform.com" website, where whenever some body submit a formin website, i am getting the same through mailmyform.com. Now iwould like to include recaptcha in the contact form. ut i dont know anything. even i read recaptcha documents, but its totally confusing. Please let me know what i need to include in the below codes to get my form fields name, emailaddress, comments and recaptcha be verified and get submitted.

i am including the code which i have right now in the web page. what else modification need to be done to get the recaptcha validation to take place along with others.

Comments Questions Suggestions Login Credentials

By Raees on Apr 11, 2009 at 3:19 PM PDT

<6LeF7AUAAAAAAOTGmGrB7i-FfI5vK6t4SjqskIfl>"> <6LeF7AUAAAAAAOTGmGrB7i-FfI5vK6t4SjqskIfl>" height="300" width="500" frameborder="0">

 <script type="text/javascript">
      function validateForm()
        {
             if(document.Contacts.Name.value == ""
             || document.Contacts.Email_address.value == ""
             || document.Contacts.Query_comments.value == "" )
             {
             alert("Please enter all required fields"); return false;
             }
             else
             {
             return echeck(document.Contacts.Email_address.value);
             }
        }
      function echeck(str)
           {
             var at="@"
             var dot="."
             var lat=str.indexOf(at)
             var lstr=str.length
             var ldot=str.indexOf(dot)
             if (str.indexOf(at)==-1)
                {
             alert("Invalid E-mail ID")
                return false
             }

             if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
                {
             alert("Invalid E-mail ID")
             return false
             }

             if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
             alert("Invalid E-mail ID")
             return false
             }

             if (str.indexOf(at,(lat+1))!=-1){
             alert("Invalid E-mail ID")
             return false
             }

             if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
             alert("Invalid E-mail ID")
             return false
             }

             if (str.indexOf(dot,(lat+2))==-1){
             alert("Invalid E-mail ID")
             return false
             }

             if (str.indexOf(" ")!=-1){
             alert("Invalid E-mail ID")
             return false
             }

             return true
        }
      </script> 

With regards, Raees

By raees on Apr 11, 2009 at 3:21 PM PDT

pasting once again the complete first part of the code.

Comments Questions Suggestions Login Credentials <6LeF7AUAAAAAAOTGmGrB7i-FfI5vK6t4SjqskIfl>"> <6LeF7AUAAAAAAOTGmGrB7i-FfI5vK6t4SjqskIfl>" height="300" width="500" frameborder="0">
By raees on Apr 11, 2009 at 3:25 PM PDT

Willy,

Thanks a MILLION for this post. I am building a login screen, and definitely saw the need for an anti-bot measure. I had no idea how I would do it with my existing Struts/JSP/Servlets setup, aside from the download of the jar and such. Anyway, between your original post, and some of your comments, I had success within about 30 minutes. Nicely done, and thank you again! I am by no means a Java expert, you made me feel like one!

By Rory on Apr 17, 2009 at 2:21 PM PDT

How do you add validtation to captcha befor redirecting to another page.

By Guest on Apr 30, 2009 at 1:42 AM PDT

Can anyone help ?

I am getting the error message :

net.tanesha.recaptcha.ReCaptchaException: Cannot load URL: api-verify.recaptcha.net

By Shilbhadra on May 30, 2009 at 5:42 AM PDT

Thanx a lot for this help.

I Dunno much abt Websites...but i was just searching on net to build it..& came around the concept of Captcha

i just Copied the Step2 Code in a text file...& written

Code given in Step 2

& saved it as Captcha.html

& when i Ran this file it was showing Captcha.....but can u pls send me the Code for validating the Code inserted...Pls give COde which i can copy paste as Done above for showing Captcha

Thanx a lot

By Shash on Jun 5, 2009 at 2:34 PM PDT

the above code is working fine with IE but its not working in firefox. the challenge and the response its taking is null. Can you please guide me on that.

Thanks in advance.

By sarvesh on Jun 16, 2009 at 12:05 AM PDT

Kindly can any one help me on why the given code is not working with mozilla firefox.

By sarvesh on Jul 13, 2009 at 12:39 AM PDT

Is anyone having problems with this using firefox 2? It is working correctly on FF3. But FF2 I am not able to get the response field. I am doing this using ajax. And I see the text box on the page using firebug. But when I post the request, the response field is always blank.

Thank you.

By Megan on Jul 22, 2009 at 8:39 AM PDT

When i am trying to validate my form after collecting the challenge and response field from jsp, i am getting this error :

net.tanesha.recaptcha.ReCaptchaException: Cannot load URL

This error occurs when i send the request form action class to reCaptcha sever. Can anyone guide me for this error.

By Abhishek on Jul 26, 2009 at 4:57 AM PDT

I am also getting the same error when executing the expression below. I entered the link in my browser url field and I got a page with "false" and invalid argument, which makes believe that I can connect to the server.

Server returned HTTP response code: 403 for URL: http://api-verify.recaptcha.net/verify

ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, response);

Thanks a bunch

By Mckey on Jul 30, 2009 at 8:23 AM PDT

Good job, i was able to integrate my jsp MVC-II project to use re-captcha thanks to this small guide.
Here is the code in my controller servlet.

 String challenge = request.getParameter("recaptchaChallengeField");
    String captcharesponse = request.getParameter("recaptcharesponseField");
    String remoteAddr = request.getRemoteAddr();
    ReCaptchaImpl reCaptcha = new ReCaptchaImpl();

    /*set the private key*/

  reCaptcha.setPrivateKey(session.getServletContext().getInitParameter("captchaprivatekey"));
    /*validate the captcha */
    ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, captcha_response);
    /*handle error */
    if (!reCaptchaResponse.isValid()) {
        message="captcha error: "+ reCaptchaResponse.getErrorMessage();
        errorForward(request,userbean,message,response);
        return;
    } 

short and sweet! Thanx

By tenzin on Aug 4, 2009 at 7:32 AM PDT

I am working on integrating this into a JSF application, and I have it working when I first go to my jsf page. The reCaptcha image shows up and all is good. BUt, if I hit my submit button at the bottom of the page, and if I return to the same page to display an error message such as the reCaptcha words are incorrect, the reCaptcha image disappears. After trying to figure it out for a little, I noticed the problem was a security error in the browser is preventing this from appearing. The error states "Security Error: Content at http://localhost/mypage.jsf may not load data from http://api.recaptcha.net/challenge?k=mykey?&rand=0.616572868255853." The same problem happens in both IE7 and Firefox. IE7 gives a pop up message where Firefox just shows the message in the error console. Is there something I am doing wrong or misunderstanding about how to set this up? Why does it work when the page first loads, but not when the page is reloaded after form submission?

By stevew on Aug 23, 2009 at 8:57 AM PDT

Hey I'm trying to add reCAPTCHA to my website as a registreation verification. The website is hosted by webs.com premium services and the guide just doesnt really make any sense to me because when I put in the code on come pages it works and others don't. I also do not see a submit button either. Thank you

By Sam Andersen on Sep 4, 2009 at 7:41 PM PDT

Hi,

Can anyone explain me how to use ReCaptcha in ICE-Facets? Though it can be shown on the screen, validation can't happen as I am not getting the 'Challenge' and 'Respone' values in PortletRequest, as I can't use HttpRequest.

Any help in this regard will be appreciated.

Regards, -prasad.

By Prasad on Oct 9, 2009 at 9:12 AM PDT

This may have already been covered, but I am relatively new to web design and I am using Yahoo sitebuilder to build and publish my website. I really don't understand all of the code and computer language. I, like other people, am fine until I get to step 4. I looked at your zip drive example and it still doesn't make any sense to me. Where is the code added in the website. Is it on the form itself, or somewhere else on the website. If you look at my website, I'm trying to add this application to the comment page and contact us page. Thank you in advance for any help you can give me.

By Jennifer on Oct 15, 2009 at 8:30 PM PDT

I have experience this code and it's working fine... Thank you...

By Ihraz on Nov 19, 2009 at 3:02 AM PST

Hi!

This is a great example, and I've almost got it working. The only thing is that my recaptcha app is behind a proxy. Any idea how to make it work? Thanks in advance for any help!

By Elin on Jan 6, 2010 at 12:26 AM PST

hi Willie, How would i give the HttpServletRequest as post in a java file if am using struts frame work? Like the one you have explained for servlet and spring, can you provide a solution please?

By Coder on Jan 25, 2010 at 6:05 AM PST

You should wrap a try catch around checkAnswer, as it might throw a ReCaptchaException if your request times out. The class from tanesha implements some primitive timeouts, but they are probably not workable in the real world.

By Brad Morris on Feb 23, 2010 at 4:37 PM PST

Thanks a lot! Got it working with minor tweaking really fast. JSF (richfaces), normal backing bean (not spring)

By mikelber on Mar 1, 2010 at 7:32 PM PST

I am getting java.net.UnknownHostException: api-verify.recaptcha.net, while validating the user response. I am working in an environment which is being protected with firewall and have a proxy server to route the requests.

Regards, Prasanth

By Prasanth on Mar 8, 2010 at 5:01 AM PST

Thanks a lot guys. Got it working correctly by JSP and Servlet, by using above mentioned Servlet code. And in the JSP i use it as follows,


By Chathuranga on Jul 1, 2010 at 8:16 AM PDT

// code here
By Chathuranga on Jul 1, 2010 at 8:45 AM PDT

I have a problem, a compilation error: package net.tanesha.recaptcha.ReCaptchaImpl does not exist plz help me

By thuxette on Aug 14, 2010 at 3:53 PM PDT

he myth of pandora is ancient, appears in several distinct Greek versions, pandora armbandand has been interpreted in many ways. In all literary versions, Neu Eingetroffen however, Pandora Armbänder the myth is a rosetta stone kind of theodicy, addressing the question pop information, web easy get, sports fashion, news-fashionof why there is evil in the world. In the seventh hot-winter century BC, Hesiod, both in his Theogony (briefly, without naming Pandora outright rosetta stone language, rosetta stone spanish, abercrombie and fitch, Abercrombie Fitch

By pandora schmuck on Aug 30, 2010 at 10:53 PM PDT

Post a comment

Your name:
Your e-mail address (won't be displayed):
Your web site (optional):
example: www.xyz.com
Your comment:
Preview:
By You
Please help us reduce comment spam:
Spring in Practice
My brother and I are writing Spring in Practice for Manning!

What's New?

2009-08-30 - Check out my two-part series on DZone: Spring Integration: A Hands-On Tutorial.
2009-03-25 - My new article Getting Started with Spring Batch 2.0 is available on DZone.
Home | Consulting | Tech Articles | Mailing List | Contact | Spring Blog
Copyright © 2008 Wheeler Software, LLC.