0

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:

  1. How is ServletContext created by the Container - if it is an interface?
  2. 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

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.

  • Ok, thanks I never considered the container's implementation. For my Tomcat it would be ApplicationContext which implements the ServletContext interface. – noobie Feb 27 '15 at 9:37
  • As for how it is passed to the ServletConfig, I can only guess at this point that it is passed a reference. Because it is possible to set and add attributes to the ServletContext at runtime. – noobie Feb 27 '15 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.