1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
...
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet captchaVerif</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Votre Captcha est invalide " + request.getContextPath() + "</h1>");
out.println("<h1>Veuillez <a href='index.jsp'>réessayer</a></h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
....
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
String remoteAddr = request.getRemoteAddr();
ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
reCaptcha.setPrivateKey("6LfqPx8TAAAAAHGYhRS4W6PVFyCV3fJwo74NqNxl");
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!");
RequestDispatcher disp = request.getRequestDispatcher("inscription.jsp");
disp.forward(request, response);
} else {
processRequest(request, response);
}
} |