I have 2 webapps running at two contexts: c1, c2 (both immediately after the root). I put a startupListener in c1 to share a variable, and another one in c2 to retrieve it.
My startuplistener in c1 is:
public void contextInitialized(ServletContextEvent sce) {
HashMap <String,Object> database ;
//some code to init database
ServletContext context = sce.getServletContext().getContext("/c1");
if (context!=null)
{
context.setAttribute("crossContext", true);
context.setAttribute("cache", database);
}
}
In c2 app, it is like this:
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext().getContext("/c1");
HashMap<String,Object> database = (HashMap) context.getAttribute("cache");
}
The context in the startupListener of c2 is always null, I've tried '/c1', 'c1'. What am I missing? (I'm using tomcat6, if that matters) Thanks