When the webapp gets loaded the container will create the ServletContext. There is only one ServletContext per webapp per JVM.
The ServletContext object is contained within the ServletConfig object which is provided by the web server upon servlet initialisation.
Therefore:
- How is ServletContext created by the Container – if it is an interface?
- If there is only one ServletContext per webapp and that is provided to ServletConfig (of which there is one per Servlet) then doesn’t that mean there is one ServletContext per ServletConfig?
I realize this may seem like a very basic question, but I’m trying to make sure I understand the conceptual aspects involved
1 – each container has its own implementation, which is internal to the container. For example, in Jetty, the implementation is org.mortbay.jetty.handler.ContextHandler.SContext.
2 – that’s the logical conclusion, certainly, but I don’t see any reason it needs to be. I would avoid making any assumptions about how many instances of each type of object are created, and just get the appropriate instance through the defined APIs as & when it is required. It’s best not to cache instances of container-created objects, just in case.
2