Home > Tutorials > Java

How to deploy a java web application on tomcat server

Last updated : December 26, 2020

What is a dynamic web application?

A dynamic web application produces web pages in real-time. Based on the client request, the server generates a custom response and sent to the client. To differentiate, static web pages send the same response to the same resource, where dynamic web apps can return different responses for the same resource. (not considering request parameters for the moment.)

For example, when you access your email, you get access to only your emails, and the server produces the response exclusively for you. The article How to set up a Dynamic Web Module with Maven and Eclipse explains how to create a basic, bare minimum maven web application. To recap, we named that web application SpringWeb. Now let's deploy the SpringWeb web application on the tomcat web server, enabling visitors to access it via a browser.

What is the Tomcat server?

Apache Tomcat is an open-source Java servlet container. Tomcat implements core Java enterprise specs, such as Java Servlets, JavaServer Pages (JSP), and WebSockets APIs.

Tomcat was first released in 1998 as an Apache Software Foundation project. Tomcat web server directly deal with client requests and responses. When we use Tomcat to deploy our web application, Tomcat handles requests from clients and responses back to clients. Our web application deals with Tomcat.

How to set up the Java environment?

Tomcat is a platform-independent tool. It needs Java to operate. Besides, the path variable should point to Java installation. To ensure java is installed on your system, follow the below steps.

Windows system

Open a command window by typing cmd and pressing enter. On the command window, type

java -version

If Java is installed and the path is set, you will see an output similar to :

C:\Program Files>java -version
OpenJDK version "1.8.0_131"
OpenJDK Runtime Environment (.................)

If you don't have Java installed or the path is not set, your command line output will look something like :

'java -version' is not recognized as an internal or external command

Now we are certain that the path is not set. The next step is to ensure if Java is installed. This depends on your operating system and version of the operating system.

On Windows 10

Type Java on the search bar to see programs display Such as About Java or go to

control panel -> Programs and see if you can spot the Java icon

Mac OS

Navigate to System Preferences
Look for the Java icon

If you do not have Java installed, follow How to set up Java to set up your environment. Although we discuss alternatives to start and deploy Tomcat, without proper environment variables in place, it is highly recommended to set recommended environment variables to reduce confusion and to support tools out of the box.

How to set up the Tomcat server?

Now, let's set up the Tomcat server. When it comes to the Tomcat installation, you have two options.

1. Download and install executable msi and set up Tomcat as a service.
2. Download the zip file and install the Tomcat server manually.

We will follow the second approach since it allows us to use the same downloaded server embedded in Eclipse. The zip file contains all the batch files required by Eclipse.

Download the Apache Tomcat server from https://tomcat.apache.org. Make sure to download the zip format and extract the contents to your local drive. We will refer to this location as C:\tomcat.

Set up CATALINA_HOME

Now it is time to set the CATALINA_HOME environment variable. CATALINA_HOME is the folder where Apache Tomcat is installed. In our case, it is C:\tomcat. CATALINA_HOME is essential because it points to the location of files the Tomcat uses in the deployment process. For instance, log files are written to CATALINA_HOME/logs, and the configuration is read from CATALINA_HOME/conf.

To ensure if CATALINA_HOME is set, (which is unlikely) type below in the command prompt :

C:\>echo %CATALINA_HOME%

If the path for CATALINA_HOME is already set, the output would be the path to CATALINA_HOME. The output of %CATALINA_HOME% means the path is not set.

Lets set the path by typing :

set CATALINA_HOME=C:\tomcat

Or navigating to
System Properties -> Environment Variables -> New
and entering a new variable as variable name = CATALINA_HOME and variable value = C:\tomcat

Please make sure to replace the Tomcat installation path with your actual Tomcat server location. At this point, you will have to close your existing command prompt and re-open it in order to make changes into effect.

The deployment and starting the server.

Now we are all set to test run the server. Open another command prompt and navigate to CATALINA_HOME/bin. This translates into C:\tomcat\bin in this example, but yours may vary depending on the location of the Tomcat server.

Let's start the server by typing :

C:\tomcat\bin>startup.bat

This will open a new command window and display startup logs, and any errors if you haven't set up the environment variables properly. By default, the server will start in port 8080.

Go to a web browser and type http://localhost:8080. This will bring you to the Tomcat welcome page.

Deploying a dynamic web application war file

Now we have an up and running web server ready to deploy our own custom web application. First shut down the server we started in the above step.

C:\tomcat\bin>shutdown.bat

This command will stop the server and close any additional consoles it opened. Navigate to C:\tomcat\webapps folder. This is where we place our deployable web application. You will notice there are some folders already in this location. They are included by the Tomcat and used to manage your environment. They help to manage the deployment process, security, etc.

Copy and paste your web application war archive into the C:\tomcat\webapps folder. In case if you followed How to set up a Dynamic Web Module with Maven and Eclipse to create your web application, copy the SpringWeb.war file into webapps folder and start the server :

C:\tomcat\bin>startup.bat

Now your web application is deployed in your local environment and ready to serve clients. Open a web browser and type :

http://localhost:8080/SpringWeb

This will bring to the landing page of your application. Please be aware that SpringWeb is the application we created in another tutorial. Your application context name may vary depending on how you have named it.

How to start Tomcat without JAVA_HOME and CATALINA_HOME

If you have neither set up in your machine, you still can start the server by doing some changes to the configuration files. When you start Tomcat, the server itself should know where to find Java installation and where the Tomcat is located on the computer it runs. When we set JAVA_HOME and CATALINA_HOME at the environmental level, the server is capable of finding those parameters since they are shared amongst all applications run on the system. Without them being available to the server, you have to manually point to their locations where ever server is referring to these variables.

There are three major configuration files that use these two environmental variables.

C:\tomcat\bin>startup.bat
C:\tomcat\bin>shutdown.bat
C:\tomcat\bin>catalina.bat
C:\tomcat\bin>setclasspath.bat

You will have to find those occurrences of references to our two environmental variables and replace their paths with the absolute paths of Java and Tomcat installation locations. Since these settings may change from Tomcat version to version, it is not advisable to follow this approach.

Lance
By: Lance
Lance is a software engineer with over 15 years of experience in full-stack software development.
Read more...

Leave a comment

No Comments