Mark,
On Fri, Sep 21, 2018 at 12:54 AM Mark Thomas <[email protected]> wrote:
> On 21/09/18 05:57, Igal Sapir wrote:
> > I want to embed Tomcat in a simple application that does not use JSP, and
> > that sets the default servlet's listings initParam to true.
> >
> > When I use the StandardContext, Tomcat.initWebappDefaults() [1] is called
> > and adds the default servlet and the JSP servlet. I would like to
> prevent
> > that, but hopefully without having to rewrite the whole Tomcat class nor
> to
> > subclass it.
> >
> > Further, when I try to override a setting in WEB-INF/web.xml, e.g. to
> > specify init-param of "listings" with value "true" for the servlet
> > "default", I get an error that "default" is not unique, but in a regular
> > Tomcat deployment that works just fine.
> >
> > Is there an easy way to override Tomcat.initWebappDefaults() or to
> prevent
> > it from being called?
>
> There are a couple few options.
>
> Sub-classing looks to be the simplest.
>
> You can use addContext() rather than addWebapp() but then you become
> responsible for all of the configuration. If the app is simple, this
> shouldn't be too much effort.
>
The app is simple, but it is for use by other developers who may use their
own web.xml files, so I think that sub-classing would be much easier and
that's what I did.
While searching for a solution prior to asking on the mailing list, I
noticed that quite a few users were looking for a simple solution of using
addWebapp() without setting up the default servlets. What do you think
about adding a System Property that will allow to opt-out of
initWebappDefaults() and set the DefaultWebXml to null so that the web.xml
files will be parsed?
I am thinking of a property name
org.apache.catalina.startup.INIT_WEBAPP_DEFAULTS [default true], or
org.apache.catalina.startup.DISABLE_WEBAPP_DEFAULTS [default false].
Alternatively, we can add the sub-classing implementation that I used (with
the proper ASF license headers, of course). The code is pasted below in
case anyone else needs such an implementation.
I can add either solution myself if approved.
Thank you,
Igal
/**
* This class extends Tomcat to override the default functionality which
registers the default and JSP servlets, and
* prevents the parsing of the Web Application Deployment Descriptors
web.xml.
*
* Using this class therefore enables the parsing of the web.xml files, and
does not add any defaults beyond them.
*
* @author Igal Sapir
*/
public class TomcatRunner extends Tomcat {
/**
* Tomcat.addWebapp() sets DefaultWebXml to a non-null value to prevent
the parsing of the web.xml file(s).
* Here we want to parse those files, so after calling the super method
we set that value to null.
*
* @param host The host in which the context will be deployed
* @param contextPath The context mapping to use, "" for root context.
* @param docBase Base directory for the context, for static files.
* Must exist, relative to the server home
* @param config Custom context configurator helper
* @return the deployed context
* @see #addWebapp(String, String)
*/
public Context addWebapp(Host host, String contextPath, String docBase,
LifecycleListener config) {
Context ctx = super.addWebapp(host, contextPath, docBase, config);
((ContextConfig) config).setDefaultWebXml(null);
return ctx;
}
/**
* Returns a listener that does nothing, as opposed to the default one
in Tomcat which always
* adds the default servlet and the JSP servlet with the rules that are
set in the defautl web.xml
* @return a listener that does nothing.
*/
@Override
public LifecycleListener getDefaultWebXmlListener() {
// noop
return event -> {};
}
}