Java Servlet ServletConfig vs ServletContext Example (original) (raw)

Servlets are modules of the Java code that run in a server application to answer the client requests. ServletContext and ServletConfig are two important interfaces of the Servlet API which is used by Java J2EE programmer during the web application development. In this tutorial, we will see how the ServletContext and ServletConfig are different from each other.

1. Introduction

The correct understanding of ServletContext and ServletConfig is very important for any J2EE application developer. Apart from this, the difference between the ServletContext and ServletConfig is a popular Servlet JSP interview question.

Both ServletContext and ServletConfig are basically the configuration objects which are used by the servlet container to initialize the various parameter of a web application. But they have some difference in terms of scope and availability. Let’s see what ServletContext and ServletConfig objects are and then we will see the differences between them.

1.1 What is ServletConfig?

ServletConfig is an interface in the Servlet API and is used to initialize a single servlet in a web application by the servlet container. Inside deployment descriptor known as web.xml, developers define the servlet initialization parameter related to that servlet. The particulars are declared in the <init-param /> tag in a name-value pair. Following is the signature of the ServletConfig interface:

public interface ServletConfig

By using ServletConfig and a combination of <init-param /> developers can configure any Servlet in a J2EE environment.

Fig. 1: ServletConfig Workflow

Fig. 1: ServletConfig Workflow

1.1.1 Some points to be known with ServletConfig Interface

1.1.2 Example Code

Let’s see the simple code snippet that follows the ServletConfig implementation.

ServletConfigTest com.jcg.servlet.ServletConfigTest topicName Difference between ServletConfig and ServletContext

In servlet, we get this parameter value by the getInitParameter("param-name") method i.e.

String name = getServletConfig().getInitParameter("topicName");

1.2 What is ServletContext?

ServletContext is even more important than the ServletConfig and its one per web application. This object is common for all the servlet and developers use this object to get the detail of the whole web application or the execution environment. An important thing to remember is that the ServletContext represents a web application in a single JVM. Following is the signature of the ServletContext interface:

public interface ServletContext

By using the ServletContext object developers can share the objects to any Servlet or JSP in the whole web application.

Fig. 2: ServletContext Workflow

Fig. 2: ServletContext Workflow

1.2.1 Some points to be known with ServletContext Interface

1.2.2 Example Code

Let’s see the simple code snippet that follows the ServletContext implementation.

globalVariable javacodegeek.com

In servlet, we get this parameter value by the getInitParameter("param-name") method i.e.

String value = getServletContext().getInitParameter("globalVariable");

2. Difference between ServletContext vs. ServletConfig

Now let’s see the difference between the ServletContext and ServletConfig in Servlets JSP in the tabular format:

ServletConfig ServletContext
1. ServletConfig object represent a single servlet ServletContext object represent the whole application running on a particular JVM and is common for all the servlets
2. It is like a local parameter associated with the particular servlet It is like a global parameter associated with the whole application and is shareable by all servlets
3. It is a name-value pair defined inside the servlet section of the web.xml file and it has a servlet wide scope ServletContext has an application-wide scope and is defined outside of the servlet tag
4. getServletConfig() method is used to get the ServletConfig object getServletContext() method is used to get the ServletContext object
5. For e.g.: Shopping cart of a user is a specific to a particular user and developers can use the getServletConfig() object To get the MIME type of a file or an application, the session related information is stored using the ServletContext object
6. Each Servlet comes with a separate ServletConfig object There will be only one ServletContext object available and accessible by all servlets

2.1 Sample Code Snippet

Following code snippet will demonstrate the difference between the ServletContext and ServletConfig object.

web.xml

ServletContext vs. ServletConfig Demo com.jcg.servlet.Demo servletconfig ServletConfig Example name Java Code Geek age 23 email support@javacodegeek.com mobile 0123456789 Demo /

That’s all for this post. The most important difference to remember is that ServletContext is per web application while the ServletConfig is per servlet basis.

Happy Learning!!

3. Conclusion

In this section, developers learned how the ServletContext and ServletConfig are different from each other. Developers can download the code snippet as an Eclipse project in the Downloads section. I hope this article served you with whatever developers were looking for.

4. Download the Eclipse Project

This was an example of the difference between the ServletContext and ServletConfig object.

Photo of Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).