title: Getting two servlets to use the same object using Servlet context
body: <p>I am trying to write a simple program where i can put a HashMap on an application scoped session and get two applications/contexts deployed as two war files access the HashMap. </p>
  
  <p>Servlet 1</p>
  
  <pre><code>public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException{
          PrintWriter out = response.getWriter();
  
          HashMap&lt;String ,String&gt; hm = new HashMap&lt;String, String&gt;();
          hm.put("1", "1");
  
          this.getServletContext().setAttribute("usermanager", hm);
          this.getServletConfig().getServletContext().setAttribute("usermanager2", hm);   
      }
  </code></pre>
  
  <p>Servlet 2</p>
  
  <pre><code>public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws IOException{
  
      HashMap newMap2 = (HashMap) this.getServletConfig().getServletContext().getAttribute("usermanager"); 
      HashMap newMap3 = (HashMap) this.getServletContext().getAttribute("usermanager2"); 
  
      System.out.println("newmap2 size " + newMap2.size());
      System.out.println("newmap3 size " + newMap3.size());   
  }
  </code></pre>
  
  <p>To test this i restarted Tomcat 6 then run access Servlet 1 first so that it initializes the Hashmap object. When i subsequently access Servlet 2 i get a NULL-POINTER error which is pointing at the line number where i tried to call newMap2.size()</p>
  
  <p>What am i doing wrong?</p>
  
  <pre><code>java.lang.NullPointerException
      at com.TestServlet.doGet(TestServlet.java:24)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Thread.java:619)
  </code></pre>