Updated February 18, 2023
Introduction to ServletContext
ServletContext is the object Servlet Container used to share initial values (data) or configuration information with the entire application. It is the set of features to operate web applications using a single object. It works on the object, methods, and interfaces to communicate container data and servlet. It is the initial stage of the project deployment to connect other functionality. It is an interface that has several methods for talking to its servlet container.
Key Takeaways
- Give default information or essential data in the initial stage of the servlet method. It initialized data before starting operation.
- Use the “web.xml” file to get the parameter (context) value to the servlet container.
- Provide configuration of interface and listeners and initialization of the servlet life cycle.
- Communicate with the servlet page and fix data without changing any servlet page.
- Display data using the servlet init parameter in the xml file.
- Uses various methods, interfaces, parameters, and listeners to initialize, display and operate data
What is ServletContext?
The project is deployed, and the web container makes a ServletContext object. You can use this object to get configuration information from the file web.xml. Each web application has only one object.
If there is similar content that requires a servlet, then it is easy to manage all of the servlet containers using ServletContext. The servlet page gets the information from the” web.xml” file. Therefore we do not require changing the servlet if the content or conditions change. So, there is no problem with maintenance.
How to Use ServletContext Interface?
There are various ways to use it, some of them are as follows:
- The object connects the container or jar files and the servlet using the servlet interfaces.
- The web.xml file can be utilized to obtain details about configuration from the ServletContext object.
- You can get, set, or remove required attributes from the web.xml file by using the object.
- The object provides communication between different (inter) applications.
Methods
Some of the interface’s most used methods are listed below.
- public String getInitParameter(String names) method: Show the container’s parameter value of the particular parameter name.
- public function getInitParameterNames() method: Show the context initialization parameters name.
- public void setAttr(String names, Object object_name) method: Set the required object in the servlet application’s scope.
- public Object getAttribute(String input_name) method: Returns the attribute of the given particular name.
- public Enumeration getInitParameterNames() method: Return the initialization parameters name as an Enumeration of the objects.
- public void removeAttribute(String input_name) method: Delete the attribute with the input name from the ServletContext function.
ServletContext of Object
- The object gives the getServletContext() method for the ServletConfig interface.
- The object gives the getServletContext() method for the GenericServlet class.
Syntax:
The following syntax uses to create the ServletContext object.
public ServletContext getServletContext();
The following syntax uses to operate the object.
ServletContext object_name = getServletConfig().getServletContext();
Or
ServletContext object_name = getServletContext();
ServletContext Listener
Listener is informed that the web application initialization process is starting. All Listeners are known as the given context is initialized before any servlets in the web application.
- javax.servlet.ServletContextListener: Interface for getting informed when the ServletContext lifecycle keeps changing.
- javax.servlet.ServletContextAttributeListener: Interface for getting notified when a ServletContext attribute modification.
How to Operate ServletContext Listener?
The servlet page uses the following structure for the ServletContext listeners.
Code:
import javax.servlet.*;
import java.sql.*;
public class MyServletContextDemo implements ServletContextListener{
public void contextInitialized(
ServletContextEvent args) {
//write code here…
}
public void contextDestroyed(
ServletContextEvent args) {
//write code here…
}
}
The web.xml files use the following structure to get data.
Code:
<context-param>
<param-name> Value </param-name>
<param-value>
Technology
</param-value>
</context-param>
Examples
Given below are the examples mentioned:
Example #1
The basic example of the ServletContext initialized parameter.
Filename: SeveletDemoExample.java
Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletDemoExample extends HttpServlet{
public void doGet(HttpServletRequest requests, HttpServletResponse responses)
throws ServletException, IOException
{
responses.setContentType("text/html");
PrintWriter printw = responses.getWriter();
ServletContext context_var = getServletContext();
String driver_names = context_var.getInitParameter("dname");
printw.println("Database driver name is : "+driver_names);
printw.close();
}}
Filename: web.xml
Code:
<web-app>
<servlet>
<servlet-name> educba </servlet-name>
<servlet-class> ServletDemoExample </servlet-class>
</servlet>
<context-param>
<param-name> driver_names </param-name>
<param-value>
sun.jdbc.odbc.JdbcOdbcDriver
</param-value>
</context-param>
<servlet-mapping>
<servlet-name> educba </servlet-name>
<url-pattern> /context_var </url-pattern>
</servlet-mapping>
</web-app>
Output:
Example #2
The basic example of the interface and methods.
Filename: index.html
Code:
<form action = "context_var" method = "post">
<h4> ServletContext Demo Example </h4>
<input type = "submit" value = "Go to Next Page">
</form>
Filename: SeveletDemoExample.java
Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletDemoExample extends HttpServlet{
public void doGet(HttpServletRequest requests, HttpServletResponse responses)
throws ServletException, IOException
{
responses.setContentType("text/html");
PrintWriter printw = responses.getWriter();
ServletContext context_var = getServletContext();
String user_data = context_var.getInitParameter("UserData");
String user_value = context_var.getInitParameter("Value");
printw.println("Learning Center: " + user_data+ " AND Domain: " +Value);
printw.close();
}}
Filename: web.xml
Code:
<web-app>
<servlet>
<servlet-name> educba </servlet-name>
<servlet-class> ServletDemoExample </servlet-class>
</servlet>
<context-param>
<param-name> UserData </param-name>
<param-value>
Educba
</param-value>
</context-param>
<context-param>
<param-name> Value </param-name>
<param-value>
Technology
</param-value>
</context-param>
<servlet-mapping>
<servlet-name> educba </servlet-name>
<url-pattern> /context_var </url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.html
</welcome-file>
</welcome-file-list>
</web-app>
Output 1:
Output 2:
FAQ
Given below are the FAQs mentioned:
Q1. What is get servlet context?
Answer: getContext returns a ServletContext object that conforms to a given URL. These servlet methods get access to the specific context for different parts or functions of the server and get RequestDispatcher objects from the context.
Q2. What is the use of ServletContext?
Answer: It defines a set of methods that a servlet can use to talk to its servlet container. For example, a servlet can use these methods to find the MIME type of a file, send the information, or write to such a log file. There is only one context per “web service” per JVM (Java Virtual Machine).
Q3. Is a ServletContext thread safe?
Answer: We can tell the Java Servlet Configuration that connecting attributes between servlets with ServletContext setAttribute and getAttribute are required thread-safe.
Conclusion
The ServletContext interface uses to get static data for all servlet files. It helps to communicate with the servlet container and the data in the xml file. It helps to maintain data from one file and does not change for the servlet file.
Recommended Articles
This is a guide to ServletContext. Here we discuss the introduction, how to use ServletContext interface? methods, object, and examples. You may also have a look at the following articles to learn more –