A Ktor application can be run and deployed inside servlet containers that include Tomcat and Jetty. To deploy inside a servlet container, you need to generate a WAR archive and then deploy it to a server or a cloud service that supports WARs.
In this topic, we'll show you how to:
configure Ktor to use it in a servlet application;
apply Gretty and War plugins for running and packaging WAR applications;
run a Ktor servlet application;
generate and deploy a WAR archive.
Configure Ktor in a servlet application
Ktor allows you to create and start a server with the desired engine (such as Netty, Jetty, or Tomcat) right in the application. In this case, your application has control over engine settings, connection, and SSL options.
In contrast to the approach above, a servlet container should control the application lifecycle and connection settings. Ktor provides a special ServletApplicationEngine engine that delegates control over your application to a servlet container.
To use Ktor in a servlet application, you need to include the ktor-server-servlet-jakarta artifact in the build script:
To generate a WAR file with your application using the War plugin, execute the war task. For the jetty-war example, a command looks as follows:
The jetty-war.war is created in the build/libs directory. You can deploy the generated archive inside a servlet container by copying it to the jetty/webapps directory. For instance, a Dockerfile below shows how to run the created WAR inside a Jetty or Tomcat servlet container: