Apache Tomcat Tutorial: Deploy Java Web Applications

Apache Tomcat is an open-source web server and servlet container used to deploy and run Java web applications.

In simple English, developers build a Java application, package it as a WAR file, and deploy that WAR file to Tomcat. Tomcat receives HTTP requests, runs the Java application, and returns responses to users.

What Is Apache Tomcat?

Tomcat provides the runtime environment required for technologies such as:

  • Jakarta Servlets
  • Jakarta Server Pages
  • WebSocket
  • Expression Language

Tomcat is commonly used for traditional Java and Spring applications that are packaged as WAR files.

Tomcat vs. Apache HTTP Server

Tomcat and Apache HTTP Server are different products.

  • Apache HTTP Server: Primarily serves static content and works as a web server or reverse proxy.
  • Apache Tomcat: Executes Java Servlets and Java-based web applications.
  • Common production design: Apache HTTP Server or Nginx handles public HTTPS traffic and forwards application requests to Tomcat.

Commands such as yum install httpd install Apache HTTP Server, not Tomcat. Therefore, the HTTPD commands in the original web-server notes should not be used as Tomcat installation commands.

How Tomcat Works

A basic request follows this process:

  1. A user sends an HTTP request.
  2. Tomcat receives it through an HTTP connector, commonly on port 8080.
  3. Tomcat identifies the correct application using its context path.
  4. The servlet container runs the Java application.
  5. Tomcat sends the HTTP response back to the user.

For example, deploying shopping.war normally creates this application URL:

http://server-ip:8080/shopping

The WAR filename normally becomes the application’s context path.

Tomcat Prerequisites

Before installing Tomcat, verify:

  • A supported Java version is installed
  • JAVA_HOME is configured when required
  • The Tomcat port is available
  • The server has sufficient CPU, memory, and disk space
  • Firewall or cloud security-group rules allow required traffic

Always match Java with the Tomcat version. For example:

  • Tomcat 11 requires Java 17 or later.
  • Tomcat 10.1 requires Java 11 or later.

Do not assume that every current Tomcat version runs on Java 8.

Check Java with:

java -version
echo $JAVA_HOME

Important Tomcat Directories

  • bin/ — Startup and shutdown scripts
  • conf/ — Tomcat configuration files
  • webapps/ — Deployed applications and WAR files
  • logs/ — Tomcat and application logs
  • lib/ — Shared Java libraries
  • temp/ — Temporary files
  • work/ — Compiled JSP and runtime files

Two important variables are:

  • CATALINA_HOME — Tomcat installation directory
  • CATALINA_BASE — Runtime configuration, logs, and deployed applications for a Tomcat instance

They may point to the same directory in a simple installation. Separate values are useful when running multiple Tomcat instances.

Important Configuration Files

server.xml

Used to configure:

  • HTTP connectors
  • Port numbers
  • Hosts
  • Engines
  • Services

The default non-TLS HTTP connector normally uses port 8080. Change the port when another application, such as Jenkins, is already using it.

tomcat-users.xml

Used in basic environments to configure Tomcat users and roles.

Common roles include:

  • manager-gui — Manager web interface
  • manager-script — Automated deployment interface
  • admin-gui — Host Manager interface

Use strong passwords and restrict Manager access to trusted IP addresses.

context.xml

Used for application-specific settings such as resources, sessions, and access restrictions.

web.xml

Defines default servlet and web-application behavior. Individual applications can also contain their own WEB-INF/web.xml.

How to Deploy a WAR File

Assume Maven creates this file:

target/myapp.war

Basic deployment

mvn clean package
cp target/myapp.war $CATALINA_BASE/webapps/

With automatic deployment enabled, Tomcat detects and deploys the WAR file. The application can normally be accessed at:

http://server-ip:8080/myapp

Tomcat also supports deployment through its Manager application, Ant tasks, Maven integrations, and CI/CD automation.

Useful Tomcat Administration Commands

