1,首先訪問http://code.google.com/intl/zh-CN/apis/recaptcha/intro.html,然后點擊左側相應語言,博主示例用的jsp,所以下載jar包,可直接【點此下載jar包】解壓zip中的recaptcha4j-0.0.7.jar至web項目lib下。
2,訪問https://www.google.com/recaptcha/admin/create並用google賬戶登錄,在文本框輸入自己網站的網址,如global-key.mycompany.com ,點擊create key,生成Public Key和Private Key。
3,在jsp中編寫示例form
<%@ page import="net.tanesha.recaptcha.ReCaptcha" %>
<%@ page import="net.tanesha.recaptcha.ReCaptchaFactory" %>
<html>
<body>
<form action="" method="post">
<%
ReCaptcha c = ReCaptchaFactory.newReCaptcha("填入之前生成的public_key", "填入之前生成的private_key", false);
out.print(c.createRecaptchaHtml(null, null));
%>
<input type="submit" value="submit" />
</form>
</body>
</html>
4,編寫示例服務端接收校驗
<%@ page import="net.tanesha.recaptcha.ReCaptchaImpl" %>
<%@ page import="net.tanesha.recaptcha.ReCaptchaResponse" %>
<html>
<body>
<%
String remoteAddr = request.getRemoteAddr();
ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
reCaptcha.setPrivateKey("填入之前生成的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()) {
out.print("Answer was entered correctly!");
} else {
out.print("Answer is wrong");
}
%>
</body>
</html>