简体   繁体   English

使用AJAX验证JSP页面中的验证码

[英]Validate Captcha in JSP Page using AJAX

Trying to Use Captcha in my JSP page as below 尝试在我的JSP页面中使用验证码,如下所示

<%@ page import="net.tanesha.recaptcha.ReCaptcha" %>
<%@ page import="net.tanesha.recaptcha.ReCaptchaFactory" %>
<html>
<head>
<title>Sample Application JSP Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" language="javascript" src="ajax.js"></script>
</head>

<body bgcolor=white>
<form action="CaptchaServlet">
<table border="0" cellpadding="10">
<tr>
<td width="10" align="center" height="10">
<img src="SimpleServletPath">
</td>
<td>
<h1>Sample Application JSP Page</h1>
</td>
</tr>
<tr>
<td>
Please Enter your Comments
<p>
<%
ReCaptcha c = ReCaptchaFactory.newReCaptcha   
("6LdlHOsSAAAAAM8ypy8W2KXvgMtY2dFsiQT3HVq-    ", "6LdlHOsSAAAAACe2WYaGCjU2sc95EZqCI9wLcLXY", false);
out.print(c.createRecaptchaHtml(null, null));
%>
<INPUT TYPE="TEXT" NAME="text1">
<input type="submit" value="submit" />
</p>  
</td>
</tr>
</table>

</form>
</body>
</html> 

The servlet is as follows servlet如下

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

    String remoteAddr = request.getRemoteAddr();
    ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
    reCaptcha.setPrivateKey("6LdlHOsSAAAAACe2WYaGCjU2sc95EZqCI9wLcLXY");

    String challenge = request.getParameter("recaptcha_challenge_field");
    String uresponse = request.getParameter("recaptcha_response_field");
    ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
    PrintWriter out= response.getWriter();
    if (reCaptchaResponse.isValid()) {
        String user = request.getParameter("user");
        out.write("CAPTCHA Validation Success! User "+user+" registered.");
    } else {
        out.write("CAPTCHA Validation Failed! Try Again.");
    }   
}

This works good, but the JSP page gets refreshed when submit value is clicked. 这很好用,但是单击提交值时会刷新JSP页面。 How can we pass the Captcha values to Servlet using AJAX and return a value that the Capcha is valid or not without refreshing the page. 我们如何使用AJAX将验证码值传递给Servlet并返回一个验证码有效或无效的值,而无需刷新页面。

Here is the strategy. 这是策略。

Have your submit tracked by a javascript method. 用javascript方法跟踪您的提交。 That method will send the captcha data to the server. 该方法会将验证码数据发送到服务器。 and on sucesss or error the javascript will update the dom with the error message sent by the server/Servlet. 并且在成功或错误时,javascript将使用服务器/ Servlet发送的错误消息来更新dom。

Follow this link https://gist.github.com/madan712/4972634 . 请点击此链接https://gist.github.com/madan712/4972634

In the link above it uses another jsp to validate ( working like a servlet) but you can give the url mapping name there in the url:[your_servlet_path] 在上面的链接中,它使用另一个jsp进行验证(类似于servlet),但是您可以在url中提供url映射名称:[your_servlet_path]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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