Kotlin is a great fit for developing server-side applications.
You can write concise and expressive code while maintaining full compatibility with Java. - https://kotlinlang.org/
In software industry server side application development play very important role to solve complex problem and achieve business requirement in various Industry like Banking, Transport etc.
Java servlet provides server side development with a simple, consistent mechanism for extending the functionality of a Web server and for accessing existing business systems. Servlets are server-side Java EE components that generate responses to requests from clients.
Typical implementations of these APIs on Application servers or servlet containers use a standard servlet for handling all interactions with the HTTP requests and responses that delegate to the web service methods for the actual business logic.
Kotlin is write selection for every where , Kotlin good for Mobile cross-platform, Data Science, Server side based application. support or Kotlin is great language for server side application development.
In this tutorial we will walks through the process of creating a simple controller using HttpServlet to display Hello Kotlin School.
Java EE Http servlets can be used from Kotlin much like any other Java library or framework. We'll see how to make a simple controller that returns "Hello, Kotlin School.!".
with kotlin we are use gradle with IntelliJ Gradle project structure. The following two dependency required for HTTP Servlet API.
Creating Web Applications with Http Servlets
buildscript {
ext.kotlin_version = '1.3.72'
}
dependencies {
compile group: 'javax', name: 'javaee-api', version: '7.0'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
To generate war file required war plugin
apply plugin: 'war'
Welcome Controller
@WebServlet(name = "Welcome", value = ["/welcome"])
class WelcomeController : HttpServlet() {
override fun doGet(req: HttpServletRequest, res: HttpServletResponse) {
res.writer.write("Hello, Kotlin School!")
}
}
Using IntelliJ IDEA you can easily run and debug the application in any of the possible application servers defined such as Tomcat, Glassfish or WildFly.
Server side frameworks using Kotlin.
In computer programming, a software framework is an abstraction in which software providing generic functionality can be selectively changed by additional user-written code, thus providing application-specific software. It provides a standard way to build and deploy applications and is a universal, reusable software environment that provides particular functionality as part of a larger software platform to facilitate development of software applications, products and solutions. Software frameworks may include support programs, compilers, code libraries, tool sets, and application programming interfaces (APIs) that bring together all the different components to enable development of a project or system.
In JavaEE most popular framework are Sprint, JSF, Hibernate, Struts.
In Kotlin Ktor is a multiplatform toolkit built by JetBrains for creating server side applications. It makes use of coroutines for high scalability and offers an easy-to-use API.

Ktor Applications can be hosted in any servlet container with Servlet 3.0+ API support such as Tomcat, or standalone using Netty or Jetty. Support for other hosts can be added through the unified hosting API.
Hello Ktor!
- Set up a Ktor project
- Hello World
- Accessing Hello Ktor application
Using Gradle and IntelliJ IDE Ktor setup is easy or you can create Ktor project using Ktor Generator .
Now create project using Intellij IDE Plugin. here first step is install plugin in Intellij IDE.




fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
Here we will use Netty EngineMain to run application.
@kotlin.jvm.JvmOverloads
This is Kotlin Standard Library Annotation comes under Package kotlin.jvm.
Instructs the Kotlin compiler to generate overloads for this function that substitute default parameter values.
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
routing {
get("/") {
call.respondText("HELLO WORLD! ", contentType = ContentType.Text.Plain)
}
}
}
Here fun main is application entry point, routing is install routing feature and start configuring it and get define a route that will handle specific path
Ktor module is just user-defined function receiving the application class that is in charge of configuring the server pipeline, install features, registering routes handling request etc
anApplication
instance is main unit of Kotr
application. when request
coming either HHTP
or HTTPS
or event WEBSOCKET
it converted to an ApplicationCall
and goes through a pipeline which is owned by Application
module
is an extension method of the class Application
(where Application
is the receiver). Since it is defined as a top-level function, Kotlin creates a JVM class with a Kt
suffix (FileNameKt
), and adds the extension method as a static method with the receiver as its first parameter. In this case, the class name is ApplicationKt
in the com.kotlinschool
package, and the Java method signature would be static public void module(Application app)
.
Running the Hello World Application
Ktor application cab be self hosted either hosted on application server. now next part part is how to host ktor applications externally.
When you need to run Ktor based application is an independently , that time required an application.conf file to tell Ktor how to start application.
ktor {
deployment {
port = 8080
port = ${?PORT}
}
application {
modules = [ com.kotlinschool.ApplicationKt.module ]
}
}
In above code snippet is part of application.conf file it will auto create with default value using intellij.
modules = [ com.kotlinschool.ApplicationKt.module ]
Here com.kotlinschol is package name and ApplicationKt is main file.