ZOFTINO.COM android and web dev tutorials

Developing Kotlin Web Application using Eclipse and Maven and Deploying it to Tomcat

This tutorial provides details about developing kotlin web application using Eclipse and Maven and deploying it to Tomcat.

First, you need to install latest JDK, Eclipse and Eclipse Maven plug-in. You can find step by step instructions in eclipse maven tutorial about installation and configuration.

Then you need to install eclipse kotlin plug-in from eclipse market place. This plug-in is used for kotlin programming.

Then download Tomcat and extract it to a folder and install eclipse tomcat plugin from eclipse market place.

Then create web application using maven. To do that in eclipse, click file menu, click new and other menu items, then expand maven, select maven project, click next, again click next, then select maven-archetype-webapp, click next, enter group id, artifact id and package info then click finish.

If you don’t see source folders in the created web project, right click the web project, then click properties menu item, then select Java Build Path, go to libraries, remove JRE system library, then click Add Library, then choose JRE System Library, click next, then select latest JDK and click apply and close. After this, you will see src/main/java and src/test/java folders in the web project.

Then add kotlin source folders. To do that, right click project, click new menu item, then click source folder, enter src/main/kotlin as folder name and click ok. Repeat the same steps for adding src/test/kotlin folder.

Then add kotlin source files. To do that, right click src/main/kotlin, click new menu item, click other, then expand kotlin, then select Kotlin File option, then enter package name and kotlin file name and click ok.

Then add servelet dependency to pom.xml.

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>

Then add kotlin dependency to pom.xml.

  <dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-stdlib</artifactId>
    <version>1.2.51</version>
  <dependency>

Then add the following build element with ktolin and java source folders to pom.

<plugins>
	<plugin>
		<artifactId>kotlin-maven-plugin</artifactId>
		<groupId>org.jetbrains.kotlin</groupId>
		<version>1.2.51</version>
		<executions>
			<execution>
				<id>compile</id>
				<goals>
					<goal>compile</goal>
				</goals>
				<configuration>
					<sourceDirs>
						<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
						<sourceDir>${project.basedir}/src/main/java</sourceDir>
					</sourceDirs>
				</configuration>
			</execution>
			<execution>
				<id>test-compile</id>
				<goals>
					<goal>test-compile</goal>
				</goals>
				<configuration>
					<sourceDirs>
						<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
						<sourceDir>${project.basedir}/src/test/java</sourceDir>
					</sourceDirs>
				</configuration>
			</execution>
		</executions>
	</plugin>
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
		<version>3.5.1</version>
		<executions>
			<execution>
				<id>default-compile</id>
				<phase>none</phase>
			</execution>
			<execution>
				<id>default-testCompile</id>
				<phase>none</phase>
			</execution>
			<execution>
				<id>java-compile</id>
				<phase>compile</phase>
				<goals>
					<goal>compile</goal>
				</goals>
			</execution>
			<execution>
				<id>java-test-compile</id>
				<phase>test-compile</phase>
				<goals>
					<goal>testCompile</goal>
				</goals>
			</execution>
		</executions>
	</plugin>
</plugins>

After saving pom.xml, right click project, click Maven menu item and then click Update Project.

Here is kotlin file in our example, which handles http request.

package com.zoftino.web.kotlin

import javax.servlet.annotation.WebServlet
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

@WebServlet(name = "coupon", value = "/coupons")
class CouponConstroller : HttpServlet() {
    override fun doGet(req: HttpServletRequest, res: HttpServletResponse) {		
        res.writer.write("flat 20% off on fashion, ");
		res.writer.write("upto 50% off on mobiles, ");
		
		res.writer.write(Deals.getDeals());
    }
}

Here is a java class which is called from kotlin class.

package com.zoftino.web.kotlin;

public class Deals {
	public static String getDeals() {
		return "sale on shoes, get $40 off on $200 and above";
	}
}

After adding kotlin and java code, build war. To run maven build, right click project, then click Run As and Run Configuration menu items, then double click Maven Build, enter run configuration name, select project base directory by clicking workspace, enter package as maven goal, then click apply and run. It will create war file in target directory after successful build and following is the output from the maven build.


[INFO] --- maven-war-plugin:2.2:war (default-war) @ coupon ---
[INFO] Packaging webapp
[INFO] Assembling webapp [coupon] in [C:\Project\kotlin\eclipse\coupon\target\coupon]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\Project\kotlin\eclipse\coupon\src\main\webapp]
[INFO] Webapp assembled in [313 msecs]
[INFO] Building war: C:\Project\kotlin\eclipse\coupon\target\coupon.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.517 s
[INFO] Finished at: 2018-07-20T20:46:15+05:30
[INFO] ------------------------------------------------------------------------

Here is the kotlin web application project structure after the build.

project structure of kotlin web application in eclipse using maven and deploy it to tomcat

Now, you can deploy the war to Tomcat. Refresh the target direct, right click kotlin war file, click export and file system menu items, then enter path to webapps directory of tomcat and click finish.

Then click start tomcat icon to deploy the kotlin war, make sure that tomcat version and tomcat home are set in preferences > tomcat. You can find details about using tomcat in eclipse in running web application in tomcat from eclipse.

access kotlin web application tomcat