This section explains you the differences between ServletConfig and ServletContext.
This section explains you the differences between ServletConfig and ServletContext.Here we will see how the ServletConfig And ServletContext are different from each other. Most of the developer are confused with these two topics. Often they couldn't decide what they should use from them. Both of these are the interfaces of Servlet API, that are used by the web developer who develops web sites using J2EE. Both of these are the configuration object that the Servlet container uses them for initializing the various web application's parameters.
ServletConfig :
javax.servlet.ServletConfig is an interface that declares some methods to find out the name-value parameter names, values, servlet name, servlet context. Servlet container uses the Servlet configuration object to pass information to a Servlet on initialization. In the configuration information name-value pairs called, initialization parameters are contained.
Methods of ServletConfig
ServletContext :
javax.servlet.ServletContext is an interface that declares methods and fields to communicate with components in a web application by Servlet container. ServletContext is one for one web application in a particular JVM. ServletContext object can be referred by the all components in a particular web application. The ServletContext object can also be used for inter-application communication. For reading more in detail about the ServletContext click here.
Basically, ServletConfig and ServletContext are differentiate from each other on the basis of scope and availability.
Following code snippet will demonstrate you the difference between ServletContext and ServletConfig
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>servletContext</display-name>
<servlet>
<servlet-name>contextExample</servlet-name>
<servlet-class>net.roseindia.ServletContextExample</servlet-class>
<init-param>
<param-name>servletconfig</param-name>
<param-value>ServletConfig Example</param-value>
</init-param>
</servlet>
<context-param>
<param-name>name</param-name>
<param-value>Deepak</param-value>
</context-param>
<context-param>
<param-name>age</param-name>
<param-value>26</param-value>
</context-param>
<context-param>
<param-name>email</param-name>
<param-value>[email protected]</param-value>
</context-param>
<context-param>
<param-name>mobile</param-name>
<param-value>123456789</param-value>
</context-param>
<servlet-mapping>
<servlet-name>contextExample</servlet-name>
<url-pattern>/ServletContextExample</url-pattern>
</servlet-mapping>
</web-app>
Ads