title: How to configure programatically the session tracking modes for Tomcat
body: <p>I'm using an embedded tomcat server version 7. I want to programmatically configure the server with the session tracking mode (in fact I search a way to get rid of jsessionid i.e neither in the cookies nor in the url).
  But there is no a method in <strong>org.apache.catalina.Context</strong> to simply configure this property like in a web.xml descriptor file (I cannot use such file).
  So what is the best way to do it?</p>
  
  <p>I tried the above code</p>
  
  <pre><code>Context ctxt = tomcat.addWebapp(.......);
  ctxt.getServletContext().setSessionTrackingModes(
          my_modes);
  </code></pre>
  
  <p>But this method failed with the error</p>
  
  <pre><code>java.lang.IllegalStateException: The session tracking modes for context / cannot be set whilst the context is running
  at org.apache.catalina.core.ApplicationContext.setSessionTrackingModes(ApplicationContext.java:1235)
  at org.apache.catalina.core.ApplicationContextFacade.setSessionTrackingModes(ApplicationContextFacade.java:611)
  </code></pre>
  
  <p>I tried to use <a href="https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/Context.html#addApplicationListener(java.lang.String)" rel="nofollow">https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/Context.html#addApplicationListener(java.lang.String)</a>. The listener on contextInitialized set the tracking mode</p>
  
  <pre><code>servletContextEvent.getServletContext().setSessionTrackingModes(
          EnumSet.copyOf(sessionTrackingModes));
  </code></pre>
  
  <p>But I don't know how to configure tomcat with an instance of this listener because with the last method <strong>Context.addApplicationListener</strong> the method takes the className of the listener and when running the server fails to instantiate my listener because the absence of an empty constructor (my listener constructor takes the list of sessionTrackingMode.. cannot be configurable)</p>
  
  <pre><code>[read-1] ERROR Aug 18 19:10:03 - Error configuring application listener of class xx.xx.xx.SessionTrackingListener
  java.lang.InstantiationException: xx.xx.xx.SessionTrackingListener
  at java.lang.Class.newInstance(Class.java:427)
  at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:125)
  at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4715)
  ..............
  Caused by: java.lang.NoSuchMethodException: xx.xx.xx.xx.SessionTrackingListener.&lt;init&gt;()
  at java.lang.Class.getConstructor0(Class.java:3082)
  at java.lang.Class.newInstance(Class.java:412)
  </code></pre>
  
  <p>So do you suggest a better way to do it</p>