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>
Solution
Change
router.post(torouter.get(.Description
Currently, you are configuring the Router to only handle HTTP
POSTrequest. That means, it is configured to respond to such an HTTP request:But when you try to open
localhost.8080/Testin your browser, it will send such a request to your server:This is why you have to tell the router to handle
GETand notPOSTrequests.Additional information:
GETandPOSTare so calledHTTP 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/MethodsAbout Verticles
In your code, you can remove
extends AbstractVerticleand 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: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