我正在尝试将HashMap放到ServletContext对象上,以便我可以从位于不同上下文的war文件中访问它。我还没有尝试从mutliple上下文访问它。我只是试图从同一个servlet访问,我似乎无法获得价值。以下两种方法都返回nullpointerexceptions。
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容器上。
答案 0 :(得分:1)
属性名称应匹配:usernamanager
vs usermanager