DevOps

How to Run Tomcat on Two Separate Ports

This article explains how to run an Apache Tomcat server on two different ports using multiple methods, including server configuration, embedded Tomcat, and network-level port forwarding, providing practical solutions for various deployment scenarios.

1. What is Tomcat?

Apache Tomcat is an open-source implementation of the Jakarta Servlet, Jakarta JSP, and WebSocket specifications. It acts as a lightweight application server that allows you to deploy and run Java web applications packaged as WAR files. Key components of Tomcat include:

  • Connector: Listens for incoming requests on a specific port and protocol (HTTP, HTTPS, AJP)
  • Engine: Processes requests routed from connectors
  • Host: Represents a virtual host (domain)
  • Context: Represents a web application

To run Tomcat on multiple ports, we mainly work with <Connector> configurations.

2. Add Multiple <Connector> in server.xml

The most common and straightforward way to run Tomcat on two different ports is by configuring multiple connectors in the server.xml file.

<Service name="Catalina">

  <!-- Default HTTP Connector -->
  <Connector
      port="8080"
      protocol="org.apache.coyote.http11.Http11NioProtocol"
      connectionTimeout="20000"
      redirectPort="8443" />

  <!-- Additional HTTP Connector -->
  <Connector
      port="9090"
      protocol="org.apache.coyote.http11.Http11NioProtocol"
      connectionTimeout="20000"
      redirectPort="8443" />

  <Engine name="Catalina" defaultHost="localhost">
      <Host name="localhost" appBase="webapps"
            unpackWARs="true" autoDeploy="true" />
  </Engine>

</Service>

Make sure to restart Tomcat after modifying the server.xml file to apply the configuration changes.

2.1 Code Explanation

The <Service name="Catalina"> element defines a logical grouping that connects one or more network listeners (connectors) to a single request-processing engine in Tomcat. In this configuration, two HTTP <Connector> elements are defined: the first listens on port 8080 and the second on port 9090, both using the non-blocking NIO HTTP protocol (Http11NioProtocol) to efficiently handle concurrent requests. The connectionTimeout attribute specifies the maximum time (in milliseconds) Tomcat will wait for a client request before timing out, while redirectPort="8443" indicates the HTTPS port to which requests will be redirected when a secure connection is required. Both connectors forward incoming traffic to the same <Engine> named Catalina, which processes requests for the default virtual host localhost. The <Host> configuration maps this virtual host to the webapps directory, enables automatic deployment of applications, and allows WAR files to be unpacked, ensuring that the same applications are accessible simultaneously through both ports 8080 and 9090.

2.2 Code Output

http://localhost:8080/myapp
http://localhost:9090/myapp

The output shows that the same web application (myapp) deployed on the Tomcat server is accessible through two different HTTP endpoints, http://localhost:8080/myapp and http://localhost:9090/myapp, confirming that Tomcat is successfully listening on both ports simultaneously. This happens because multiple connectors are configured to route incoming requests from different ports to the same engine and host, allowing users or clients to reach the identical application functionality regardless of which port they use, without requiring any duplication of deployment or additional application-level configuration.

3. Add Multiple Connectors With Embedded Tomcat

When using Embedded Tomcat (commonly in Spring Boot or standalone Java applications), connectors can be added programmatically.

// EmbeddedTomcatExample.java
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;

public class EmbeddedTomcatExample {

public static void main(String[] args) throws Exception {
    Tomcat tomcat = new Tomcat();
    tomcat.setPort(8080);

    Connector secondConnector = new Connector();
    secondConnector.setPort(9090);

    tomcat.getService().addConnector(secondConnector);
    tomcat.getConnector(); // triggers default connector

    tomcat.addWebapp("", "/path/to/webapp");
    tomcat.start();
    tomcat.getServer().await();
}

3.1 Code Explanation

This code demonstrates how to run an embedded Apache Tomcat server on two different ports using a standalone Java application. A Tomcat instance is first created and configured to listen on port 8080, which becomes the default HTTP connector. A second Connector object is then instantiated and assigned port 9090, allowing Tomcat to accept requests on an additional port. The second connector is registered with the Tomcat service using tomcat.getService().addConnector(), while tomcat.getConnector() ensures that the default connector is initialized. The addWebapp() method deploys a web application from the specified file system path to the root context, and finally tomcat.start() launches the server while tomcat.getServer().await() keeps the JVM running, enabling the same embedded application to be accessed concurrently via both ports 8080 and 9090.

3.2 Code Output

Tomcat started on port(s): 8080, 9090
Application available at:
http://localhost:8080
http://localhost:9090

This output confirms that the embedded Tomcat server has successfully started with two active HTTP connectors listening on ports 8080 and 9090. The log message indicates that both ports are bound and ready to accept incoming requests, and the listed URLs show that the same deployed application is accessible through either endpoint. This verifies that the embedded Tomcat configuration is correctly routing traffic from multiple ports to the same application instance without requiring duplicate deployments.

4. Use Network Level Port Forwarding

Another approach is to expose multiple ports externally while Tomcat listens on a single internal port. This is handled at the OS or network layer.

sudo iptables -t nat -A PREROUTING -p tcp --dport 9090 -j REDIRECT --to-port 8080

This approach is beneficial in containerized or restricted environments where changing the Tomcat configuration is not permitted.

4.1 Code Explanation

This command configures network-level port forwarding on a Linux system using iptables, where sudo grants administrative privileges, the -t nat option selects the Network Address Translation table, and the PREROUTING chain ensures that incoming packets are processed before routing decisions are made. The rule matches TCP traffic arriving on port 9090 (--dport 9090) and uses the REDIRECT target to transparently forward those requests to port 8080 on the same machine, allowing clients to access a Tomcat application via port 9090 even though the server itself is only listening on port 8080.

4.2 Code Output

http://localhost:8080
http://localhost:9090  (forwarded internally to 8080)

This output illustrates that the web application is accessible both directly through http://localhost:8080 and indirectly through http://localhost:9090, where requests on port 9090 are transparently forwarded at the network level to port 8080. As a result, users can use either port to reach the same Tomcat-hosted service, even though Tomcat itself is only configured to listen on port 8080.

5. Conclusion

Running Tomcat on two different ports is a common and practical requirement, and depending on your deployment model, you can choose from multiple approaches: configuring multiple connectors in server.xml, which is best suited for traditional Tomcat installations; using embedded Tomcat connectors, ideal for Spring Boot or standalone Java applications; or implementing network-level port forwarding, which is useful when configuration changes are restricted. Each method offers its own advantages, and understanding these options enables you to design flexible, scalable, and maintainable Tomcat-based systems.

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button