What is the difference between ServletConfig
and ServletContext
interface?
ServletConfig is used for sharing init parameters specific to a servlet while ServletContext is for sharing init parameters within any Servlet within a web application.
ServletConfig is used by only single servlet to get configuration information, whereas ServletContext is used by multiple objects to get configuration information. ServletConfig object is one per servlet class and destroyed once the servlet execution is completed.
ServletConfig is an object containing some initial parameters or configuration information created by Servlet Container and passed to the servlet during initialization. ServletConfig is for a particular servlet, which means one should store servlet-specific information in web. xml and retrieve them using this object.
The object of ServletContext provides an interface between the container and servlet. The ServletContext object can be used to get configuration information from the web. xml file. The ServletContext object can be used to set, get or remove attribute from the web.
The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets. It is used for intializing purposes.
The ServletContext parameters are specified for an entire application outside of any particular servlet and are available to all the servlets within that application. It is application scoped and thus globally accessible across the pages.
Source : Difference between ServletConfig and ServletContext in Java
ServletConfig
ServletConfig
available in javax.servlet.*;
package
ServletConfig
object is one per servlet class
Object of ServletConfig
will be created during initialization process of the servlet
This Config object is public to a particular servlet only
Scope: As long as a servlet is executing, ServletConfig
object will be available, it will be destroyed once the servlet execution is completed.
We should give request explicitly, in order to create ServletConfig
object for the first time
In web.xml – <init-param>
tag will be appear under <servlet-class>
tag
Here's how it looks under web.xml : (Source)
<servlet> <servlet-name>ServletConfigTest</servlet-name> <servlet-class>com.stackoverflow.ServletConfigTest</servlet-class> <init-param> <param-name>topic</param-name> <param-value>Difference between ServletConfig and ServletContext</param-value> </init-param> </servlet>
ServletContext
ServletContext
available in javax.servlet.*;
package
ServletContext
object is global to entire web application
Object of ServletContext
will be created at the time of web application deployment
Scope: As long as web application is executing, ServletContext
object will be available, and it will be destroyed once the application is removed from the server.
ServletContext
object will be available even before giving the first request In web.xml
– <context-param>
tag will be appear under <web-app>
tag
Here's how it looks under web.xml :
<context-param> <param-name>globalVariable</param-name> <param-value>com.stackoverflow</param-value> </context-param>
So finally…….
No. of web applications = That many number of ServletContext
objects [ 1 per web application ]
No. of servlet classes = That many number of ServletConfig
objects
Difference between ServletContext and ServletConfig in Servlets JSP in tabular format(Source)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With