I am going to answer your comment here, I think it answers your question as well.
Your question in the comment is as follows:
resourceMap is local map to this partcular method. after completing
execution of this method resourceMap local variable is going to
destroy. if so then how resourceMap context variable refer to local
variable resourceMap ?
Well resource map is just a local reference. The object itself in the heap. You are right that resourceMap gets lost after the method execution completes. However you are passing a copy of the reference to another object which lives longer. See the following example:
class TestClass {
public static void main(String[] args) {
final ServletContext servletContext = new ServletContext();
final ServletContextEvent servletContextEvent = new ServletContextEvent(servletContext);
final ServletContextListener servletContextListener = new ServletContextListener();
servletContextListener.contextInitialized(servletContextEvent);
final Object resourceMap = servletContext.getMap().get("resourceMap");
// See, resourceMap is still accesiable. Because it still has an alive reference to it from ServletContext!
System.out.println(resourceMap);
// Output will be:
// {1=abc1}
}
}
class ServletContext {
final Map<String, Object> map = new HashMap<String, Object>();
public void setAttribute(String resourceMap, Map<String, String> value) {
this.map.put(resourceMap, value);
}
public Map<String, Object> getMap() {
return map;
}
}
class ServletContextEvent {
private final ServletContext servletContext;
public ServletContextEvent(ServletContext servletContext) {
this.servletContext = servletContext;
}
public ServletContext getServletContext() {
return servletContext;
}
}
class ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
Map<String, String> resourceMap = new HashMap<String, String>();
resourceMap.put("1", "abc1");
sce.getServletContext().setAttribute("resourceMap", resourceMap);
}
}
Does this answer your question?