How to deploy a java web application on tomcat server

Last updated : Jul 30, 2023 12:00 AM

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.

Creating a Java web application is not in the scope of this article. If you do not have a web application war file, follow Dynamic Web Project with Maven and Eclipse to create one.

Tomcat needs Java installed and the path set to run. This article explains

  • How to install Java
  • How to configure Java
  • How to install Tomcat
  • How to deploy a dynamic web application in Tomcat

In Windows, Mac, and Linus operating systems. First, let's start with Windows.

1. How to deploy a Java web app on Tomcat on Windows #

To deploy the Tomcat server on Windows, we need to have Java installed and the JAVA_HOME variable set.

1.1 How to install Java on Windows #

Before trying to install Java, ensure you do not have Java installed in your system. Open a command prompt and type the below command.

java -version

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

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

If that is the case, you can skip to the next step, How to set JAVA_HOME in Windows.

Otherwise, your command line output will look something like this:

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

Now we know at least the path is not set. The next step is to ensure that Java is installed. Without the path set, there are several ways to check if Java is installed.

  • Go to control panel -> Programmes -> Programmes and features and type Java in the search box. If Java is installed, a Java icon will appear.
  • Click Start Menu and type Java, and look for the Java icon.

If Java is installed, you can skip to the next step, How to set JAVA_HOME in Windows.

Download and install Java on windows#

Download Java from the Oracle site or the free version from the OpenJDK. Follow the installation steps. Make sure to note down the full installation path. My installation path is c:\learnbestcoding\jdk18

How to set JAVA_HOME in Windows #

  • Click Start and type environment variables
  • Select Edit the system environment variables
  • Click Environment variables
  • Click new under System variables
  • Enter JAVA_HOME for the variable name and the JDK installation path (c:\learnbestcoding\jdk18) for the variable value.
  • Click ok. Restart the command window and type echo %JAVA_HOME%

How to install Tomcat on Windows #

  • Download Tomcat
  • Extract the archive to your local hard drive. My Tomcat is in c:\learnbestcoding\tomcat. This location is called CATALINA_HOME.
  • Create CATALINA_HOME environment variable. Variable value is c:\learnbestcoding\tomcat.

Now we have completed all the steps to run the Tomcat server on the Windows system.

How to deploy Tomcat on Windows #

  • Place the Java war file in the CATALINA_HOME\webapps folder. That is c:\learnbestcoding\tomcat\webapps
  • Open a command prompt and navigate to the CATALINA_HOME\bin folder
  • Run the server by typing startup.bat
  • Open a browser and navigate to http://localhost:8080/webapp. Replace webapp with the actual context name of the web application.

How to deploy a Java web app on Tomcat on Mac #

1.2 How to install Java on Mac #

To ensure if you already have Java installed in your system,

  • Open a terminal window and type javac -version. If Java is installed, it will output the installed version.
  • Open a terminal window and type which java. If Java is installed, it will output the installation location.

If Java is installed, you can skip to the next step, How to install Tomcat on Mac.

Install Java with Homebrew #

  • Install Homebrew
  • Install Java with brew install openjdk@18
  • Confirm the installation with brew info java
  • For the system Java wrappers to find this JDK, symlink it with sudo ln -sfn /opt/homebrew/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
  • Set the path with export PATH="/opt/homebrew/opt/openjdk/bin:$PATH"
  • Create JAVA_HOME with echo export "JAVA_HOME=/opt/homebrew/opt/openjdk" >> ~/.zshrc or echo export or "JAVA_HOME=/opt/homebrew/opt/openjdk" >> ~/.bash_profile

How to install Tomcat on Mac #

  • Download Tomcat from https://tomcat.apache.org
  • Extract the archive into your local hard drive. My Tomcat location is /Users/learnbestcoding/SERVERS/apache-tomcat-10.0.14. That is also your CATALINA_HOME.
  • Set CATALINA_HOME with export CATALINA_HOME=/Users/learnbestcoding/SERVERS/apache-tomcat-10.0.14

Now the Tomcat server is ready to run on the Mac.

How to deploy Tomcat on Mac #

  • Place the Java war file in the CATALINA_HOME\webapps folder. That is /Users/learnbestcoding/SERVERS/apache-tomcat-10.0.14/webapps
  • Open a command prompt and navigate to the CATALINA_HOME\bin folder
  • Run the server by typing startup.sh
  • Open a browser and navigate to http://localhost:8080/webapp. Replace webapp with the actual context name of the web application.
  • To stop the server run shutdown.sh

How to deploy a Java web app on Tomcat on Linux #

1.3 How to install Java on Linux #

To ensure you don't have Java installed in your Linux system,

  • Open a terminal window and type javac -version. If Java is installed, it will output the installed version. Otherwise, the output will be java: command not found.
  • Open a terminal window and type which java. If Java is installed, it will output the installation location.

If Java is installed, you can skip to the next step, How to install Tomcat on Linux. But don't forget to set JAVA_HOME.

Install Java on Linux with apt or yum #

  • Run sudo apt update or sudo yum update (for aws)
  • Install Java with sudo apt-get install openjdk-18-jdk -y (check FAQ for AWS Linux)
  • Confirm the installation with the java --version
  • Find the installation location with which java
  • Set JAVA_HOME with export JAVA_HOME=/usr/bin/java

How to install Tomcat on Linux #

  • Create a directory to hold Tomcat server folder. I use /home/learnbestcoding/tomcat as my Tomcat location. mkdir /home/learnbestcoding/tomcat
  • Download and extract Apache Tomcat server into /home/learnbestcoding/tomcat directory.
  • That Tomcat installation directory is also called CATALINA_HOME. Set CATALINA_HOME variable with export CATALINA_HOME=/home/learnbestcoding/tomcat

How to deploy Tomcat on Linux #

  • Place the Java war file in the CATALINA_HOME\webapps folder. That is /home/learnbestcoding/tomcat/webapps
  • Open a command prompt and navigate to the CATALINA_HOME/bin folder.
  • Run the server by typing ./startup.sh
  • Open a browser and navigate to http://localhost:8080/webapp. Replace webapp with the actual context name of the web application.
  • To stop the server run ./shutdown.sh

FAQ #

Q. sudo: apt: command not found

A. That usually happens in AWS EC2 instances. For AWS instances, use sudo amazon-linux-extras install java-openjdk11 -y with your preferred JDK version.

Q. mkdir: cannot create directory 'tomcat': Permission denied

A. This happens when the base folder is not in your home directory. Use sudo mkdir tomcat

Q. Cannot find /home/ec2-user/tomcat/bin/setclasspath.sh This file is needed to run this program

A. This can be due to the CATALINA_HOME variable pointing to the wrong folder. Make sure the CATALINA_HOME points to your actual tomcat server folder. The unset CATALINA_HOME will solve this error, but it will also remove the CATALINA_HOME variable. The correct solution is to fix the CATALINA_HOME path.

Lance

By: Lance

Hi, I'm Lance Raney, a dedicated Fullstack Developer based in Oklahoma with over 15 years of exp

Read more...