title: Frustrated trying to migrate from v1 to Recaptcha v2 in Java application using Struts2 framework
body: <p>I have an existing Java application on Struts 2 framework.  While trying to migrate away from captcha v1 and implement v2, I am not having any luck.  I'm hoping someone can help, I would think this should be basic.  The I'm not a robot just sits and spins when it is clicked and nothing goes to my action class until I click submit. I have tried replacing the sitekey in jsp with my actual key hard-coded.  No luck.  Here are my new code snippets:</p>
  
  <p>my.jsp</p>
  
  <pre><code>&lt;script src="https://www.google.com/recaptcha/api.js" async defer&gt;&lt;/script&gt;
  &lt;div class="g-recaptcha" data-sitekey=&lt;s:property value="#session.captchaPublicKey"/&gt;"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>In my action action class on submit:</p>
  
  <pre><code>public void validateCaptcha() throws Exception{
    logger.debug("in captcha validate");  
    String gRecaptchaResponse = request.getParameter("g-recaptcha-response");
      System.out.println(gRecaptchaResponse);
      boolean verify = Captcha.verify(gRecaptchaResponse);
      System.out.println("Captcha Verify"+verify);
      //RequestDispatcher rd = getServletContext().getRequestDispatcher(
      if (!verify == true) {
          addActionError("You missed the Captcha.");      
  
      }
  
  }
  </code></pre>
  
  <p>My Captcha.java class</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.StringReader;
  import java.net.URL;
  
  import javax.json.Json;
  import javax.json.JsonObject;
  import javax.json.JsonReader;
  import javax.net.ssl.HttpsURLConnection;
  
  public class Captcha {
  
  public static final String url = "https://www.google.com/recaptcha/api/siteverify";
  public static final String secret = WamProps.getProperty("RECAPTCHA_PRIVATEKEY");
  private final static String USER_AGENT = "Mozilla/5.0";
  
  public static boolean verify(String gRecaptchaResponse) throws IOException {
      if (gRecaptchaResponse == null || "".equals(gRecaptchaResponse)) {
          return false;
      }
  
      try{
      URL obj = new URL(url);
      HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
  
      // add reuqest header
      con.setRequestMethod("POST");
      con.setRequestProperty("User-Agent", USER_AGENT);
      con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
  
      String postParams = "secret=" + secret + "&amp;response="+ gRecaptchaResponse;
  
      // Send post request
      con.setDoOutput(true);
      DataOutputStream wr = new DataOutputStream(con.getOutputStream());
      wr.writeBytes(postParams);
      wr.flush();
      wr.close();
  
      int responseCode = con.getResponseCode();
      System.out.println("\nSending 'POST' request to URL : " + url);
      System.out.println("Post parameters : " + postParams);
      System.out.println("Response Code : " + responseCode);
  
      BufferedReader in = new BufferedReader(new InputStreamReader(
              con.getInputStream()));
      String inputLine;
      StringBuffer response = new StringBuffer();
  
      while ((inputLine = in.readLine()) != null) {
          response.append(inputLine);
      }
      in.close();
  
      // print result
      System.out.println(response.toString());
  
      //parse JSON response and return 'success' value
      JsonReader jsonReader = Json.createReader(new StringReader(response.toString()));
      JsonObject jsonObject = jsonReader.readObject();
      jsonReader.close();
  
      return jsonObject.getBoolean("success");
      }catch(Exception e){
          e.printStackTrace();
          return false;
      }
    }
  }
  </code></pre>