public class Tomcat extends Object
addWebapp
. These methods
create a webapp context, configure it, and then add it to a Host
.
They do not use a global default web.xml; rather, they add a lifecycle
listener that adds the standard DefaultServlet, JSP processing, and welcome
files.
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 String |
basedir |
protected Connector |
connector |
protected Engine |
engine |
protected Host |
host |
protected String |
hostname |
protected int |
port |
protected Server |
server |
protected Service |
service |
Constructor and Description |
---|
Tomcat() |
Modifier and Type | Method and Description |
---|---|
Context |
addContext(Host host,
String contextPath,
String dir) |
Context |
addContext(Host host,
String contextPath,
String contextName,
String dir) |
Context |
addContext(String contextPath,
String docBase)
Add a context - programmatic mode, no default web.xml used.
|
void |
addRole(String user,
String role)
Add a role to a user.
|
static Wrapper |
addServlet(Context ctx,
String servletName,
Servlet servlet)
Static version of
addServlet(String, String, Servlet) . |
static Wrapper |
addServlet(Context ctx,
String servletName,
String servletClass)
Static version of
addServlet(String, String, String) |
Wrapper |
addServlet(String contextPath,
String servletName,
Servlet servlet)
Add an existing Servlet to the context with no class.forName or
initialisation.
|
Wrapper |
addServlet(String contextPath,
String servletName,
String servletClass)
Equivalent to <servlet><servlet-name><servlet-class>.
|
void |
addUser(String user,
String pass)
Add a user for the in-memory realm.
|
Context |
addWebapp(Host host,
String contextPath,
String docBase) |
Context |
addWebapp(Host host,
String contextPath,
String docBase,
ContextConfig config) |
Context |
addWebapp(String contextPath,
String docBase)
This is equivalent to adding a web application to Tomcat's webapps
directory.
|
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.
|
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 URL |
getWebappConfigFile(String path,
String contextName) |
void |
init()
Initialize the server.
|
protected void |
initBaseDir() |
static void |
initWebappDefaults(Context ctx)
Static version of
initWebappDefaults(String) |
void |
initWebappDefaults(String contextPath)
Provide default configuration for a context.
|
String |
noDefaultWebXmlPath() |
void |
setBaseDir(String basedir)
Tomcat needs a directory for temp files.
|
void |
setConnector(Connector connector) |
void |
setHost(Host host)
Sets the current host - all future webapps will
be added to this host.
|
void |
setHostname(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 Service service
protected Engine engine
protected Connector connector
protected Host host
protected int port
protected String hostname
protected String basedir
public void setBaseDir(String basedir)
basedir
- The Tomcat base folder on which all others
will be derivedpublic void setPort(int port)
port
- The port numberpublic void setHostname(String s)
s
- The default host namepublic Context addWebapp(String contextPath, String docBase) throws ServletException
ServletContainerInitializer
processing will be
applied.contextPath
- The context mapping to use, "" for root context.docBase
- Base directory for the context, for static files.
Must exist, relative to the server homeServletException
- if a deployment error occurspublic Context addContext(String contextPath, 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 restcontextPath
- 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(String contextPath, String servletName, String servletClass)
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, String servletName, 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(String contextPath, 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, 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() 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(String user, String pass)
user
- The user namepass
- The passwordpublic void addRole(String user, String role)
user
- The user namerole
- The role nameaddUser(String, String)
public Connector getConnector()
public void setConnector(Connector connector)
public Service getService()
public void setHost(Host host)
host
- The current hostpublic Host getHost()
public Engine getEngine()
public Server getServer()
public Context addContext(Host host, String contextPath, 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, String contextPath, String contextName, 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, String contextPath, String docBase)
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, relative to the server homeaddWebapp(String, String)
public Context addWebapp(Host host, String contextPath, String docBase, ContextConfig config)
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, relative to the server homeconfig
- Custom context configurator helperaddWebapp(String, String)
public LifecycleListener getDefaultWebXmlListener()
Lifecycle.addLifecycleListener(LifecycleListener)
and then pass the
result of noDefaultWebXmlPath()
to
ContextConfig.setDefaultWebXml(String)
.public 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 enableNaming()
Lifecycle
in order for the NamingContextListener
to be
used.public void initWebappDefaults(String contextPath)
contextPath
- The context to set the defaults forpublic static void initWebappDefaults(Context ctx)
initWebappDefaults(String)
ctx
- The context to set the defaults forCopyright © 2000-2016 Apache Software Foundation. All Rights Reserved.