【问题标题】:ServletContext - Application scoped session object returning nullServletContext - 应用程序范围的会话对象返回 null
【发布时间】:2012-02-25 17:06:01
【问题描述】:

我正在尝试将 HashMap 放在 ServletContext 对象上,以便我可以从位于不同上下文的 war 文件中访问它。我还没有尝试从多个上下文中访问它。我只是尝试从同一个 servlet 访问,但似乎无法获得该值。下面的两种方法都返回空指针异常。

public class TestServlet extends HttpServlet{

private static final long serialVersionUID = -8002515227440283546L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException{
    PrintWriter out = response.getWriter();

    HashMap<String ,String> hm = new HashMap<String, String>();
    hm.put("1", "1");

    this.getServletContext().setAttribute("usernamanager", hm);
    this.getServletConfig().getServletContext().setAttribute("usernamanager", hm);  

    HashMap newMap2 = (HashMap) this.getServletConfig().getServletContext().getAttribute("usermanager"); 
    HashMap newMap3 = (HashMap) this.getServletContext().getAttribute("usermanager"); 

    out.println("App1 - " + newMap2.get("1"));
    out.println("App1 - " + newMap3.get("1"));
}
}

servlet 部署在 Tomcat 容器上。

【问题讨论】:

  • 属性名称不匹配...“usernamanager”和“usermanager”
  • 你是对的。这是我的一个愚蠢的错误。谢谢
  • 请将此作为答案发布并且不接受。

标签: java session tomcat servlets


【解决方案1】:

属性名称应匹配:usernamanager vs usermanager

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-07
    • 2013-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多