Skip to main content

Difference between ServletConfig and ServletContext

Difference : ServletContext vs ServletConfig

ServletContextServletConfig
Servlet Context is a global configuration for servlets in a an application i.e this is same for all the servlets. Servlet Config is a local configuration for each servlet in an application i.e each servlet has its own servlet configuration that may differ from the servlet config of another servlet.
ServletContext is one per application.ServletConfig is one per servlet.
 It is created at the time of deployment of the web application.It is initialized when a servlet is initialized.
 Its scope is as long as the application is up and running, it will be destroyed when the application is removed from the server. Its scope isand is destroyed when a servlet is destroyed by the servlet container.
 In web.xml it should be declared as <context-param> tag and it should be under <web-app> tag.In web.xml it should be declared as <init-param> under <servlet-class> tag.
ServletContext object will be available even before giving the first requestWe should give request explicitly, in order to create ServletConfig object for the first time
Example


Comments