title: Recaptcha.widget is null and as a result does not render the form on recaptcha validation failure
body: <p>Working on implementing recaptcha in a CMS integrated website. First is my HTMl body tag that invokes the javascript method to create a recaptcha on loading the body.</p>
  
  <pre><code>    &lt;body onload="return reloadRecaptcha();"&gt;
  </code></pre>
  
  <p>Recaptcha appears below the form using the following code, Here is my js method that generates the recaptcha. </p>
  
  <pre><code>    function reloadRecaptcha() {
      console.log("@ reloadRecaptcha ");
      var publicKey = "";
      var strServername = location.hostname;
  
      if (strServername.indexOf("01") &gt;= 0 || strServername.indexOf("02") &gt;= 0 ){ 
          publicKey = "HARDCODED_INTERNAL_KEY1";
      } else {
          publicKey = "HARDCODED_PUBLIC_KEY2";
      }
  
      var div = "recap";
  
      if (!$("#message").is(":empty")) {
          console.log("@ if Recaptcha.create ");
          Recaptcha.create(publicKey,div,{theme: "red", callback: Recaptcha.focus_response_field});
      } else {
          console.log("@ else Recaptcha.create ");
          Recaptcha.create(publicKey,div,{theme: "red"});
      }
      console.log("@ Exit reloadRecaptcha ");
      return false;
  }
  </code></pre>
  
  <p>When I submit the form including the repatcha answer to the servlet which validates the answer and forwards back to the same form on error, I get the following error in my browser's console.</p>
  
  <ul>
  <li>In CHROME, error message "Uncaught TypeError: Cannot set property 'innerHTML' of null" @ line recaptcha_ajax.js:112.</li>
  <li>In firefox, error message "TypeError: Recaptcha.widget is null" @ line recaptcha_ajax.js:112.</li>
  </ul>
  
  <p>I have no clue why when I forward to the form jsp on error only I get this error and not during initial loading of the the form.</p>
  
  <p>Code snippet of my server side code( a servlet):</p>
  
  <pre><code>    protected void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException  {
  System.out.println("***** In the Captcha Validatation servlet**** ");
  ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
  String remoteAddr = request.getRemoteAddr();
  String strServerUrl = request.getServerName().toLowerCase();
  String strPrivateKey = "";
  if ((strServerUrl.indexOf("01") &gt;= 0) || (strServerUrl.indexOf("02") &gt;= 0))
    strPrivateKey = getPropertyString("recaptcha.internal.privatekey");
  else {
    strPrivateKey = getPropertyString("recaptcha.external.privatekey");
  }
  reCaptcha.setPrivateKey(strPrivateKey);
  String challenge = request.getParameter("recaptcha_challenge_field");
  String uresponse = request.getParameter("recaptcha_response_field");
  ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
  HttpSession session = request.getSession(false);
  if (session != null) {
    String sessionId = session.getId();
    Map paramMap = new HashMap();
    for (Enumeration e = request.getParameterNames(); e.hasMoreElements(); ) {
      String key = (String)e.nextElement();
      String value = request.getParameter(key);
      paramMap.put(key, value);
    }
    session.setAttribute("paramMap", paramMap);
    if (reCaptchaResponse.isValid())
    {
      StringBuilder successUrl = new StringBuilder();
      successUrl.append(getPropertyString("recaptcha.validationsuccess.url"));
      successUrl.append("?evtvar=").append(request.getParameter("evtvar"));
      successUrl.append("&amp;vgnextoid=").append(request.getParameter("vgnextoid"));
      RequestDispatcher rd = request.getRequestDispatcher(successUrl.toString());
      rd.forward(request, response);
    }
    else
    {
      System.out.println("***** reCaptchaResponse INVALID *** ");
      session.setAttribute("error1", getPropertyString("recaptcha.validationfailure.message"));
      System.out.println("SERVLET PATH --&gt; " + request.getServletPath());
      String url_form = "/vgn-ext-templating/v/index.jsp?vgnextoid=40289c7c6099f210VgnVCM100000268315acRCRD&amp;vgnextfmt=default";
      System.out.println("Forwarding to the url after captcha validation  --&gt; " + url_form);
      RequestDispatcher rd = request.getRequestDispatcher(url_form);
      rd.forward(request, response);
    }
  }}
  </code></pre>
  
  <p>What I perceive from the error is when the form reload to display the validation error message it could not find the div#recap</p>
  
  <pre><code>    &lt;div id="recap" style="height:130px;"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>which is always present as part of my html, just below main form.</p>
  
  <p>When I hit this url "http:///vgn-ext-templating/v/index.jsp?vgnextoid=40289c7c6099f210VgnVCM100000268315acRCRD&amp;vgnextfmt=default" directly from the browser the recaptcha and form appears as expected but the issue happens only on error and forwarding back to the form. Any help is appreciated.</p>