Servletconfig and Servletcontext Example in Java

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 ofServletContext 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

  • For each Servlet under execution, a ServletConfig object is created by the servlet container and is used by the programmer to read the servlet specific data declared in the web.xml
  • ServletConfig is an interface from the javax.servlet package
  • The getServletConfig() method of the GenericServlet class returns an object of the ServletConfig
  • The ServletConfig object created by the container for a specific servlet cannot read the other servlet's <init-param /> data
  • The ServletConfig object is specific for a Servlet. Another way to say is that, if 100 servlet objects are being executed in the container, 100 ServletConfig objects are also created implicitly for communication with the Servlet
  • When the container destroys the Servlet object, the corresponding ServletConfig object is also destroyed
  • The programmer can make use of the ServletConfig object to read tags from the Servlet
  • The data in the <init-param /> tag is known as initialization parameter

1.1.2 Example Code

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

<servlet>  <servlet-name>ServletConfigTest</servlet-name>  <servlet-class>com.jcg.servlet.ServletConfigTest</servlet-class>  <init-param>   <param-name>topicName</param-name>   <param-value>Difference between ServletConfig and ServletContext</param-value>  </init-param> </servlet>        

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

  • ServletContext is an interface from the javax.servlet package and its object can be obtained from the getServletContext() method of the ServletContext i.e.
    ServletConfig configObj = getServletConfig(); ServletContext contextObj = configObj.getServletContext();            
  • The ServletContext object comes into existence when the container is started and is destroyed when the container stops its execution
  • The ServletContext object is common to all servlets and other components
  • ServletContext parameters are defined outside the Servlet tag but, inside the <context-param /> tag

1.2.2 Example Code

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

<context-param>     <param-name>globalVariable</param-name>     <param-value>javacodegeek.com</param-value> </context-param>        

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

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" 	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 	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 vs. ServletConfig</display-name> 	<servlet> 		<servlet-name>Demo</servlet-name> 		<servlet-class>com.jcg.servlet.Demo</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>Java Code Geek</param-value> 	</context-param> 	<context-param> 		<param-name>age</param-name> 		<param-value>23</param-value> 	</context-param> 	<context-param> 		<param-name>email</param-name> 		<param-value>support@javacodegeek.com</param-value> 	</context-param> 	<context-param> 		<param-name>mobile</param-name> 		<param-value>0123456789</param-value> 	</context-param> 	<servlet-mapping> 		<servlet-name>Demo</servlet-name> 		<url-pattern>/</url-pattern> 	</servlet-mapping> </web-app>        

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.

hyderexamated.blogspot.com

Source: https://examples.javacodegeeks.com/enterprise-java/servlet/java-servlet-servletconfig-vs-servletcontext-example/

0 Response to "Servletconfig and Servletcontext Example in Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel