Paul Clapham wrote:You've got a reference to a ServletConfig object (because it's a parameter of the init() method). A ServletConfig object has a getServletContext() method; if you call that method, what do you suppose it returns?
(Hint: Notice how "ServletConfig" is a link in your post? Click on that link and you'll see the API documentation for it.)
Clapham you are right ,
init(ServletConfig config) is a life cycle method of SERVLET, and it was executed by web container at the time of servlet object creation. Remember this init() method will be executed only onceā¦,
ServletConfig instantiated by web container.
An object of ServletContext is created by the web container at time of deploying the project. This object can be used to get configuration information from web.xml file. There is only one ServletContext object per web application. in the fallowing ways we can get servleet context object.
1. getServletContext() method of ServletConfig interface returns the object of ServletContext.
2. getServletContext() method of GenericServlet class returns the object of ServletContext.