To deploy a Java web application to any Java Server Runtime, we normally need to package it into a .war file, then copy this file to a specified directory of the Java Server Runtime. Java Server Runtime when starting will automatically deploy the application for us. Apache Tomcat is a Java Server Runtime and it is no exception. In this tutorial, I will show you how to deploy Java web applications to Apache Tomcat!
First, I will create a new Spring MVC Maven project as an example:
You can run this example application with Maven using Eclipse Jetty as I mentioned in the tutorial Create Spring MVC project using Maven
To deploy this application with Apache Tomcat, you need to know which version of Apache Tomcat is suitable for the application by relying on the Java version and Jakarta EE version information that the application is using.
My example application is using Java 17 and Spring framework 6.x with Jakarta EE version 9, so the Apache Tomcat version you need to use is 10.x or higher. You can refer here to know how to install Apache Tomcat.
I have already installed Apache Tomcat:
Now, I will use the “mvn package” command to build the source code of the application into a .war file.
To deploy the example application, I will copy this springmvc-0.0.1-SNAPSHOT.war file into the webapps folder of the Apache Tomcat directory. You will see Apache Tomcat automatically deploying the application as follows:
As you can see, a new folder with the same name as the .war file has been created. The contents of this folder are extracted from the .war file!
We access the home page of Apache Tomcat using the address http://localhost:8080/, but for the application, we need to add the newly added directory name to be able to access the application. For example, I can access my application using the address http://localhost:8080/springmvc-0.0.1-SNAPSHOT/, my results are as follows: