[JAVA] Try implementing recaptcha with Jetty embedded.


I would like to implement reCAPTHCA on my service, https://mirm.jp, too, and I'm in trouble if it is accessed by post when creating a new server. It's a memorandum, so it's pretty rough.

Implementation on html

https://www.google.com/recaptcha/admin First of all, please register to get the recaptcha key.


<script src='https://www.google.com/recaptcha/api.js'></script>


Please mix in the form.

<div class="g-recaptcha" data-callback="clearcall" data-sitekey="Put your site key here"></div>
<input type="submit" value="Create!">

Java side implementation

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;

import org.json.JSONException;
import org.json.JSONObject;

    public class getJsonFromWeb {

        private static String readAll(Reader rd) throws IOException {
            StringBuilder sb = new StringBuilder();
            int cp;
            while ((cp = rd.read()) != -1) {
                sb.append((char) cp);
            return sb.toString();

        public static JSONObject readJsonFromUrl(String url) {
            try (InputStream is = new URL(url).openStream()) {
                BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
                String jsonText = readAll(rd);
                return new JSONObject(jsonText);
            } catch (IOException | JSONException e1) {
                return new JSONObject("{\"success\": false,\"error-codes\": \"jsondecodeerror\"}");


Make a query. .. ..
                if (recaptchacode == null) {
                    result = create_recaptchaerror;

                String secretKey = "----------";

                JSONObject jsoned = readJsonFromUrl("https://www.google.com/recaptcha/api/siteverify?secret="+secretKey+"&response="+recaptchacode);

                try {
                    if (!jsoned.getBoolean("success")) {
                        result = create_recaptchaerror;
                }catch (Exception e){
                    result = create_recaptchaerror;


It's a memo, but it looks like this.




Recommended Posts

Try implementing recaptcha with Jetty embedded.
Try using another Servlet container Jetty with Docker
Try DI with Micronaut
Try create with Trailblazer
Try WebSocket with jooby
Try WildFly with Docker
Try implementing Yubaba in Kinx
Try using GloVe with Deeplearning4j
Try using view_component with rails
Try DB connection with Java
Try gRPC with Java, Maven
Try implementing a WebFlux session
Try reading XML with JDOM
Try implementing a WebFlux filter