How to host multiple sites within one servlet container?

The following howto uses Jease as an example, but it is applicable for every web-application which needs to generate pretty urls.

Jease tries really hard to make pretty urls simple. In order to achieve this goal, Jease relies on two concepts:

  • Using a smart servlet-filter which can handle content contained within Jease or in the filesystem transparently without the need to add any prefix, extension or parameter. If a ressource can be resolved via Jease, it will be served, otherwise the request will be delegated to be served by the default handler of the servlet container.
  • Jease is deployed as ROOT-application in your servlet container (e.g. Tomcat or Jetty), so no additional context-path is added in front of the path.

This works like a charm for one Jease-Instance. But what do you do if you want to run several independent Jease-instances from one physical server?

The simple approach would be to install and run several servlet containers on different ports and hide them via a proxy (like Apache)... but that's usually a waste of ressources.

A better approach is to deploy several instances within the same servlet container... but stop: we already deployed one Jease-instance as ROOT-application in our webapps-folder, so no second instance can take this special position.

But if we want to run independent sites for different domains, we can make use of a concept called "Virtual Hosting". A servlet contaienr is able to serve different applications depending on the requested domain.

Configure Tomcat to serve multiple sites

  • Create a folder called "sites" in Tomcat (so "sites" and "webapps" are contained within the same directory in the root of your Tomcat-installation).
  • Create a subfolder "yourdomain.org" in sites.
  • Copy a fresh (or already customized) ROOT.war from a Jease-Installation to "sites/yourdomain.org"
  • Now edit Tomcat/conf/server.xml and add a second entry for a new virtual host right below the definition for the default host (which is the first entry below):
...
<!-- This entry is already contained in server.xml -->

<Host name="localhost"  appBase="webapps"
 unpackWARs="true" autoDeploy="true"
  xmlValidation="false" xmlNamespaceAware="false" />

<!-- This entry adds a virtual host for yourdomain.org -->

<Host name="yourdomain.org"  appBase="sites/yourdomain.org"
  unpackWARs="true" autoDeploy="true"
  xmlValidation="false" xmlNamespaceAware="false" />
...
  • Now restart Tomcat.
  • From now on Tomcat serves all requests for http://yourdomain.org/ from the ROOT-application contained in "sites/yourdomain.org".

Configure Jetty to serve multiple sites

  • Create a new folder called "sites" in the root directory of your Jetty installtion.
  • Create a new folder jetty/sites/yourdomain.org/
  • Copy ROOT.war to jetty/sites/yourdomain.org/
  • Create a file with the following content to jetty/conf/yourdomain.xml:
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
 <Set name="contextPath">/</Set>
 <Set name="war">
  <SystemProperty name="jetty.home"/>/sites/yourdomain.org/ROOT.war
 </Set>
 <Set name="virtualHosts">
   <Array type="java.lang.String">
     <Item>www.yourdomain.org</Item>
   </Array>
 </Set>
</Configure>
  • Now restart Jetty.

Configure Apache as proxy for a servlet container

If you're running Apache in front of your servlet container, you'll need to configure Apache as proxy server for your servlet container as follows:

<Proxy http://www.yourdomain.org:8080/>
Order Allow,Deny
Allow from all
</Proxy>

<VirtualHost *:80>
ServerName yourdomain.org
ServerAlias www.yourdomain.org
ProxyPass / http://www.yourdomain.org:8080/
ProxyPassReverse / http://www.yourdomain.org:8080/
</VirtualHost>

This way you can easily add additional sites as well. That's easy, isn't it?

Last modified on 2011-02-28 by Maik Jablonski

Java with Ease

1970-01-01

Java is a very productive development environment if you choose and combine the right tools and patterns.

Jease is the result of my personal quest bringing tools and patterns together which make the life of a web-developer as simple as it can be.