【发布时间】:2013-03-02 12:23:31
【问题描述】:
我有 2 个 web 应用程序在两个上下文中运行:c1、c2(都紧跟在根目录之后)。我在 c1 中放了一个 startupListener 来共享一个变量,在 c2 中放另一个来检索它。
我在 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);
}
}
在c2 app中是这样的:
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext().getContext("/c1");
HashMap<String,Object> database = (HashMap) context.getAttribute("cache");
}
c2 的 startupListener 中的 context 总是为 null,我试过 '/c1', 'c1'。我错过了什么? (如果这很重要,我正在使用tomcat6) 谢谢
【问题讨论】:
-
它检索到的值为 null 而不是我猜的上下文?
-
不,上下文为空,下一行触发NPE。
标签: java web-applications servlets tomcat6