博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
kotlin web_Kotlin Web应用程序教程
阅读量:2534 次
发布时间:2019-05-11

本文共 8978 字,大约阅读时间需要 29 分钟。

kotlin web

In this tutorial, we’ll learn how to create Kotlin Web Application. We assume that you’re well versed with and before going down the business end of this tutorial.

在本教程中,我们将学习如何创建Kotlin Web应用程序。 我们假设您在开始学习本教程的业务之前对和十分熟悉。

Kotlin Web应用程序 (Kotlin Web Application)

kotlin web app
Java EE is an extension of the Java SE and is used for creating web applications, and essentially anything related to web (not to forget microservices).

Java EE是Java SE的扩展,用于创建Web应用程序, 以及与Web相关的任何内容(不要忘记微服务)。

We’re also well aware that Kotlin, the latest language developed by JetBrains is statically typed and is based on JVM. It’s a fact that Kotlin is there with an aim to address the issues that Java Developers face.

我们也很清楚,JetBrains开发的最新语言Kotlin是静态类型的,并且基于JVM。 Kotlin的存在就是为了解决Java开发人员面临的问题,这是事实。

Kotlin is concise, clear and has a very friendly syntax to learn and adapt to. Moreover, it is 100% interoperable with Java, compiles easily to Java 6 and Java 8 bytecode.

Kotlin简洁明了,并具有非常友好的语法来学习和适应。 此外,它与Java 100%互操作,可轻松编译为Java 6和Java 8字节码。

Knowing that Kotlin has so many advantages over Java, why not adopt it in our Java EE environment.

知道Kotlin比Java具有许多优势,为什么不在我们的Java EE环境中采用它。

That’s what this tutorial is all about. We’ll be developing a simple using Kotlin, Servlets and Tomcat localhost server. Before that let’s see the challenges faced in integrating Kotlin in a Java EE Application.

这就是本教程的全部内容。 我们将使用Kotlin,Servlet和Tomcat localhost服务器开发一个简单的 。 在此之前,让我们看看将Kotlin集成到Java EE应用程序中所面临的挑战。

  • Kotlin classes are final by default : Unless a class is declared as open class, it’s final by default. Most of the time, java ee application classes aren’t final, so instead of defining a class as open class every time (failing to do so can lead to a runtime exception), we can integrate the all-open plugin in our IntelliJ Idea build.gradle file.

    Kotlin类默认为final :除非将某个类声明为open class ,否则默认为final。 在大多数情况下,java ee应用程序类不是最终的,因此我们不必每次都将类定义为open class (否则可能会导致运行时异常),我们可以将all-open插件集成到IntelliJ Idea中build.gradle文件。
  • Kotlin by default has constructors with named arguments: The no-arg compiler plugin generates an additional zero-argument constructor for classes with a specific annotation thereby allowing us to instantiate classes without a constructor explicitly specified.

    默认情况下,Kotlin的构造函数带有命名参数 :no-arg编译器插件会为带有特定批注的类生成一个附加的零参数构造函数,从而使我们可以在没有显式指定构造函数的情况下实例化类。

Following are the modifications to be done in the build.gradle file of our project.

以下是我们项目的build.gradle文件中要进行的修改。

