kotlin web

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

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

Kotlin Web应用程序 (Kotlin Web Application)

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

Java EE是Java SE的扩展,用于创建Web应用程序, 其余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 Web Application 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服务器开发一个简单的Web应用程序 。 在此之前,让我们看看将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 servlet 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 IntelliJ Ultimate. That’s what is required for creating Java EE Applications.

我们将在Kotlin中使用Servlet创建一个Http Web应用程序。
注意 : Servlet的核心是可以处理HTTP请求(通常是GET和POST请求)的类。
让我们在IntelliJ Ultimate中使用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.

从侧栏中选择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)

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.WebServlet
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import 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 annotation 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:

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

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

将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.

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

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.

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

使用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文件中进行设置,如下所示。

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.1"xmlns="https://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="https://xmlns.jcp.org/xml/ns/javaee https://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"metadata-complete="false"><servlet><servlet-name>MyServlet</servlet-name><servlet-class>MyServlet</servlet-class></servlet><servlet-mapping><servlet-name>MyServlet</servlet-name><url-pattern>/hello/*</url-pattern></servlet-mapping></web-app>

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

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

import javax.servlet.annotation.WebServlet
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import 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("<html><body>")out.print("<h3>JournalDev Tutorial</h3>")out.print("<h4>Servlet App Using Kotlin</h4>")out.print("</body></html>")}
}

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

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

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应用程序项目

Download Kotlin Web Application Project下载Kotlin Web应用程序项目

翻译自: https://www.journaldev.com/17957/kotlin-web-application-tutorial

kotlin web

kotlin web_Kotlin Web应用程序教程相关推荐

  1. 使用VS Code开发.Net Core 2.0 MVC Web应用程序教程之三(配置文件读取)

    干了一天的活,还有点时间,给兄弟们写点东西吧. 大家有没有发现一个问题?那就是在.Net Core的MVC项目里面,没有.config文件了!!!同志们,没有config文件了啊,这样搞,我以后要做些 ...

  2. 使用VS Code开发.Net Core 2.0 MVC Web应用程序教程之一

    好吧,现在我们假设你已经安装好了VS Code开发工具..Net Core 2.0预览版的SDK dotnet-sdk-2.0.0(注意自己的操作系统),并且已经为VS Code安装好了C#扩展(在V ...

  3. 操作方法:具有多个Mongo存储库和Kotlin的Spring Boot 2 Web应用程序

    首先,免责声明:如果您正在编写微服务 (每个人现在都对吗?)并希望它是惯用的 ,那么通常不会在其中使用几个不同的数据源. 图片取自Pixabay© https: //pixabay.com/illus ...

  4. framer x使用教程_如何使用Framer Motion将交互式动画和页面过渡添加到Next.js Web应用程序

    framer x使用教程 The web is vast and it's full of static websites and apps. But just because those apps ...

  5. ASP .NET Core Web MVC系列教程:使用ASP .NET Core创建MVC Web应用程序

    本系列教程翻译自微软官方教程,官方教程地址:Get started with ASP.NET Core MVC | Microsoft Docs 本系列教程介绍了构建MVC Web应用程序的基础知识. ...

  6. ASP.NET Core Web Razor Pages系列教程:使用ASP.NET Core创建Razor Pages Web应用程序

    ASP .Net Core Razor Pages MySQL Tutorial 本系列教程翻译自微软官方教程,官方教程地址:Tutorial: Create a Razor Pages web ap ...

  7. r语言中的shiny教程_如何使用Shiny在R中编写Web应用程序

    r语言中的shiny教程 新年快乐! 这个月我忙于撰写一些较大的文章,因此请在接下来的几周内查找这些文章. 对于本月的Nooks和Crannies,我想简要指出一个我一直在用它进行自我教育的出色R库. ...

  8. Abp vnext Web应用程序开发教程 10 —— 书与作者的关系

    文章目录 关于本教程 下载源代码 介绍 向书实体添加关系 数据库和数据迁移 更新EF核心映射 添加新的EF核心迁移 更改数据播种器 应用层 数据传输对象 IBookAppService BookApp ...

  9. Abp vnext Web应用程序开发教程 9 —— 作者:用户界面

    文章目录 关于本教程 下载源代码 介绍 图书列表页面 Index.cshtml IndexModel.cshtml.cs Index.js 本地化 添加到主菜单 运行应用程序 创建模态 CreateM ...

最新文章

  1. c# 通过API启动外部程序
  2. numpy 辨异(三)—— hstack/column_stack,linalg.eig/linalg.eigh
  3. java 不能使用foreach_为什么我不能在Java Enumeration上使用foreach?
  4. 08. 旋转数组的最小数字(C++版本)
  5. 485通信c语言讲解,51单片机485通讯讲解 通俗易懂
  6. nohup启动jar_nohup命令详解
  7. Java web 实现视频在线播放的常用几种方法
  8. 梦幻西游的服务器能修改吗,【梦幻西游端游互通版修真西游】Win服务端+架设教程+各种修改教程...
  9. 「小车看百度,大车看深兰」,自动驾驶公交驶向千亿蓝海市场
  10. php 正则 tr,js正则匹配table tr
  11. css立体翻页,[原创]纯CSS3打造的3D翻页翻转特效
  12. UID/DID/SID总结
  13. “华为杯”第十七届中国研究生 数学建模竞赛-【华为杯】D题:无人机集群协同对抗(附优秀论文及python代码实现)
  14. 网络版五子棋程序的开发
  15. 实验........
  16. 185-186 抽象类+接口案例/运动员和教练
  17. 读《怪诞行为学》有感
  18. 计算机语言的发明者,人工智能之父与Lisp编程语言发明人麦卡锡去世
  19. 阿里巴巴-口碑-上海-Java一面部分面试题
  20. 酒店管理系统代码 java_酒店管理系统源代码

热门文章

  1. 深蓝词库转换1.6发布
  2. 【转】[Python Tip]如何在Windows下方便地进入命令行运行程序
  3. [转载] python中@property装饰器
  4. [转载] python数据类型转换
  5. [转载] OpenCV-Python图像位与运算bitwise_and函数详解
  6. [转载] 使用Python在Pandas Dataframe中建立索引
  7. [转载] python类内部成员的访问及外部访问(入门)
  8. 02 java多线程基础
  9. Choerodon 的微服务之路(二):Choerodon 的微服务网关
  10. Flocker 做为后端存储代理 docker volume-driver 支持