Difference Between ServletConfig And ServletContext

This section explains you the differences between ServletConfig and ServletContext.

Difference Between ServletConfig And ServletContext

Difference 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

  • String getInitParameter(String name) : This method is used to find out the value of the parameter associated with the specified parameter name.
  • Enumeration getInitParameterNames() : This method is used to find out the parameters names initialized in the web.xml file.
  • ServletContext getServletContext() : This method is used to get the ServletContext reference in which the Servlet is executing.
  • String getServletName() : This method is used to get the name of current Servlet.

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.

  • In a single web application on a particular JVM ServletConfig object refers to a single Servlet whereas, the ServletContext object is referred as common to all Servlet and other components.
  • ServletConfig object parameters are like the local parameter whereas, ServletContext object parameters are like the global parameter.
  • ServletConfig name value parameters are defined inside the <servlet></servlet> tag in web.xml file whereas, the ServletContext name-value parameters are defined outside the Servlet tag but, inside the <context-param></context-param> tag.
  • ServletConfig object can be get using getServletConfig() method whereas, ServletContext object can be get using getServletConfig().getServletContext() or getServletContext() method.

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>