Difference between ServletContext and ServletConfig

By Kablu Mandal

ServletContext Defines a set of methods that a servlet uses to communicate with its servlet container.

ServletConfig is a servlet configuration object used by a servlet container used to pass information to a servlet during initialization. All of its initialization parameters can ONLY be set in deployment descriptor.



The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized.

You can specify param-value pairs for ServletContext object in <context-param> tags in web.xml file.

The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets.

The ServletContext parameters are specified for an entire application outside of any particular servlet and are available to all the servlets within that application.


ServletContext 
 ServletContext is implemented by the servlet container for all servlet to communicate with its servlet container, for example, to get the MIME type of a file, to get dispatch requests, or to write to a log file.

ServletConfig ServletConfig is implemented by the servlet container to initialize a single servlet using init().



ServletContext : Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized .

ServletConfig : The object created after a servlet is instantiated and its default constructor is read. It is created to pass initialization information to the servlet.

One ServletConfig object is created per servlet

One ServletContext object is created per Application



Can be used to pass deployment time information to the Servlet. Information is stored as parameters in the deployment descriptor(web.xml)




Can be used to share Application level information across servlets and is this information is also stored in the web.xml as well as parameters (So it kind of acts as GlobalContext or Application Context)




Provides a set of methods to set and get the initialization configuration information


Provides a set of methods to the servlet so that servlet can communicate with the Container


Servlet can access a ServletContext object only through ServletConfig object

Comments

Popular Posts