Code examples: jetty-war, tomcat-war, tomcat-war-ssl
Learn how to run and deploy a Ktor application inside a servlet container using a WAR archive.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.
Ktor supports Jetty up to the 9.4.x version and Tomcat up to 9.0.x.
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.
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.
Note that connection and SSL settings are not in effect when a Ktor application is deployed inside a servlet container. The tomcat-war-ssl sample demonstrates how to configure SSL in Tomcat.
To use Ktor in a servlet application, you need to include the ktor-server-servlet
artifact in the build script:
Note that you don't need the separate Jetty or Tomcat artifacts when a Ktor application is deployed inside a servlet container.
To register a Ktor servlet in your application, open the WEB-INF/web.xml
file and assign ServletApplicationEngine to the servlet-class
attribute:
{src="snippets/jetty-war/src/main/webapp/WEB-INF/web.xml" include-lines="7-16"}
Then, configure the URL pattern for this servlet:
{src="snippets/jetty-war/src/main/webapp/WEB-INF/web.xml" include-lines="18-21"}
The Gretty plugin allows you to run a servlet application on Jetty and Tomcat. To install this plugin, open the build.gradle.kts
file and add the following code to the plugins
block:
{src="snippets/jetty-war/build.gradle.kts" include-lines="5,8,10"}
Then, you can configure it in a gretty
block as follows:
{src="snippets/jetty-war/build.gradle.kts" include-lines="12-15"}
{src="snippets/tomcat-war/build.gradle.kts" include-lines="12-16"}
Note that if you want to use Tomcat, you need to specify servletContainer
explicitly.
Finally, configure the run
task:
{src="snippets/jetty-war/build.gradle.kts" include-lines="29-33"}
The War plugin allows you to generate WAR archives. You can install it by adding the following line to the plugins
block in your build.gradle.kts
file:
{src="snippets/jetty-war/build.gradle.kts" include-lines="5,9-10"}
You can run a servlet application with the configured Gretty plugin by using the run
task. For example, the following command runs the jetty-war example:
./gradlew :jetty-war:run
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:
./gradlew :jetty-war:war
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:
{src="snippets/jetty-war/Dockerfile"}
{src="snippets/tomcat-war/Dockerfile"}
You can find the complete examples here: jetty-war and tomcat-war.