Class Tomcat


public class Tomcat extends Object
Minimal tomcat starter for embedding/unit tests.

Tomcat supports multiple styles of configuration and startup - the most common and stable is server.xml-based, implemented in org.apache.catalina.startup.Bootstrap.

This class is for use in apps that embed tomcat.


  • all tomcat classes and possibly servlets are in the classpath. (for example all is in one big jar, or in eclipse CP, or in any other combination)
  • we need one temporary directory for work files
  • no config file is required. This class provides methods to use if you have a webapp with a web.xml file, but it is optional - you can use your own servlets.

There are a variety of 'add' methods to configure servlets and webapps. These methods, by default, create a simple in-memory security realm and apply it. If you need more complex security processing, you can define a subclass of this class.

This class provides a set of convenience methods for configuring web application contexts; all overloads of the method addWebapp(). These methods are equivalent to adding a web application to the Host's appBase (normally the webapps directory). These methods create a Context, configure it with the equivalent of the defaults provided by conf/web.xml (see initWebappDefaults(String) for details) and add the Context to a Host. These methods do not use a global default web.xml; rather, they add a LifecycleListener to configure the defaults. Any WEB-INF/web.xml and META-INF/context.xml packaged with the application will be processed normally. Normal web fragment and ServletContainerInitializer processing will be applied.

In complex cases, you may prefer to use the ordinary Tomcat API to create webapp contexts; for example, you might need to install a custom Loader before the call to Container.addChild(Container). To replicate the basic behavior of the addWebapp methods, you may want to call two methods of this class: noDefaultWebXmlPath() and getDefaultWebXmlListener().

getDefaultWebXmlListener() returns a LifecycleListener that adds the standard DefaultServlet, JSP processing, and welcome files. If you add this listener, you must prevent Tomcat from applying any standard global web.xml with ...

noDefaultWebXmlPath() returns a dummy pathname to configure to prevent ContextConfig from trying to apply a global web.xml file.

This class provides a main() and few simple CLI arguments, see setters for doc. It can be used for simple tests and demo.

Costin Manolache
See Also: