How actually applicationServlet manages context varibles?

104 Views Asked by At

How actually applicationServlet manages context varibles ? If I set variables in applicationContext level whether it stores in Method area or stack area or heap , if so how it stored and how its accessed ?

Here i created local variable as MAP and put some values in applicationContext level.

I wrote some sample code below here.

    public class ContextLoaderListener implements ServletContextListener {
    private ServletContext ctx;
    private WebApplicationContext springContext;

    @Override
    public void contextInitialized(ServletContextEvent sce) {
    Map<String, String> resourceMap=new HashMap<>();
    resourceMap.put("1","abc1");
    resourceMap.put("2","abc2");
    resourceMap.put("3","abc3");
    sce.getServletContext().setAttribute("resourceMap", resourceMap);
    }
    @Override
    public void contextDestroyed(ServletContextEvent sce) { }
    }
1

There are 1 best solutions below

0
On BEST ANSWER

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?