大学4年,老师唯一让我们学习的web服务器是tomcat,配置方式是先从官网下载阿帕奇的tomcat文件,然后在开发平台导入,然后再配置web.xml等文件,

是一个可同步可异步请求的服务器框架;

直到我遇到vert.x框架,这东西其实就是全异步java服务器,底层是使用Netty运行的,因此,可将vert.x看作是个与tomcat类似但是使用方式不同的服务器,

搭建vert.x服务器不需要单独下载文件再去开发平台单独配置信息,也不需要配置web.xml文件,

只需要运行一次主函数,即可启动程序,然后使用路由管理,可分多级路由拦截路径访问响应的内容或信息,访问方式可在路由设置,包括get、post等,默认是get方式,

参数传输在前端没有什么改变,支持url后的参数写法,也支持restful参数写法,post传输则需要指定一个BodyHandle,然后才能通过requestContext对象来获取body体中的数据,包括json和formData数据

package xue.myVertX;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;

/**
 *  简单的路由使用
 */
public class SimpleRouter extends AbstractVerticle {
    @Override
    public void start() throws Exception {
        // 创建HttpServer
        HttpServer server = vertx.createHttpServer();
        // 创建路由对象
        Router router = Router.router(vertx);
        // 监听/index地址
        router.route("/index").handler(request -> {
            request.response().end("INDEX SUCCESS");
        });

// 获取参数

//        router.route(HttpMethod.GET, "/method/:user/:pass").handler(request -> {
//            String user = request.request().getParam("user");
//            String pass = request.request().getParam("pass");
//            request.response()
//                    .putHeader("Content-type", "text/html;charset=utf-8")
//                    .end("接收到的用户名为:" + user + " 接收到的密码为:" + pass);
//        });
        
        // 把请求交给路由处理--------------------(1)
        server.requestHandler(router::accept);
        server.listen(8080);
    }

    public static void main(String[] args) {
        Vertx.vertx().deployVerticle(new SimpleRouter());
    }
}
View Code

相关文章:

  • 2021-11-15
  • 2021-09-21
  • 2022-01-25
  • 2021-06-23
  • 2022-01-01
  • 2021-09-20
  • 2021-05-14
猜你喜欢
  • 2021-08-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2021-05-17
相关资源
相似解决方案