0

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.

3
  • The attribute names do not match..."usernamanager" and "usermanager" Commented Feb 25, 2012 at 17:12
  • You are right. Its a silly mistake from on my part. Thanks Commented Feb 25, 2012 at 17:18
  • Please post this as an answer and ill accept. Commented Feb 25, 2012 at 17:31

1 Answer 1

1

The attribute names should match: usernamanager vs usermanager

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.