0

My first Vertx Web app :

I expect To get the index.html at localhost.8080/Test then find a way to retrieve the data, but the page doesn't show

I have a RequestResponseExample class:

public class RequestResponseExample extends AbstractVerticle {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();

        Router router = Router.router(vertx);

        router.post("/Test").handler(rc -> rc.response().sendFile("index.html"));

        vertx.createHttpServer()
            .requestHandler(router)
            .listen(8080);
    }

}

And My Html Code index.html

<html>
<head>
  <meta charSet="UTF-8">
  <title>OTP Authenticator Verification Example Page</title>
</head>
<body>
<form action="/" method="post" encType="multipart/form-data">
  <div>
    <label>Code:</label>
    <input type="text" name="code"/><br/>
  </div>
  <div>
    <input type="submit" value="Submit"/>
  </div>
</form>
</body>
</html>

2 Answers2

2

Solution

Change router.post( to router.get(.

Description

Currently, you are configuring the Router to only handle HTTP POST request. That means, it is configured to respond to such an HTTP request:

POST /Test

But when you try to open localhost.8080/Test in your browser, it will send such a request to your server:

GET /Test

This is why you have to tell the router to handle GET and not POST requests.

Additional information: GET and POST are so called HTTP request methods. If you want to learn more about that, I recommend you to read the following article: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

About Verticles

In your code, you can remove extends AbstractVerticle and it will work the same way. If you want your code to get executed in the context of a verticle you have to create an instance of your class and then you have to deploy it:

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;

public class RequestResponseExample extends AbstractVerticle {

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();

        vertx.deployVerticle(new RequestResponseExample());
    }

    @Override
    public void start(){
        Router router = Router.router(vertx);

        router.get("/Test").handler(rc -> rc.response().sendFile("index.html"));
        router.post().handler(BodyHandler.create());
        router.post("/").handler(rc -> System.out.println(rc.request().formAttributes().get("code")));

        vertx.createHttpServer()
            .requestHandler(router)
            .listen(8080);
    }

}

Since I see a bit of confusion on your side, you may want to also read the following article about Verticles: https://vertx.io/docs/vertx-core/java/#_verticles

  • I Used Main to deploy REQUEST and RESPONSE verticle I Used router.get(" /Test ").handler( rc -> rc.response().sendFile(" index.html ") ); on The request side [Works]. I'm having a problem on how to get the value using POST Method on the RESPONSE verticle though – RABII SISSI Feb 07 '22 at 11:21
  • @RABIISISSI: If you want the Router to also accept the form data that gets send by the browser when the user submits the form from the index.html you have to add two additional handlers: `router.post(BodyHandler.create())` and after that `router.post("/").handler(rc -> System.out.println(rc.request().formAttributes().get("code")))` – Jonas Taulien Feb 08 '22 at 14:06
  • @RABIISISSI additionally: If I was able to answer your question(s), please accept this answer as solution – Jonas Taulien Feb 08 '22 at 14:07
  • @RABIISISSI I have added the changes to the example code in the "About Verticles"-section – Jonas Taulien Feb 08 '22 at 14:15
1

I think that you send a GET request, but you handle a POST request to return the html file. I think firstly you have to handle a GET request that's return the html page, and also write a route for the form.