1、依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId>
<artifactId>kotlin-vertx</artifactId>
<version>1.0-SNAPSHOT</version> <properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.8.20</kotlin.version>
</properties> <dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency> <dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-auth-jdbc</artifactId>
</dependency> <dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-codegen</artifactId>
</dependency> <dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-service-proxy</artifactId>
</dependency> <dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency> <dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-auth-common</artifactId>
</dependency> <dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-coroutines-core</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>target/generated-sources/annotations</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>${maven.compiler.target}</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build> <dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-dependencies</artifactId>
<version>4.4.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> </project>

2、表单

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<form method="post" action="http://localhost:8080/index">
姓名: <input type="text" name="name" ><br>
密码: <input type="password" name="password"> <br>
<input type="submit">
</form>
</body>
</html>

3、完整代码

package org.example.kotlin_web

import io.vertx.core.AbstractVerticle
import io.vertx.core.Vertx
import io.vertx.core.buffer.Buffer
import io.vertx.core.json.JsonArray
import io.vertx.core.json.JsonObject
import io.vertx.ext.jdbc.JDBCClient
import io.vertx.ext.web.Router
import io.vertx.ext.web.RoutingContext class PostHttp :AbstractVerticle(){
override fun start() {
var router = Router.router(vertx);
router.post().path("/index").handler { ctx: RoutingContext ->
val request = ctx.request()
val response = ctx.response()
response.putHeader("Content-Type", "text/plain; charset=UTF-8")
val formAttributes = request.formAttributes()
request.bodyHandler { body: Buffer ->
val formData = body.toString()
println("Received form data: $formData")
response.setStatusCode(200)
response.end("Form submitted successfully")
}
}
}
} fun main(){
var vertx = Vertx.vertx();
val jdbcClient = JDBCClient.createShared(
vertx, JsonObject()
.put("url", "jdbc:mysql://localhost:3306/vertxTest")
.put("driver_class", "com.mysql.cj.jdbc.Driver")
.put("user", "root")
.put("password", "123456")
)
jdbcClient.getConnection { res ->
if (res.succeeded()) {
val connection = res.result()
val params = JsonArray().add("lisi").add(123) connection.queryWithParams("select id from kotlin where name=? and password=?", params) { queryResult ->
if (queryResult.succeeded()) {
val result = queryResult.result()
println("成功")
} else {
val error = queryResult.cause()
println("失败")
} // 关闭连接
connection.close()
}
} else {
val error = res.cause()
println("连接失败")
}
}
}

后面该搞ktor了

