public class Tomcat
extends java.lang.Object
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.
Requirements:
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.
| Modifier and Type | Class and Description |
|---|---|
static class |
Tomcat.DefaultWebXmlListener
Fix reload - required if reloading and using programmatic configuration.
|
static class |
Tomcat.ExistingStandardWrapper
Helper class for wrapping existing servlets.
|
static class |
Tomcat.FixContextListener
Fix startup sequence - required if you don't use web.xml.
|
| Modifier and Type | Field and Description |
|---|---|
protected java.lang.String |
basedir |
protected java.lang.String |
hostname |
protected int |
port |
protected Server |
server |
| Constructor and Description |
|---|
Tomcat() |
| Modifier and Type | Method and Description |
|---|---|
Context |
addContext(Host host,
java.lang.String contextPath,
java.lang.String dir) |
Context |
addContext(Host host,
java.lang.String contextPath,
java.lang.String contextName,
java.lang.String dir) |
Context |
addContext(java.lang.String contextPath,
java.lang.String docBase)
Add a context - programmatic mode, no default web.xml used.
|
static void |
addDefaultMimeTypeMappings(Context context)
Add the default MIME type mappings to the provide Context.
|
void |
addRole(java.lang.String user,
java.lang.String role)
Add a role to a user.
|
static Wrapper |
addServlet(Context ctx,
java.lang.String servletName,
Servlet servlet)
Static version of
addServlet(String, String, Servlet). |
static Wrapper |
addServlet(Context ctx,
java.lang.String servletName,
java.lang.String servletClass)
Static version of
addServlet(String, String, String) |
Wrapper |
addServlet(java.lang.String contextPath,
java.lang.String servletName,
Servlet servlet)
Add an existing Servlet to the context with no class.forName or initialisation.
|
Wrapper |
addServlet(java.lang.String contextPath,
java.lang.String servletName,
java.lang.String servletClass)
Equivalent to <servlet><servlet-name><servlet-class>.
|
void |
addUser(java.lang.String user,
java.lang.String pass)
Add a user for the in-memory realm.
|
Context |
addWebapp(Host host,
java.lang.String contextPath,
java.lang.String docBase)
This is equivalent to adding a web application to a Host's appBase (usually Tomcat's webapps directory).
|
Context |
addWebapp(Host host,
java.lang.String contextPath,
java.lang.String docBase,
LifecycleListener config)
This is equivalent to adding a web application to a Host's appBase (usually Tomcat's webapps directory).
|
Context |
addWebapp(java.lang.String contextPath,
java.lang.String docBase)
This is equivalent to adding a web application to a Host's appBase (usually Tomcat's webapps directory).
|
Context |
addWebapp(java.lang.String contextPath,
java.net.URL source)
Copy the specified WAR file to the Host's appBase and then call
addWebapp(String, String) with the newly
copied WAR. |
protected Realm |
createDefaultRealm()
Create an in-memory realm.
|
void |
destroy()
Destroy the server.
|
void |
enableNaming()
Enables JNDI naming which is disabled by default.
|
Connector |
getConnector()
Get the default HTTP connector that is used by the embedded Tomcat.
|
LifecycleListener |
getDefaultWebXmlListener()
Return a listener that provides the required configuration items for JSP processing.
|
Engine |
getEngine()
Access to the engine, for further customization.
|
Host |
getHost() |
Server |
getServer()
Get the server object.
|
Service |
getService()
Get the service object.
|
protected java.net.URL |
getWebappConfigFile(java.lang.String path,
java.lang.String contextName) |
void |
init()
Initialize the server.
|
void |
init(ConfigurationSource source)
Initialize the server given the specified configuration source.
|
void |
init(ConfigurationSource source,
java.lang.String[] catalinaArguments)
Initialize the server given the specified configuration source.
|
protected void |
initBaseDir() |
static void |
initWebappDefaults(Context ctx)
Static version of
initWebappDefaults(String). |
void |
initWebappDefaults(java.lang.String contextPath)
Provide default configuration for a context.
|
static void |
main(java.lang.String[] args)
Main executable method for use with a Maven packager.
|
java.lang.String |
noDefaultWebXmlPath() |
void |
setAddDefaultWebXmlToWebapp(boolean addDefaultWebXmlToWebapp)
By default, when calling addWebapp() to create a Context, the settings from from the default web.xml are added to
the context.
|
void |
setBaseDir(java.lang.String basedir)
Tomcat requires that the base directory is set because the defaults for a number of other locations, such as the
work directory, are derived from the base directory.
|
void |
setConnector(Connector connector)
Set the specified connector in the service, if it is not already present.
|
void |
setHost(Host host)
Sets the current host - all future webapps will be added to this host.
|
void |
setHostname(java.lang.String s)
The the hostname of the default host, default is 'localhost'.
|
void |
setPort(int port)
Set the port for the default connector.
|
void |
setSilent(boolean silent)
Controls if the loggers will be silenced or not.
|
void |
start()
Start the server.
|
void |
stop()
Stop the server.
|
protected Server server
protected int port
protected java.lang.String hostname
protected java.lang.String basedir
public void setBaseDir(java.lang.String basedir)
If this method is not called then Tomcat will attempt to use these locations in the following order:
setPort(int) which defaults to 8080 if not setTODO: disable work dir if not needed ( no jsp, etc ).
basedir - The Tomcat base folder on which all others will be derivedpublic void setPort(int port)
port - The port numberpublic void setHostname(java.lang.String s)
s - The default host namepublic Context addWebapp(java.lang.String contextPath, java.lang.String docBase)
initWebappDefaults(String)). This may be prevented by calling
setAddDefaultWebXmlToWebapp(boolean) with false. Any WEB-INF/web.xml and
META-INF/context.xml packaged with the application will always be processed and normal web fragment
and ServletContainerInitializer processing will always be applied.contextPath - The context mapping to use, "" for root context.docBase - Base directory for the context, for static files. Must exist and be an absolute path.public Context addWebapp(java.lang.String contextPath, java.net.URL source) throws java.io.IOException
addWebapp(String, String) with the newly
copied WAR. The WAR will NOT be removed from the Host's appBase when the Tomcat instance stops. Note that
ExpandWar provides utility methods that may be used to delete the WAR and/or expanded directory if
required.contextPath - The context mapping to use, "" for root context.source - The location from which the WAR should be copiedjava.io.IOException - If an I/O error occurs while copying the WAR file from the specified URL to the appBasepublic Context addContext(java.lang.String contextPath, java.lang.String docBase)
ServletContainerInitializer processing and no annotation processing. If a
ServletContainerInitializer is added programmatically, there will still be no scanning for
HandlesTypes matches.
API calls equivalent with web.xml:
// context-param
ctx.addParameter("name", "value");
// error-page
ErrorPage ep = new ErrorPage();
ep.setErrorCode(500);
ep.setLocation("/error.html");
ctx.addErrorPage(ep);
ctx.addMimeMapping("ext", "type");
Note: If you reload the Context, all your configuration will be lost. If you need reload support, consider using a LifecycleListener to provide your configuration.
TODO: add the rest
contextPath - The context mapping to use, "" for root context.docBase - Base directory for the context, for static files. Must exist, relative to the server homepublic Wrapper addServlet(java.lang.String contextPath, java.lang.String servletName, java.lang.String servletClass)
In general it is better/faster to use the method that takes a Servlet as param - this one can be used if the servlet is not commonly used, and want to avoid loading all deps. ( for example: jsp servlet ) You can customize the returned servlet, ex:
wrapper.addInitParameter("name", "value");
contextPath - Context to add Servlet toservletName - Servlet name (used in mappings)servletClass - The class to be used for the Servletpublic static Wrapper addServlet(Context ctx, java.lang.String servletName, java.lang.String servletClass)
addServlet(String, String, String)ctx - Context to add Servlet toservletName - Servlet name (used in mappings)servletClass - The class to be used for the Servletpublic Wrapper addServlet(java.lang.String contextPath, java.lang.String servletName, Servlet servlet)
contextPath - Context to add Servlet toservletName - Servlet name (used in mappings)servlet - The Servlet to addpublic static Wrapper addServlet(Context ctx, java.lang.String servletName, Servlet servlet)
addServlet(String, String, Servlet).ctx - Context to add Servlet toservletName - Servlet name (used in mappings)servlet - The Servlet to addpublic void init(ConfigurationSource source)
source - The configuration sourcepublic void init(ConfigurationSource source, java.lang.String[] catalinaArguments)
source - The configuration sourcecatalinaArguments - The arguments that should be passed to Catalinapublic void init()
throws LifecycleException
LifecycleException - Init errorpublic void start()
throws LifecycleException
LifecycleException - Start errorpublic void stop()
throws LifecycleException
LifecycleException - Stop errorpublic void destroy()
throws LifecycleException
LifecycleException - Destroy errorpublic void addUser(java.lang.String user,
java.lang.String pass)
user - The user namepass - The passwordpublic void addRole(java.lang.String user,
java.lang.String role)
user - The user namerole - The role nameaddUser(String, String)public Connector getConnector()
public void setConnector(Connector connector)
connector - The connector instance to addpublic Service getService()
public void setHost(Host host)
host - The current hostpublic Host getHost()
public Engine getEngine()
public Server getServer()
public Context addContext(Host host, java.lang.String contextPath, java.lang.String dir)
host - The host in which the context will be deployedcontextPath - The context mapping to use, "" for root context.dir - Base directory for the context, for static files. Must exist, relative to the server homeaddContext(String, String)public Context addContext(Host host, java.lang.String contextPath, java.lang.String contextName, java.lang.String dir)
host - The host in which the context will be deployedcontextPath - The context mapping to use, "" for root context.contextName - The context namedir - Base directory for the context, for static files. Must exist, relative to the server homeaddContext(String, String)public Context addWebapp(Host host, java.lang.String contextPath, java.lang.String docBase)
initWebappDefaults(String)). This may be prevented by calling
setAddDefaultWebXmlToWebapp(boolean) with false. Any WEB-INF/web.xml and
META-INF/context.xml packaged with the application will always be processed and normal web fragment
and ServletContainerInitializer processing will always be applied.host - The host in which the context will be deployedcontextPath - The context mapping to use, "" for root context.docBase - Base directory for the context, for static files. Must exist and be an absolute path.public Context addWebapp(Host host, java.lang.String contextPath, java.lang.String docBase, LifecycleListener config)
initWebappDefaults(String)). This may be prevented by calling
setAddDefaultWebXmlToWebapp(boolean) with false. Any WEB-INF/web.xml and
META-INF/context.xml packaged with the application will always be processed and normal web fragment
and ServletContainerInitializer processing will always be applied.host - The host in which the context will be deployedcontextPath - The context mapping to use, "" for root context.docBase - Base directory for the context, for static files. Must exist and be an absolute path.config - Custom context configuration helper. Any configuration will be in addition to equivalent of
the default web.xml configuration described above.public LifecycleListener getDefaultWebXmlListener()
Lifecycle.addLifecycleListener(LifecycleListener) and then pass the result of
noDefaultWebXmlPath() to ContextConfig.setDefaultWebXml(String).public java.lang.String noDefaultWebXmlPath()
ContextConfig.setDefaultWebXml(String) when using
getDefaultWebXmlListener().protected Realm createDefaultRealm()
protected void initBaseDir()
public void setSilent(boolean silent)
silent - true sets the log level to WARN for the loggers that log information on Tomcat start
up. This prevents the usual startup information being logged. false sets the log
level to the default level of INFO.public void setAddDefaultWebXmlToWebapp(boolean addDefaultWebXmlToWebapp)
false value prior to calling addWebapp() allows to opt out
of the default settings. In that event you will need to add the configurations yourself, either programmatically
or by using web.xml deployment descriptors.addDefaultWebXmlToWebapp - false will prevent the class from automatically adding the default
settings when calling addWebapp(). true will add the default
settings and is the default behavior.addWebapp(Host, String, String, LifecycleListener)public void enableNaming()
Lifecycle in order for the
NamingContextListener to be used.public void initWebappDefaults(java.lang.String contextPath)
contextPath - The path of the context to set the defaults forpublic static void initWebappDefaults(Context ctx)
initWebappDefaults(String).ctx - The context to set the defaults forpublic static void addDefaultMimeTypeMappings(Context context)
context - The web application to which the default MIME type mappings should be added.protected java.net.URL getWebappConfigFile(java.lang.String path,
java.lang.String contextName)
public static void main(java.lang.String[] args)
throws java.lang.Exception
args - the command line argumentsjava.lang.Exception - if an error occursCopyright © 2000-2024 Apache Software Foundation.
Apache Tomcat, Tomcat, Apache, the Apache Tomcat logo and the Apache logo are either registered trademarks or trademarks of the Apache Software Foundation.