• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
  • Himai Minh
  • Carey Brown
  • salvin francis

How to run multiple webapps in one tomcat container

Ranch Hand
Posts: 638
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am used to running one war file  in tomcat from ROOT,

But now I need to run multiple as well as root so I have


Now http://wwwtest.reports.com/dataservice points at root

But I also want to be able to set up

http://wwwtest.reports.com1/dataservice for app1

Now under


I have
export JAVA_OPTS=


Is it possible to set up multiple hostnames to map to each app under the webapp
Saloon Keeper
Posts: 22784
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do all sorts of things.

First and foremost you need to understand how Tomcat handles WARs and presents them as webapps.

A WAR, of course, is a JEE-standard Web ARchive file and can be deployed to Tomcat either in its native form (ZIP) or in "exploded" form (unzipped). By default, Tomcat will explode a WAR file for its own internal convenience.

Deploying a webapp consists of taking a WAR and associating it with a context path. The context path is the part of the URL that comes after the server information (name and optionally port) and before the WAR-relative resource paths. For most webapps, the default is that Tomcat takes the name of the exploded WAR. So if I unzip mywebapp.war into TOMCAT_HOME/webapps, then I can access it via http://localhost:8080/mywebapp.

ROOT is a special case, since "/" is not a valid directory name, which is why Tomcat maps http://localhost:8080/ (no context path) to TOMCAT_HOME/webapps/ROOT.

There are other ways to deploy WARs. In fact, you don't actually have to put your WAR in TOMCAT_HOME/webapps if you configure a Tomcat Context, but that's a more advanced feature.

So, as you can see, you can deploy many webapps very easily in a single Tomcat server. Each one lives under a different URL context path.

But what if instead of using a different context path, you want a different host path? Say instead of http://localhost:8080/mywebapp you want the URL http://mywebapp.coderanch.com:8080/.

There are two ways to do this. One is to set up virtual hosts in Tomcat and map DNS for the virtual hostname to point to the Tomcat server. This isn't as simple as it sounds.

So there's also another way, which is generally preferred and that's to front Tomcat with a reverse proxy server such as Apache, Nginx, or IIS.

The advantages of setting up this extra server are many:

1. You can host both JEE and non-JEE (php, for example) webapps at the same host.
2. You can avoid having to explicitly override the port number in the URL (http defaults to 80, but Tomcat can only listen on port 80 if it's running insecure).
3. You can rewrite URLs to be virtually anything you want with relatively little effort.
4. It's a lot easier to set up SSL for something like Apache than it is to set up SSL for Tomcat.

So by using a reverse proxy, I can take an incoming request targeting https://www.mywebapp.com and route it to http://localhost:8009/mywebapp/. Note that the proxy-to-Tomcat pipeline connects to Tomcat's port 8009, not 8080.
Tony Evans
Ranch Hand
Posts: 638
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yiou ever watch scrooge the musical, where they all sing that song Thank You very much, well that is what I am singing to you now.

I am so used to using ROOT so used to so its http://localhost:8080/ all I needed to do was http://localhost:8080/myApp.

Your explanation was clear and informative.
    Bookmark Topic Watch Topic
  • New Topic