vertx 的http服务表单提交与mysql验证的更多相关文章

  1. EasyUI中在表单提交之前进行验证

    使用EasyUi我们可以在客户端表单提交之前进行验证,过程如下:只需在onSubmit的时候使用return  $("#form1").form('validate')方法即可,E ...

  2. AngularJS 表单提交后显示验证信息与失焦后显示验证信息

    虽然说AngularJS的实时表单验证非常有用,非常高效方便,但是当用户还没有完成输入时便弹出一个错误提示,这种体验是非常糟糕的. 正常的表单验证逻辑应该是在用户提交表单后或完成当前字段中的输入后,再 ...

  3. Form表单提交,js验证

    Form表单提交,js验证 1,  Onclick() 2, Onsubmit() Button标签 input (属性 submit  button )标签 Input type=button    ...

  4. form的onsubmit事件--表单提交前的验证最佳实现方式

    今天遇到了一个问题,页面中include了很多的公共页面(都是没有form的),并且里面好多的地方都是自行提交的(页面中加入一个type=“submit”域,然后js中写入sumbit.click来执 ...

  5. 【jquery采坑】Ajax配合form的submit提交(微擎表单提交,ajax验证,submit提交)

    1.采坑:实现form的submit提交,在提交之前,进行ajax的不同校验,然后onsubmit=return check(),进行提交 1/1 目的:可以实现以 from的submit提交,然后还 ...

  6. Ajax表单提交插件jquery form

    jQuery Form插件是一个优秀的Ajax表单插件,我们可以非常容易的使用它处理表单控件的值,清空和复位表单控件,附件上传,以及完成Ajax表单提交. jQuery Form有两个核心方法ajax ...

  7. a标签指定的url,在表单提交前进行js验证的实现

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. yii2表单提交CSRF验证

    Yii2表单提交默认需要验证CSRF,如果CSRF验证不通过,则表单提交失败,解决方法如下: 第一种解决办法是关闭Csrf public $enableCsrfValidation = false; ...

  9. Flask基础(16)-->WTForms表单创建和简单验证

    Flask基础(16)-->WTForms表单创建和简单验证 前言:使用Flask_WTF需要配置参数SECRET_KEYCSRF_ENABLED是为了CSRF(跨站请求伪造)保护.SECRET ...

  10. Swift3.0服务端开发(二) 静态文件添加、路由配置以及表单提交

    今天博客中就来聊一下Perfect框架的静态文件的添加与访问,路由的配置以及表单的提交.虽然官网上有聊静态文件的访问的部分,但是在使用Perfect框架来访问静态文件时还是有些点需要注意的,这些关键点 ...

随机推荐

  1. 2023CISCN华中赛区re

    2023CISCN华中赛区re 当时出的题 misc3-babyandroid 找so文件,加密过程也不复杂 每三个一组进行加密 这里就是先每个减去65 然后 大概是 y1=(31x1)%26+65 ...

  2. PicGo+Github图床配置

    为了将 PicGo 设置为使用 GitHub 作为图床,您需要先创建一个 GitHub 仓库用于存储图片,然后在 PicGo 中进行相应的配置.您已经创建了一个仓库,所以让我们来配置 PicGo. 安 ...

  3. JWT(Json Wen Token)原理剖析

    JWT(即json web token),大家先看下面这张图 大家可以观察到,jwt String就是生成后的jwt字符集,其中有两个 "."(注意:jwt校验会对".& ...

  4. Nomad 系列-Nomad 挂载存储卷

    系列文章 Nomad 系列文章 概述 显然,如果 Nomad 要运行有状态存储,那么挂载存储卷就是必备功能. Nomad 允许用户通过多种方式将持久数据从本地或远程存储卷装载到任务环境中: 容器存储接 ...

  5. 探索计算机的I/O控制方式:了解DMA控制器的作用与优势

    I/O控制方式 在前面我们已经了解到,每个设备都配备了一个设备控制器.当CPU向设备控制器发送命令并将其存储在寄存器中时,设备控制器会执行相应的操作.然而,尽管设备控制器会更新状态寄存器的状态,但是如 ...

  6. 关于.Net 6.0 在Linux ,Docker容器中,不安装任何依赖就生成图形验证码!!!!!!!!!!!

    在.Net Framework时代,我们生成验证码大多都是用System.Drawing. 在.Net 6中使用也是没有问题的. 但是,System.Drawing却依赖于Windows GDI+. ...

  7. 10.0 探索API调试事件原理

    本章笔者将通过Windows平台下自带的调试API接口实现对特定进程的动态转存功能,首先简单介绍一下关于调试事件的相关信息,调试事件的建立需要依赖于DEBUG_EVENT这个特有的数据结构,该结构用于 ...

  8. 【vulnhub】——DC-9靶机

    [vulnhub]--DC-9靶机 1. 主机发现 扫描kali主机C段(Kali和DC-9主机在同一个网关下): 发现主机为192.168.108.146,进行详细端口扫描: 可以看到靶机开了一个s ...

  9. 用AI打造一个属于自己的歌手,让她C位霸气出道

    ​ 一.前言 今天玩儿点儿特别的,AI大行其道的今天,还没玩过AI模型的程序员绝对不是个好厨子.我本人比较喜欢音乐,但是一直没有出道,很是遗憾.那么今天,我就使用AI模型亲手打造一个堪比真人的歌手,让 ...

  10. C++快读、快写模版

    inline int read() { char ch = getchar(); int x = 0,f = 1; while (!isdigit(ch)) if (ch == '-') f = -1 ...