Apache Tomcat - A Reliable Java Application Server

Understanding Tomcat, Its Features, and How to Use It

When deploying Java web applications, having a stable and efficient application server is essential. Apache Tomcat, one of the most widely used Java application servers, provides a lightweight yet powerful environment for running Java Servlets, JSP (JavaServer Pages), and WebSocket applications.

Tomcat is open-source and maintained by the Apache Software Foundation. It is known for its simplicity, speed, and compatibility with Java Enterprise technologies. Unlike full Java EE (Jakarta EE) servers like WildFly or GlassFish, Tomcat focuses specifically on Servlet and JSP processing, making it ideal for smaller and more efficient Java applications.

In this article, we will explore what Tomcat is, how it works, its key features, and how to configure it for Java web applications.

What is Apache Tomcat?

Apache Tomcat is a Java Servlet container that implements Java Servlet, JSP, and WebSocket specifications. It allows developers to deploy Java web applications without requiring a full Java EE application server.

Key Features:

  • Servlet & JSP Support – Implements the Jakarta Servlet API and JSP API for Java web development.
  • Lightweight & Fast – Optimized for speed and resource efficiency.
  • Cross-Platform – Runs on Linux, Windows, macOS, and supports containerization with Docker.
  • Security & Authentication – Supports TLS/SSL, role-based authentication, and access control.
  • Reverse Proxy Integration – Works well with Nginx, Apache HTTP Server, and load balancers.
  • Scalability – Supports clustering, session replication, and distributed deployment.

Tomcat is commonly used in production environments to host Java web applications and is often paired with Apache HTTP Server or Nginx for improved performance.

Installing Apache Tomcat

1. Download and Install Tomcat

The latest version of Apache Tomcat can be downloaded from the official Tomcat website.

Installing Tomcat on Linux

wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.80/bin/apache-tomcat-9.0.80.tar.gz
tar -xvzf apache-tomcat-9.0.80.tar.gz
mv apache-tomcat-9.0.80 /opt/tomcat

Set Permissions:

cd /opt/tomcat
chmod +x bin/*.sh

Start Tomcat:

./bin/startup.sh

After starting Tomcat, it runs by default on port 8080. You can check its status by visiting:

http://localhost:8080

2. Running Tomcat as a Systemd Service

To run Tomcat as a service, create a systemd unit file:

nano /etc/systemd/system/tomcat.service

Add the following content:

[Unit]
Description=Apache Tomcat Web Application Server
After=network.target

[Service]
User=tomcat
Group=tomcat
WorkingDirectory=/opt/tomcat
ExecStart=/opt/tomcat/bin/catalina.sh run
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=always

[Install]
WantedBy=multi-user.target

Enable and start the service:

systemctl daemon-reload
systemctl enable tomcat
systemctl start tomcat

Deploying a Java Web Application

Tomcat serves WAR (Web Application Archive) files, which contain Java applications.

1. Deploying a WAR File Manually

Copy the WAR file to Tomcat’s webapps directory:

cp myapp.war /opt/tomcat/webapps/

Restart Tomcat:

systemctl restart tomcat

The application will be available at:

http://localhost:8080/myapp

2. Deploying via Tomcat Manager

Tomcat includes a web-based manager for deploying applications.

Enable the Tomcat Manager by editing conf/tomcat-users.xml:

<role rolename="manager-gui"/>
<user username="admin" password="secret" roles="manager-gui"/>

Restart Tomcat:

systemctl restart tomcat

Now, access the Tomcat Manager:

http://localhost:8080/manager

Upload a WAR file through the web interface.

Configuring Tomcat

1. Changing the Default Port

Edit conf/server.xml:

<Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" />

Change 8080 to another port (e.g., 9090).

Restart Tomcat:

systemctl restart tomcat

2. Setting Up SSL/TLS

To enable HTTPS, modify server.xml:

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
           SSLEnabled="true">
    <SSLHostConfig>
        <Certificate certificateFile="/etc/ssl/certs/tomcat.crt"
                     certificateKeyFile="/etc/ssl/private/tomcat.key"
                     type="RSA" />
    </SSLHostConfig>
</Connector>

Restart Tomcat:

systemctl restart tomcat

Integrating Tomcat with Nginx

For better performance, Tomcat is often used behind Nginx as a reverse proxy.

1. Install Nginx:

apt install nginx -y

2. Configure Nginx Reverse Proxy:

Edit the Nginx configuration file (/etc/nginx/sites-available/tomcat):

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Enable the configuration and restart Nginx:

ln -s /etc/nginx/sites-available/tomcat /etc/nginx/sites-enabled/
nginx -t
systemctl restart nginx

Now, Tomcat applications will be accessible via http://example.com.

Performance Tuning

To improve performance and scalability:

  1. Increase Memory Allocation (in setenv.sh):

    export CATALINA_OPTS="-Xms512M -Xmx2048M -server -XX:+UseG1GC"
    
  2. Enable Connection Pooling: Edit conf/server.xml and increase max threads:

    <Connector port="8080" protocol="HTTP/1.1"
               maxThreads="200"
               connectionTimeout="20000"
               redirectPort="8443" />
    
  3. Enable Gzip Compression: Add to conf/web.xml:

    <filter>
        <filter-name>CompressionFilter</filter-name>
        <filter-class>org.apache.catalina.filters.GzipFilter</filter-class>
    </filter>
    

Conclusion

Apache Tomcat is a lightweight, reliable, and widely used application server for Java web applications. It is easy to set up, integrates well with Nginx and Apache, and provides excellent performance for servlet-based applications.

By following this guide, you can install, configure, and deploy Java applications using Tomcat, ensuring a stable and secure production environment.


See also