Container passes the ServletConfig to the init() method of the Servlet at the initialization time. But how the container pass the ServletContext to which servlet's which method, because it is a application based? I mean how container initialize ServletContext?
ServletContext is inside the ServletConfig, so there's no reason to pass it to a Servlet separately.
A good workman is known by his tools.
posted 12 years ago
So whenever you want to use ServletContext init parameter in your Servlet you have to initialize the Servlet by running it's init(ServletConfig) method. If container already initialized the Servlet (which needs ServletContext init parameter)then you can access ServletContext init parameter directly.Correct me if I am wrong.
When web app is loaded in memory by container by using web.xml of the app , servletContext Object is created and becomes avaible to all servlets and JSPs in the app. Now when a servlet is loaded in memory by container, it creates ServletConfig object and pass it to the servlet. This object contains the ServletContext Object.
What prevents you(ServletConfig) and I(ServletConfig) from using the same train(ServletContext) ? The train doesn't care who's riding in, but you know which train you're using, don't you ? Does it make sense to make ServletContext accessible from the ServletConfig ?