출저 : http://gnujava.com/board/article_view.jsp?article_no=7193&menu_cd=16&sch_field=TITLE&idx_notice=NOTICE_FLAG+DESC%2C&board_no=3&sch_word=CAPTCHA

JSP example - Google Recaptcha validation using jquery (AJAX)

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
<%
String remoteip = request.getRemoteAddr();
%>
<!doctype html>
<html>
<head>
<title> Recaptcha validation using jquery ajax </title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="recaptcha_ajax.js"></script>
<script type="text/javascript">
function reloadRecaptcha() {
var publicKey = "your_public_key";
var div = "recap";
Recaptcha.create(publicKey,div,{theme: "white"});
return false;
}
function validate() {
var challenge = Recaptcha.get_challenge();
var response = Recaptcha.get_response();
var remoteip = "<%=remoteip%>";
$.ajax({
type: "POST",
url: "validateRecaptcha.jsp",
async: false,
data: {
remoteip: remoteip,
challenge: challenge,
response: response
},
success: function(resp) {
if(resp == "true") {
document.getElementById("message").innerHTML = "Perfect!";
}
else {
document.getElementById("message").innerHTML = "Incorrect Recaptcha! Please try again!";
reloadRecaptcha();
}
}
});
return false;
}
</script>
</head>
<body onload="return reloadRecaptcha();">
<form method="post" onsubmit="return validate();">
<table>
<tr>
<td colspan="2">
<div id="message" style="color:#ff0000; "></div>
<div id="recap"></div>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</body>
</html>




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>


+ Recent posts