简体   繁体   中英

How to include recapcha in html from java servlet?

I want to include recapcha plugin in my web page from servlet. If i use <%@ ..%> or it won't include or show any recapcha.

Here is my servlet

protected void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException {

    final PrintWriter out=response.getWriter();

    String s="";

    String capchadisplay="<%@ ReCaptcha c = ReCaptchaFactory.newReCaptcha(\"public-key" +
            "xxxxxxx \", \"private-key " +
            "\", false);\nout.print(c.createRecaptchaHtml(null, null));\n%>";

    String capchaimport="<%@ page language=\"java\" contentType=\"text/html; charset=utf-8\"\n" +
            "pageEncoding=\"utf-8\" xmlns:jsp=\"http://java.sun.com/JSP/Page\"%>\n" +
            "\n<%@ page import=\"net.tanesha.recaptcha.ReCaptcha\" %>\n" +
            "<%@ page import=\"net.tanesha.recaptcha.ReCaptchaFactory\" %>";

    String prefix="<!DOCTYPE html><html>\n<head>\n<title>Forms</title>\n</head>\n<body>\n";
    String subfix="</body>\n</html>\n";



    s=capchaimport.concat(prefix.concat(capchadisplay.concat(subfix)));

    out.write(s);

    out.flush();
    out.close();

}

Here is what my html page should look like:

<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" xmlns:jsp="http://java.sun.com/JSP/Page"%>

<%@ page import="net.tanesha.recaptcha.ReCaptcha" %>
<%@ page import="net.tanesha.recaptcha.ReCaptchaFactory" %>
<!DOCTYPE html>
  <html>
    <head>
      <title>
        Forms
      </title>
    </head>
    <body>   
      <% ReCaptcha c = ReCaptchaFactory.newReCaptcha("pubic-key ", "private-key ", false);
         out.print(c.createRecaptchaHtml(null, null));%>
    </body>
  </html>

Instead of a recapcha widget i get the string inside the

Now i don't know if is the right way to display recapcah, but i am open to any suggestions, i just want to display it from my servlet.

The reCAPTCHA Java Library provides a simple way to place a CAPTCHA on your Java-based website, helping you stop bots from abusing it. The library wraps the reCAPTCHA API.

Detailed step by step tutorial is provided here

After few hours of researching and lots of headeaks i have manage to come up whit a right answer. I just use this code in my capchadisplay string:

<script type="text/javascript"
   </script>
<noscript>
   <iframe src="http://www.google.com/recaptcha/api/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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM