Difference Between ServletConfig and ServletContext

Servlet Container creates both ServletConfig and ServletContext objects either during server startup or during the deployment of the web application based on Servlet configuration done in web.xml file. But there are lots of differences between ServletConfig and ServletContext objects.

ServletConfigServletContext
ServletConfig object is one per Servlet component or class.The ServletContext object is one per web application.
It is accessible only within the configured servlet class.It is visible and accessible in all the servlet/JSP components of the web application, so it is also called the global memory of a web application.
Servlet Container destroys this object right after Servlet class object destruction.Servlet Container destroys this object when the web application is stopped/reloaded/undeployed.
It is the object of the Servlet Container supplied java class that implements jakarta.servlet.ServletConfig(I).It is the object of ServletContainer supplied Java class that implements jakarta.servlet.ServletContext(I).
Can be used to gather init-param <init-param> values from the web.xml file to the Servlet component.Can be used to gather context-param <context-param> values from the web.xml file to the Servlet component.
It can be accessed through getServletConfig() Method. Example:- ServletConfig cg = getServletConfig();It can be accessed through getServletContext() Method. Example:- ServletContext cs = getServletContext();

Init Param vs Context Param

The init-param <init-param> is associated with ServletConfig object whereas context param <context-param> is associated with ServletContext object.

Difference between init param values vs context param values:- The init parameters are specific to one servlet because they are allocating memory in servletConfig object whereas context param values are specific to one web application and visible to all the servlet/jsp component of the web application because they are allocating memory in ServletContext object.

Example of <init-param> in web.xml,

<web-app>
   <servlet>
      <servlet-name>emp</servlet-name>
      <servlet-class>com.kp.servlet.GetEmployee</servlet-class>
      <init-param>
         <param-name>dbuser</param-name>
         <param-value>scott</param-value>
      </init-param>
      <init-param>
         <param-name>dbpass</param-name>
         <param-value>tiger</param-value>
      </init-param>
   </servlet>
   <servlet-mapping>
      <servlet-name>emp</servlet-name>
      <url-pattern>/getemp</url-pattern>
   </servlet-mapping>
</web-app>

The init-param values can be accessed through ServletConfig objects. Example:-

@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) 
     throws ServletException, IOException {
   // get access to ServletConfig object
   ServletConfig cg = getServletConfig();
   // read init-param values
   String user = cg.getInitParameter("dbuser");
   String pwd = cg.getInitParameter("dbpass");
   // use values to get database connection
   System.out.println(user +" "+ pwd);
}

Example of context-param <context-param> in web.xml file,

<web-app>
   <context-param>
      <param-name>URL</param-name>
      <param-value>jdbc:oracle:thin:@localhost:1521:knowprogram</param-value>
   </context-param>
   <context-param>
      <param-name>dbuser</param-name>
      <param-value>scott</param-value>
   </context-param>
   <context-param>
      <param-name>dbpass</param-name>
      <param-value>tiger</param-value>
   </context-param>
</web-app>

The context-param values can be accessed through ServletContext objects. Examples:-

@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) 
     throws ServletException, IOException {
   // get ServletContext
   ServletContext sc = getServletContext();
   // get context params
   String url = sc.getInitParameter("URL");
   String user = sc.getInitParameter("dbuser");
   String pass = sc.getInitParameter("dbpass");
   // use these data to get database connection
}

When we should use init-param vs context-param:- If the technical inputs are required only for 1 servlet component then we should go for init parameters through ServletConfig object, similarly if all servlet components are looking for the same technical inputs then we should go for context params through ServletContext objects.

How ServletConfig and ServletContext objects are created

Both ServletConfig and ServletContext objects are created by the servlet container, but which will be created first, and what is the order? ServletContext object will be created first. When we deploy the web application then the servlet container checks the deployment directory structure and loads the web.xml file to validate, after that it creates the ServletContext object. Later it verifies how many servlet components are configured in the web.xml file and based on that it will create those many ServletConfig objects.

How ServletConfig and ServletContext objects are created by the Servlet Container:- We deploy the web application, Servlet container verifies the deployment directory structure and loads web.xml file. It checks whether web.xml is well-formed and valid or not, if yes then it creates in-memory metadata. Now, the servlet container creates one ServletContext object, checks in-memory metadata to find if there are any context-param values? If yes then take them and store them in the ServletContext object.

Now, the servlet container again checks the servlet configuration in the web.xml file and creates ServletConfig objects. For example:- If two servlet components are configured in the web.xml file then it will create 2 ServletConfig objects. Again checks in-memory metadata to search if there are init param values associated with those servlet classes? If yes then take them from the web.xml file and store them in the ServletConfig object of the related servlet component.

FAQ on ServletConfig and ServletContext

Question-1) In a web server totals 10 web applications deployed, among them 7 web applications are in running mode and 3 web applications are in stopped mode. How many ServletContext objects are currently available?

a) 10
b) 7
c) 3
d) None of these

Answer:- 10-3 = 7; because the ServletContext objects are created either during startup or at the time of deployment of the web application, and it is destroyed when the web application is stopped/reloaded/undeployed.

Question-2) The web server has one web application. In that web application total of 10 Servlet Components are available; in that 3 Servlet Components are already requested and another 3 Servlet Components are enabled with <load-with-startup>.  Now answer the following questions?

2.a) How many ServletContext objects are currently available?

a) 10
b) 3
c) 6
d) 1
e) 4

Answer:- d) 1 (One); because the ServletContext object is one per web application and here web server contains only one web application.

2.b) How many ServletConfig objects are currently available?

a) 10
b) 3
c) 6
d) 1
e) 4

Answer:- a) 10; There are 10 ServletConfig objects available. The ServletConfig object is one per Servlet class object. Here the web application has 10  servlet components/class so it has 10 ServletConfig objects.

Note:- Our Servlet class object, request object, response object, ServletConfig object, ServletContext object are ServletContainer created objects. We can’t create all these objects, but we can access those objects to our Servlet Component from ServletContainer. To access request, and response objects use service(-,-)/doXxx(-,-) method parameters. To access our Servlet class object use “this” operator/keyword.

Question-3) What happens if multiple init params of a servlet are having the same name with different values?

<init-param>
   <param-name>dbuser</param-name>
   <param-value>scott</param-value>
</init-param>
<init-param>
   <param-name>dbuser</param-name>
   <param-value>user1</param-value>
</init-param>
<init-param>
   <param-name>dbuser</param-name>
   <param-value>user2</param-value>
</init-param>

In Servlet,

// get access to ServletConfig object
ServletConfig sc = getServletConfig();
// read init-param values
String user = cg.getInitParameter("dbuser");

Answer:- If multiple init params of a servlet are having the same name with different values then the first value will be considered. In the above situation, “scott” will be taken as init-param value.

Question-4) Can we take the same init param name and values in two different servlet components configurations?

Answer:- Yes. 

Question-5) What happens if multiple context params of a web application have the same name and different values? Which will be considered?

<context-param>
   <param-name>dbuser</param-name>
   <param-value>scott</param-value>
</context-param>
<context-param>
   <param-name>dbuser</param-name>
   <param-value>user1</param-value>
</context-param>

In servlet component,

// get ServletContext
ServletContext sc = getServletContext();
// get context params
String user = sc.getInitParameter("dbuser");

Answer:- If multiple context params of a web application are having the same name with different values then the last value will be considered. In the above situation, “user1” will be taken as a context-param value.

Question-6) Can we give the same name to context-param and servlet init params?

Answer:- Yes. The init param allocates memory in the ServletConfig object, and the context param allocates memory in the ServletContext object. Since both are allocating memory in two different objects therefore both context-param and init params can have the same name.

Q) What is difference between ServletConfig and ServletContext?
The ServletConfig object is one per servlet component but ServletContext object is one per web application. The ServletConfig is an object of the Servlet Container supplied java class that implements jakarta.servlet.ServletConfig(I), whearas ServletContext is an object of ServletContainer supplied Java class that implements jakarta.servlet.ServletContext(I).

Q) What is the use of ServletContext and ServletConfig?
The ServletContext object can be used to gather context-param values whereas the ServletConfig object can be used to gather init-param values from the web.xml file to the Servlet component.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *