Bug 51558 - Tomcat Embedded: using tomcat.addWebapp always overrides programmatic context-settings with default values for the context at start of tomcat server
Summary: Tomcat Embedded: using tomcat.addWebapp always overrides programmatic context...
Alias: None
Product: Tomcat 7
Classification: Unclassified
Component: Catalina (show other bugs)
Version: 7.0.16
Hardware: PC Linux
: P2 normal (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
Depends on:
Reported: 2011-07-26 10:11 UTC by bhuber
Modified: 2011-08-26 16:09 UTC (History)
0 users

Attached patch that only sets StandardManager if the manager is not already set (566 bytes, patch)
2011-08-24 22:56 UTC, Martin Grotzke
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description bhuber 2011-07-26 10:11:48 UTC
when i deploy an webapp with a tomcat server, i use the Function "tomcat.addWebapp". I looked into the code of this function and it makes the following call:

     ctx.addLifecycleListener(new DefaultWebXmlListener());

When i start tomcat, this DefaultWebXmlListener is activated automatically, and always does the following code, so the manager i set for my context gets overriden with this code:

     public static void initWebappDefaults(Context ctx) {
        // Sessions
        ctx.setManager( new StandardManager());

so when i set a StandardManager programatically, it gets overriden by this code. The workaround i used, is to unregister the DefaultWebXmlListener, and than add my own context.xml specific settings, like a custom manager for deactivating session persistance:

     Context warContext = tomcat.addWebapp(warContextPath, warLocation);
     LifecycleListener[] lclisteners = warContext.findLifecycleListeners();
     LifecycleListener defaultWebXMLListener = null;
     for (int i=0; i < lclisteners.length; i++){
       if (lclisteners[i] instanceof DefaultWebXmlListener){
         defaultWebXMLListener = lclisteners[i];
     StandardManager manager = new StandardManager();
     manager.setPathname(null); // disable session persistance:

i think its a bug that using the "tomcat.addWebapp" method makes it impossible to set context.xml specific settings programmatically, because they always get overriden by default values at the tomcat start.
Comment 1 Mark Thomas 2011-07-26 10:24:27 UTC
Use addContext()
Comment 2 bhuber 2011-07-26 11:13:18 UTC
(In reply to comment #1)
> Use addContext()

because i need to deploy a WAR-File, that includes a web.xml file, it would be  more complicated to use addContext(), because i cant find any examples in the web that use addContext() in combination with web.xml...

but your right, this should be a missing feature, not a bug. I hope some official documentation for tomcat-embedded will be available soon.
Comment 3 bhuber 2011-07-26 11:20:10 UTC
ah sorry, its ok nevermind .. i know now how to do it: ;-)
like you said, i will now just create a standardcontext instead, 
and just dont add a defaultwebxmllistener ...

  Context ctx = new StandardContext();
  if (defaultRealm == null) {
  ContextConfig ctxCfg = new ContextConfig();
  // prevent it from looking ( if it finds one - it'll have dup error )
  if (host == null) {
  } else {
Comment 4 Martin Grotzke 2011-08-24 22:47:40 UTC
A user of memcached-session-manager also ran into this issue and it took me some time that when using addWebapp the manager is just overwritten.

I'd suggest to set the StandardManager only if there's no manager set on the context.

Are there any problems why this should not be done?
Comment 5 Martin Grotzke 2011-08-24 22:56:36 UTC
Created attachment 27432 [details]
Attached patch that only sets StandardManager if the manager is not already set
Comment 6 Mark Thomas 2011-08-26 16:09:52 UTC
StandardContext already conditionally adds a StandardManager if one is not set.

Since the offending code is meant to be replicating conf/web.xml (which can't set internal Tomcat components) I have simply removed this code from the listener.

The fix has been applied to trunk and 7.0.x and will be included in 7.0.21 onwards.