12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
|
sing reCAPTCHA with Java/JSP
Important: This is an old version of reCAPTCHA API. For the latest version, please refer to Version 2.0.
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.
To use reCAPTCHA with Java/JSP, you can download the reCAPTCHA Java Library here (contributed by Soren) and unzip it. Typically the only thing you'll need is the jar file (recaptcha4j-X.X.X.jar), which you have to copy to a place where it can be loaded by your java application. For example, if you are using Tomcat to run JSP, you may put the jar file in a directory called WEB-INF/lib/.
Quick Start
After you've signed up for your API keys and downloaded the reCAPTCHA Java Library, below are basic instructions for installing reCAPTCHA on your site.
Client Side (How to make the CAPTCHA image show up)
If you want to use the Java plugin to display the reCAPTCHA widget, you'll need to import the appropriate reCAPTCHA classes. In JSP, you would do this by inserting these lines near the top of the file with the form element where the reCAPTCHA widget will be displayed:
<%@ page import="net.tanesha.recaptcha.ReCaptcha" %>
<%@ page import="net.tanesha.recaptcha.ReCaptchaFactory" %>
Then, you need to create an instance of reCAPTCHA:
ReCaptcha c = ReCaptchaFactory.newReCaptcha("your_public_key", "your_private_key", false);
Finally, the HTML to display the reCAPTCHA widget can be obtained from the following function call:
c.createRecaptchaHtml(null, null)
So, in JSP your code may look something like this:
<%@ page import="net.tanesha.recaptcha.ReCaptcha" %>
<%@ page import="net.tanesha.recaptcha.ReCaptchaFactory" %>
<html>
<body>
<form action="" method="post">
<%
ReCaptcha c = ReCaptchaFactory.newReCaptcha("your_public_key", "your_private_key", false);
out.print(c.createRecaptchaHtml(null, null));
%>
<input type="submit" value="submit" />
</form>
</body>
</html>
Don't forget to replace your_public_key
and your_private_key
with your API key values.
Server Side (How to test if the user entered the right answer)
In the application that verifies your form, you'll first need to import the necessary reCAPTCHA classes:
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
Next, you need to insert the code that verifies the reCAPTCHA solution entered by the user. The example below (in JSP) shows how this can be done:
<%@ page import="net.tanesha.recaptcha.ReCaptchaImpl" %>
<%@ page import="net.tanesha.recaptcha.ReCaptchaResponse" %>
<html>
<body>
<%
String remoteAddr = request.getRemoteAddr();
ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
reCaptcha.setPrivateKey("your_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>
'server > 팁' 카테고리의 다른 글
[SKT][핸드폰][스마트폰] 팅요금제의 모르는 진실?! / 팅요금제쓰는데 문화 상품권 어떻게 얻죠 ? / 팅 요금제 문화상품권 얻는 법 (0) | 2016.02.27 |
---|---|
[안드로이드] 대출 전화 그만왔으면 , 전화왔는데 누군지 모르겠다. 네이버 후스콜 설치 (1) | 2016.02.26 |
자동가입방지 CAPTCHA / CAPTCHA 예제 / CAPTCHA 사용법 / CAPTCHA example (1) | 2016.02.25 |
벅스 스마트폰에서 데이터 사용 없이 노래 듣기~! (0) | 2016.02.25 |
구글 블러거 테마 적용 시 페이스 북에 있는 프로필 이미지 가져오기! (0) | 2016.02.25 |