I created this simple class and tried - it works fine with captcha
http://www.google.com/recaptcha

package com.andreig.hiredonphone;
import org.apache.click.control.AbstractControl;

public class ReCaptchaField extends AbstractControl {

   private String value;
   private String public_key;

   // -------------------------------
   public ReCaptchaField( String public_key ){
     this.public_key = public_key;
     setName( "captcha" );
   }

   // -------------------------------
   public void setValue(String value) {
       this.value = value;
   }

   // -------------------------------
   public String getValue() {
       return value;
   }

   // -------------------------------
   public String getTag() {
       // Return the HTML tag
       return "br/><i>To verify that you are human</i><br/>" +
         "<script type=\"text/javascript\"
src=\"http://api.recaptcha.net/challenge?k="; +
         public_key +
         "\"></script><noscript><iframe 
src=\"http://api.recaptcha.net/noscript?k="; +
         public_key +
         "\" height=\"300\" width=\"500\"
frameborder=\"0\"></iframe><br></noscript><br";
   }

   // -------------------------------
   public boolean onProcess() {
       // Bind the request parameter to the field value
       String requestValue = getContext().getRequestParameter(getName());
       setValue(requestValue);

       // Invoke any listener of MyField
       return true;
   }

 }


You use it as this, put your public key in constructor

    ReCaptchaField captcha = new ReCaptchaField(
"6LfTf7sSAAAAAICJsZlojJaUAJX1Cws-aEBMm7oJ" );
    form.add( captcha );


Then in "onSubmit" I put


    HttpServletRequest request = getContext().getRequest();

    String remoteAddr = request.getRemoteAddr();
    ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
    reCaptcha.setPrivateKey( "my_private_key" );

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

    if( !reCaptchaResponse.isValid() ){
      msg = "Captcha field not correct";
      return false;
    }

Reply via email to