如何在java servlet的html中包含recaptcha?

时间:2013-11-29 09:02:49

标签: java jsp servlets recaptcha

我想在servlet的网页中包含recapcha插件。如果我使用<%@ ..%>或者它不会包含或显示任何回忆。

这是我的 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();

}

以下是我的 html页面应该是这样的:

<%@ 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>

而不是recapcha小部件我得到了

中的字符串

现在我不知道是否是显示recapcah的正确方法,但我对任何建议持开放态度,我只想从我的servlet中显示它。

2 个答案:

答案 0 :(得分:1)

reCAPTCHA Java Library提供了一种在基于Java的网站上放置CAPTCHA的简单方法,可帮助您阻止机器人滥用它。该库包装了reCAPTCHA API。

详细的分步教程提供了here

答案 1 :(得分:0)

经过几个小时的研究和大量的headeaks,我已经设法得到一个正确的答案。我只是在 capchadisplay 字符串中使用此代码:

<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>