The exact service name depends on the installation.

sudo systemctl status tomcat
sudo systemctl start tomcat
sudo systemctl stop tomcat
sudo systemctl restart tomcat
sudo systemctl enable tomcat

Verify port 8080:

sudo ss -lntp | grep 8080

Test Tomcat locally:

curl -I http://localhost:8080

Check service logs:

sudo journalctl -u tomcat -n 100 --no-pager

Tomcat uses JULI for internal logging, while deployed applications may use their own logging frameworks.

Common Tomcat Errors and Solutions

Tomcat is not accessible remotely

Check:

  • Tomcat service status
  • Connector port
  • Linux firewall
  • AWS security group or Azure NSG
  • Network route
  • Application context path
  • Whether Tomcat is listening on the expected interface

A Tomcat connector listens on configured IP addresses and normally listens on all configured addresses by default. Do not add 0.0.0.0 blindly without reviewing security requirements.

Port 8080 is already in use

Find the process:

sudo ss -lntp | grep 8080

Then stop the conflicting service or change Tomcat’s connector port in server.xml.

This commonly happens when Jenkins and Tomcat are configured on the same server.

WAR file is not deploying

Check:

  • The WAR file exists in webapps/
  • Tomcat has permission to read it
  • The application uses a compatible Java version
  • Required JAR files are available
  • WEB-INF/web.xml is valid
  • Tomcat logs contain startup exceptions

Tomcat’s deployment documentation recommends checking logs for parsing errors, missing classes, listener failures, and invalid context paths.

Manager login is failing

Verify:

  • Username and password
  • Required role
  • tomcat-users.xml
  • IP restrictions
  • Tomcat restart after configuration changes

Do not expose the Manager application openly to the internet. It is frequently targeted when weak passwords or unrestricted remote access are used.

Wrong Java version

Check:

java -version
echo $JAVA_HOME

Install a Java version supported by your Tomcat release and restart the service.

Tomcat in a DevOps Pipeline

A typical deployment pipeline is:

Developer → GitHub → Jenkins → Maven → WAR File → Tomcat

The process works as follows:

  1. A developer pushes code to GitHub.
  2. Jenkins starts the pipeline.
  3. Maven compiles, tests, and packages the application.
  4. Jenkins deploys the WAR file to Tomcat.
  5. The pipeline verifies application health.
  6. Monitoring tools check logs and availability.

Ansible can automate:

  • Java and Tomcat installation
  • Dedicated service-user creation
  • Configuration-file deployment
  • Port configuration
  • WAR deployment
  • Service restart
  • Health validation

These activities match the installation, configuration, service management, and troubleshooting tasks documented in the original notes.

Important Tomcat Interview Questions

What is Tomcat?

Tomcat is an open-source web server and servlet container used to deploy and run Java web applications.

What is a WAR file?

A WAR file is a Web Application Archive containing the application’s Java classes, libraries, configuration, and web resources.

What is Tomcat’s default HTTP port?

The default non-TLS HTTP connector normally uses port 8080.

Where are applications deployed?

Applications are commonly deployed in:

$CATALINA_BASE/webapps/

How do you troubleshoot a failed deployment?

Check the Tomcat service, Java version, port, file permissions, WAR file, application dependencies, context path, and logs.

How did you automate Tomcat deployment?

A strong interview answer is:

I used Jenkins to pull code from GitHub, Maven to build the WAR file, and Ansible or Jenkins deployment steps to move the WAR to Tomcat. I restarted or redeployed the application, verified the service and port, reviewed logs, and performed an HTTP health check.

Conclusion

Apache Tomcat is mainly used to deploy and run Java web applications. For practical work and interviews, focus on:

  • WAR deployment
  • Port 8080
  • server.xml
  • tomcat-users.xml
  • Java compatibility
  • Service and log troubleshooting
  • Jenkins, Maven, and Ansible integration
  • Manager security

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top