Shared objects between webapps of the same cat

I have 2 webapps working in two contexts: c1, c2 (both immediately after the root). I set startupListener in c1 to split the variable and the other in c2 to extract it.

My startuplistener in c1:

 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 application, it looks like this:

      public void contextInitialized(ServletContextEvent sce) {
            ServletContext context = sce.getServletContext().getContext("/c1");
            HashMap<String,Object> database = (HashMap) context.getAttribute("cache");

      }

The context in startupListener c2 is always zero, I tried '/ c1', 'c1'. What am I missing? (I use tomcat6 if that matters) Thanks

+5
source share
3 answers

You need to set crossContext = true. From the tomcat docs:

true, , ServletContext.getContext() -, . false ( ) , getContext() null.

http://tomcat.apache.org/tomcat-7.0-doc/config/context.html

+2

:

app2 app1.

" ": ( ) , server.xml:

<Service name="app1">
  <Connector .../>

  <Engine ...>
     <Host appbase="app1" ...>
       ...        
     </Host>
  </Engine>
</Service>
<Service name="app2">
  <Connector .../>

  <Engine ...>
     <Host appbase="app2" ...>
       ...        
     </Host>
  </Engine>
</Service>
0

Another option is to use serialization. Serialize the data in one application and read it in another.

-1
source

All Articles