Kotlin Web Application Tutorial With Examples

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

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.

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.

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

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

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

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 Web App Project

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

kotlin-javaee-project-type

 

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

kotlin-javaee-project-type

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.

kotlin-javaee-project-gradle-settings

Configuring build.gradle

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

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

Kotlin Web App Project Structure

kotlin-javaee-project-gradle-settings
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.

android-webview-bookmarks-project-structure

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

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.

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

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

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

Running the Kotlin Web Application

Edit the Configuration from the top right menu button

Setting Tomcat (a popular servlet container) as the server.
kotlin javaee run edit configuration

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.

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

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

kotlin-javaee-deployment

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.

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

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

kotlin-javaee-deployment

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

By admin

Leave a Reply

%d bloggers like this: