I am trying to put a HashMap on to the ServletContext object so that i can access it from a war file that is on a different context. I have not tried to access it from mutliple contexts yet. I just tried to access from the same servlet and i cant seem to be able to get the value. Both approaches below return 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"));
}
}
The servlet is deployed on a Tomcat container.