本文共 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应用程序。 我们假设您在开始学习本教程的业务之前对和十分熟悉。
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应用程序中所面临的挑战。
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
文件。 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应用程序所需要的。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文件的支持。
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以下的版本。
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.
运行上述项目时的输出如下。
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.
以下是屏幕上显示的最终输出。
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/