ServletConfig
ServletConfig is implemented by the servlet container to initialize a single servlet using init(). you can pass initialization parameters to the servlet using the web.xml deployment descriptor. Every servlet has its own ServletConfig object.
Example code:
<servlet> <servlet-name>ServletConfigTest</servlet-name> <servlet-class>com.shivasoft.ServletConfigTest</servlet-class> <init-param> <param-name>article</param-name> <param-value>Difference between servletContext and ServletConfig</param-value> </init-param> </servlet>
ServletContext
ServletContext is implemented by the servlet container for all servlet to communicate with its servlet container. It is applicable only within a single Java Virtual Machine.
The ServletContext object is contained within the ServletConfig object. That is, the ServletContext can be accessed using the ServletConfig object within a servlet. You can specify param-value pairs for ServletContext object in <context-param> tags in web.xml file. ServletContext is created at the application level and shared by all the Servlet codes.
Example : Database string used by the application.
<context-param> <param-name>dbString</param-name> <param-value>Provider=MySQLProv;Data Source=shivasoft;User Id=shiva; Password=soft;</param-value> </context-param>
Leave a Reply