1. OverviewApache Tomcat is a web server and servlet container used to deploy and serve Java web applications. By default, Tomcat listens on a single HTTP port, such as 8080, to handle all incoming requests. In many cases, however, we want to run the same Tomcat instance on multiple ports.This setup enables Blue-Green or Canary deployments, smooth port migrations, and separate admin or monitoring endpoints for better control.In this quick tutorial, we’ll see how to run a single Tomcat instance on multiple ports using just one JVM process and one deployment.2. Add Multiple in server.xmlLet’s look at the Tomcat’s server.xml file, where we define multiple connectors to handle incoming requests. Both Connectors share the same application and content: Now, we’ll create a small app that we can access through both ports. We’ll use the Catalina API to load the custom server.xml file above and replace the placeholder STATIC_DIR_PLACEHOLDER with the absolute path to our static directory.public Catalina startServer() throws Exception { URL staticUrl = getClass().getClassLoader().getResource("static"); if (staticUrl == null) { throw new IllegalStateException("Static directory not found in classpath"); } Path staticDir = Paths.get(staticUrl.toURI()); Path baseDir = Paths.get("target/tomcat-base").toAbsolutePath(); Files.createDirectories(baseDir); String config; try (InputStream serverXmlStream = getClass().getClassLoader().getResourceAsStream("server.xml")) { if (serverXmlStream == null) { throw new IllegalStateException("server.xml not found in classpath"); } config = new String(serverXmlStream.readAllBytes()).replace( "STATIC_DIR_PLACEHOLDER", staticDir.toString()); } Path configFile = baseDir.resolve("server.xml"); Files.writeString(configFile, config); System.setProperty("catalina.base", baseDir.toString()); System.setProperty("catalina.home", baseDir.toString()); Catalina catalina = new Catalina(); catalina.load(new String[]{"-config", configFile.toString()}); catalina.start(); System.out.println("\nTomcat started with multiple connectors!"); System.out.println("http://localhost:8081"); System.out.println("http://localhost:7081"); return catalina;}We set the required Catalina system properties, create a Catalina instance, and load it with our custom configuration file. As we can see above, the configuration file includes two HTTP connectors on different ports.We can now access the same application by connecting to either http://localhost:8081 or http://localhost:7081:3. Add Multiple Connectors With Embedded TomcatLet’s look at how to add multiple connectors directly in the code without needing a server.xml file. First, let’s create a Tomcat instance and set its primary port to 7080. Then, we’ll create a new Connector object, set its port to 8080, and add it to Tomcat’s service using addConnector() to enable the second port:public static void main(String[] args) throws Exception { Tomcat tomcat = new Tomcat(); tomcat.setBaseDir(new File("tomcat-temp").getAbsolutePath()); tomcat.setPort(7080); tomcat.getConnector(); Connector secondConnector = new Connector(); secondConnector.setPort(8080); tomcat.getService().addConnector(secondConnector); Context ctx = tomcat.addContext("", new File(".").getAbsolutePath()); Tomcat.addServlet(ctx, "portServlet", new HttpServlet() { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { int port = req.getLocalPort(); resp.setContentType("text/plain"); resp.getWriter().write("Port: " + port + "\n"); } }); ctx.addServletMappingDecoded("/", "portServlet"); tomcat.start(); System.out.println("Tomcat running on ports 8080 and 7080"); tomcat.getServer().await();}Instead of serving a static file, we’re using an inline servlet that displays which port received the request. We’ve added it to a context and mapped it to the root path /. Next, we’ll access http://localhost:7080 and http://localhost:8080, and the servlet will display the port number that received the request. When we visit these URLs, we’ll see:Port: 7080Port: 80804. Use Network Level Port ForwardingInstead of configuring Tomcat to listen on multiple ports, we can set up operating system-level port forwarding to route traffic from different ports to a single Tomcat container. For example, let’s consider a scenario where Tomcat runs locally on port 8080, but we want to access it through port 7080 as well. We can use a Linux command like this:sudo iptables -t nat -A PREROUTING -p tcp --dport 7080 -j REDIRECT --to-port 8080We’ve configured Linux firewall rules to redirect traffic from one port to another at the kernel level, before the application even sees the request. When TCP traffic arrives on port 7080, the kernel intercepts it and redirects it to port 8080. As a result, the application listens on port 8080 and receives the traffic, while the client thinks it’s connected to port 7080.We can try this example quickly by running the following Docker command:docker run -it --rm --cap-add=NET_ADMIN -p 8080:80 -p 7080:7080 ubuntu:22.04 sh -c \ "apt-get update -qq && apt-get install -y -qq iptables nginx && \ service nginx start && \ iptables -t nat -A PREROUTING -p tcp --dport 7080 -j REDIRECT --to-port 80 && \ echo 'Nginx started on port 80' && \ echo 'iptables rule: 7080 → 80' && \ iptables -t nat -L -n -v && \ tail -f /dev/null"Meanwhile, in another terminal, we can run curl http://localhost:7080 to see the following output:Welcome to nginx! body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }Welcome to nginx!If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.For online documentation and support please refer tonginx.org.Commercial support is available atnginx.com.Thank you for using nginx.5. ConclusionIn this tutorial, we discussed different approaches to running a Tomcat server on other ports. The multiple Connectors need only one file change. The configuration is stored in XML, making it easy to track. Moreover, since it’s built into Tomcat, restarting it automatically restores both connectors.The network-level port forwarding approach is clean, simple, and requires no Tomcat changes. It’s ideal for containerized or cloud environments. As always, the code is available over on GitHub.The post Running Tomcat Server on Two Different Ports first appeared on Baeldung.