同一个tomcat的webapps之间的共享对象 [英] shared objects between webapps of the same tomcat
问题描述
我有2个webapps在两个上下文中运行:c1,c2(都紧跟在root之后)。我把一个startupListener放在c1中共享一个变量,另一个放在c2中来检索它。
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.
我在c1中的startuplistener是:
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);
}
}
在c2应用中,它就像这个:
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");
}
c2的startupListener中的上下文始终为null,I '试过'/ c1','c1'。我错过了什么?
(我正在使用tomcat6,如果这很重要)
谢谢
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
推荐答案
你需要设置crossContext =真。从tomcat docs:
You need to set crossContext=true. From the tomcat docs:
如果要在此应用程序中调用
ServletContext.getContext()成功返回,则设置为true在此虚拟主机上运行的其他Web应用程序的请求
调度程序。
在安全意识环境中设置为false(默认值),使
getContext()始终返回null。
Set to true if you want calls within this application to ServletContext.getContext() to successfully return a request dispatcher for other web applications running on this virtual host. Set to false (the default) in security conscious environments, to make getContext() always return null.
http://tomcat.apache.org/tomcat-7.0-doc /config/context.html
这篇关于同一个tomcat的webapps之间的共享对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!