buildscript {    dependencies {        classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"        classpath "org.jetbrains.kotlin:kotlin-noarg:$kotlin_version"    }}apply plugin: "kotlin-noarg"apply plugin: "kotlin-allopen"

We’ll be creating an Http Web Application using Servlets in Kotlin.

Note: A at its very core is a class which can handle HTTP requests (generally GET and POST requests).
Let’s create our first Hello World Web Application using Kotlin in . That’s what is required for creating Java EE Applications.

我们将在Kotlin中使用Servlet创建一个Http Web应用程序。

注意 : 核心是可以处理HTTP请求(通常是GET和POST请求)的类。
让我们在使用Kotlin创建第一个Hello World Web应用程序。 这就是创建Java EE应用程序所需要的。

Kotlin Web App项目 (Kotlin Web App Project)

Select Gradle from the side bar, and choose the Kotlin(JVM) and Web in the right.

kotlin web application javaee project type

从侧栏中选择Gradle,然后在右侧选择Kotlin(JVM)和Web。

Setting the group, artifact and gradle settings as shown below.

如下所示设置组,工件和gradle设置。

Note: An artifact is an assembly of your project assets that you put together to test, deploy or distribute your software solution or its part. It’ll be invoked when we run our application on the server.

注意 :工件是项目资产的组合,将它们组合在一起以测试,部署或分发软件解决方案或其部分。 当我们在服务器上运行应用程序时,将调用它。

Configuring build.gradle

配置build.gradle

buildscript {    ext.kotlin_version = '1.2.10'    repositories {        mavenCentral()    }    dependencies {        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"    }}group 'com.journaldev.kotlinjavaee'version '1.0-SNAPSHOT'apply plugin: 'kotlin-platform-jvm'repositories {    mavenCentral()}dependencies {    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"    testCompile "junit:junit:4.12"    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"    testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"    testCompile group: 'junit', name: 'junit', version: '4.11'}compileKotlin {    kotlinOptions.jvmTarget = "1.8"}compileTestKotlin {    kotlinOptions.jvmTarget = "1.8"}

Let’s add the war plugin and the java ee dependency.

让我们添加war插件和java ee依赖项。

buildscript {    ext.kotlin_version = '1.2.10'    repositories {        mavenCentral()    }    dependencies {        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"    }}group 'com.jour.as'version '1.0-SNAPSHOT'apply plugin: 'kotlin-platform-jvm'apply plugin: 'war'repositories {    mavenCentral()}dependencies {    compile group: 'javax', name: 'javaee-api', version: '7.0'    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"    testCompile "junit:junit:4.12"    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"    testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"    testCompile group: 'junit', name: 'junit', version: '4.11'}compileKotlin {    kotlinOptions.jvmTarget = "1.8"}compileTestKotlin {    kotlinOptions.jvmTarget = "1.8"}

The War plugin extends the Java plugin to add support for assembling web application WAR files.

War插件扩展了Java插件,从而增加了对组装Web应用程序WAR文件的支持。

Kotlin Web App项目结构 (Kotlin Web App Project Structure)

kotlin javaee project structure
Since we’re dealing with Servlet only in this tutorial, let’s delete the jsp file. Create a new directory inside webapp, namely
WEB-INF and add a file
web.xml to it.

由于本教程仅涉及Servlet,因此我们删除jsp文件。 在webapp中创建一个新目录,即WEB-INF并向其中添加文件web.xml

The code for the MyServlet.kt (no .java extension) is given below.

下面给出了MyServlet.kt (无.java扩展名)的代码。

import javax.servlet.annotation.WebServletimport javax.servlet.http.HttpServletimport javax.servlet.http.HttpServletRequestimport javax.servlet.http.HttpServletResponse@WebServlet(name = "Home", value = "/hello")class MyServlet : HttpServlet() {    override fun doGet(req: HttpServletRequest, res: HttpServletResponse) {        res.writer.write("Hello, World!")    }}

The @WebServlet is used to declare a servlet and map it to the specified values (servlet mapping). The value argument is compulsory. The name argument is optional over here.

@WebServlet 用于声明servlet并将其映射到指定的值(servlet映射)。 value参数是强制性的。 name参数在这里是可选的。

The doGet function is used to print the input onto the screen.

doGet函数用于将输入打印到屏幕上。

Thanks to the @WebServlet annotation, deployment descriptor (web.xml file) is not required.

由于使用@WebServlet批注,因此不需要部署描述符(web.xml文件)。

Note: The above annotation doesn’t work for versions below Tomcat 7.

注意 :上面的注释不适用于Tomcat 7以下的版本。

运行Kotlin Web应用程序 (Running the Kotlin Web Application)

Edit the Configuration from the top right menu button:

kotlin javaee edit config

从右上方的菜单按钮编辑配置:

Setting Tomcat (a popular servlet container) as the server.

kotlin javaee run edit configuration

将Tomcat(流行的servlet容器)设置为服务器。

We’ve set the port number from 8080 to 8888 since the former was already in use. We’ve added the url path to the Hello World servlet that’ll launch when the project is run.

由于前者已经在使用中,因此我们将端口号从8080设置为8888。 我们已经将URL路径添加到了Hello World servlet,它将在项目运行时启动。

Let’s set up our artifact that’s needed for deployment.

kotlin javaee deployment

让我们设置部署所需的工件。

Note: If Tomcat isn’t installed, goto IntelliJ->Applications->Plugins->Tomcat Server

注意 :如果未安装Tomcat,请转到IntelliJ-> Applications-> Plugins-> Tomcat Server

The output when the above project is run is given below.

kotlin web app output

运行上述项目时的输出如下。

使用web.xml进行Servlet映射 (Using web.xml for Servlet Mapping)

Instead of setting the annotation, url pattern in the MyServlet.kt file, we can set it in the web.xml file which acts as the deployment descriptor file as shown below.

无需在MyServlet.kt文件中设置注释,URL模式,我们可以在充当部署描述符文件的web.xml文件中进行设置,如下所示。

MyServlet
MyServlet
MyServlet
/hello/*

Let’s modify the MyServlet.kt file to display HTML as the input on the screen.

让我们修改MyServlet.kt文件以在屏幕上显示HTML作为输入。

import javax.servlet.annotation.WebServletimport javax.servlet.http.HttpServletimport javax.servlet.http.HttpServletRequestimport javax.servlet.http.HttpServletResponse//@WebServlet(name = "Home", value = "/hello")class MyServlet : HttpServlet() {    override fun doGet(req: HttpServletRequest, res: HttpServletResponse) {        //res.writer.write("Hello, World!")        res.setContentType("text/html")        val out = res.getWriter()        out.print("")        out.print("

JournalDev Tutorial

") out.print("

Servlet App Using Kotlin

") out.print("") }}

Following is the final output that gets displayed on the screen.

kotlin javaee output 2

以下是屏幕上显示的最终输出。

This brings an end to kotlin web application tutorial. You can download the final Kotlin web application project from the link below.

这样就结束了kotlin Web应用程序教程。 您可以从下面的链接下载最终的Kotlin Web应用程序项目

翻译自:

kotlin web

转载地址:http://dymzd.baihongyu.com/

你可能感兴趣的文章
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>