To deploy an application in Tomcat 10, create a WAR file and place it in the /webapps
directory. Tomcat will auto-deploy the application when it starts.
Key takeaways:
Tomcat is an open-source server designed to run Java-based web applications, including servlets and JSP.
Applications are deployed using WAR files, which can be created using Maven.
Key directories in Tomcat include
bin
for scripts,conf
for configuration, andwebapps
for deployed applications.To start Tomcat, run commands in the
bin
directory based on your operating system (Windows, macOS, Linux).The
pom.xml
file is essential in Maven for defining project details, dependencies, and build configurations.Create a WAR file by running Maven in the project directory with the pom.xml file.
Deploy the WAR file by placing it in the
webapps
directory on the running Tomcat server.Access the deployed application via
http://localhost:8080
with the appropriate WAR file name.
Tomcat is an open-source server specifically designed to run Java-based web applications. These Java-based applications specifically have Java servlets and JavaServer Pages(JSP). We are going to use Maven to create a customized web application archive file (WAR) to deploy the web application on the containers. The WAR files pack the web application to make deploying it easier on the deployment containers.
There are two ways to deploy the application on Tomcat: manually and through its manager. In this Answer, we are focused on deploying the application manually. There are a series of steps that we need to follow to successfully deploy the Java Server Pages web application on the Tomcat server. The process goes from understanding the directory structure to deploying the web app on Tomcat.
First, we need to install Tomcat on our desktop. Once we have installed Tomcat, the next step is to understand its structure.
bin: Contains the scripts for starting and shutting down the server. We use catalina.sh
or catalina.bat
for starting, restarting, and stopping the server.
conf: Contains configuration files for Tomcat. The server.xml
file has the main configuration and tomcat-users.xml
has the roles for Tomcat Manager applications.
webapps: Contains the web applications and WAR files to be deployed on the Tomcat server.
Now that we understand the structure, let’s explore the steps to deploy the web application on the server.
For this purpose, navigate to the /bin
directory and start the Tomcat server using the command.
On Windows, run the following command:
catalina.bat run
On macOS and Linux, run the following command:
./catalina.sh run
We need a Java web application and a pom.xml
file to create a WAR file. We use Maven, a project management and build application tool, to create the WAR file of the Java web application. The Maven project, however, requires a Project Object Model pom.xml
file.
pom.xml
fileThe pom.xml
file contains build configuration, build plugins, project information details, and profiles for the web application.
To create a pom.xml
file, we define the following key elements:
Project information:
<groupId>
: Defines organization responsible for project.
<artifactId>
: Unique identifier for the project.
<version>
: Defines the version of project.
Dependencies:
<dependencies>
: Defines external dependencies to the project.
<dependency>
: Each dependency has its artifact ID and version.
Build configuration:
<plugins>
: Specifies all the build plugins for project.
<plugin>
: Each plugin has its artifact ID and version.
Repositories:
<respositories>
: Specify external repositories to search for repositories.
Reporting:
<reporting>
: Specify reporting plugin with their configuration.
Properties:
<properties>
: It helps define properties to use throughout the projecy model file.
To build the application into a WAR file. Run the following command in the directory containing the web application and the pom.xml
file.
mvn clean install
As a result, you will find the target
directory added to your project directory /sample-app
. You will find your WAR file in the directory /sample-app/target
. Now, the web application is ready for deployment.
We need to place the prepared web application WAR file in the /webapps
directory on Tomcat. Wait for the application to be deployed on the running Tomcat server. Our WAR file name is ROOT.war
, so we wait until we get the following command in the terminal.
[Date] [Time] INFO [Catalina-utility-1] org.apache.catalina.startup.HostConfig.deployWAR Deploymentof web application archive [/apache-tomcat-9.0.80/webapps/ROOT.war] has finished in [87] ms
Now, if you go to http://localhost:8080
, you will find your deployed web application. The port 8080
is the specified port for Tomcat. We have used ROOT.war
file to deploy our web application, so the URL has no directory. If, for example, the WAR file is web-app.war
, you will find your deployed web application at http://localhost:8080/web-app
.
Now that we have gone through the deployment process, see how the web application is deployed on the server. You can add value to the parameter using the GET
request; this can be done by adding the following URL.
http://localhost:8080/?name=manya&job=TCE
On our platform, you can use the following URL.
{{EDUCATIVE_LIVE_VM_URL}}/?name=manya&job=TCE
To use the above URL, please first "Run" the following widget, and once the app is deployed, please visit the app URL.
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Hello World DB</display-name> <welcome-file-list> <welcome-file>user.do</welcome-file> </welcome-file-list> </web-app>
This is how we can successfully deploy the Java Server Pages web application on the Tomcat server.
The process works in flow from building the application to building the environment for deploying the built version of the application. Tomcat must define the web application and the Maven plug-ins to successfully execute the web application on the server.
In conclusion, deploying Java-based web applications on a Tomcat server is a structured process involving the creation of a WAR file, configuring key project details through Maven, and understanding the Tomcat directory structure. By following the steps of starting the server, preparing the application, and placing the WAR file in the appropriate directory, developers can efficiently deploy and access their applications. Tomcat provides a reliable platform for running Java servlets and JSP, making it a preferred choice for hosting Java web applications.
Haven’t found what you were looking for? Contact Us
Free Resources