ServletContext Interface

ServletContext Interface in Java Web Application

In this article, I am going to discuss ServletContext Interface in Java Web Application. Please read our previous article where we discussed ServletConfig Interface. At the end of this article, you will understand the following pointers in detail.

  1. ServletContext Interface
  2. Advantage of ServletContext Interface
  3. Usage of ServletContext Object
  4. How to get the object of ServletContext?
  5. Methods of ServletContext
  6. What is ForeignContext?
  7. Example of ServletContext
ServletContext Interface in Java

ServletContext is an object, it will manage all the context details of a particular web application, where the context details include the logical name of the web application and context parameters, and so on. ServletConetxt is an object, it will provide a complete view of a particular web application. ServletContext object will be prepared by the container the moment when we start the server i.e. the time when we deploy the web application. ServletContext object will be destroyed by the container when we shut down the server i.e. the time when we un-deploy the web application. Due to the above reasons, the life of the ServletContext object is almost all the life of the respective web application. If we declare any data in the ServletContext object then that data will be shared with all the number of resources that are available in the present web application. Due to the above reason, ServletContext will provide more shareability. In web applications, the container will prepare ServletContext object irrespective of getting requests from the client. In the web application, ServletContext will allow both parameters and attributes data. Due to the above reason, ServletContext will allow both Static Inclusion and Dynamic Inclusion of data.

ServletContext Interface in Java Web Application

ServletContext objects can be created and removed only by the server, not by the programmer. ServletContext object is created at the time of deploying the project. ServletContext object will be removed by the server when we un-deploy the project. One servletContext object is created from one project. The ServletContext object can be used by all the servlets which belong to the particular project. But ServletContext object of one project cannot be used by a servlet which belongs to another project.

Advantage of ServletContext Interface

It is easy to maintain. If any information is shared with all or any the servlet, it’s better to form it available for all the servlets. We provide this information from the online .xml file, so if the knowledge is modified, we don’t get to modify the servlet. Thus, it removes the maintenance problems.

Usage of ServletContext Object
  1. It provides an interface between the container and the servlet.
  2. It is used to get configuration information from the web.xml file.
  3. It is used to set, get, or remove attributes from the web.xml file.
  4. It is used to provide inter-application communication.
How to get the object of ServletContext?

To get the ServletContext object we have to use the following method from ServletConfig:
public ServletContext getServletContext();
Example: ServletContext context = config.getServletContext();

Note: In servlet 3.0 version, it is possible to get ServletContext object even from ServletRequest.
Example: ServletContext context = req.getServletContext();
If we want to get the logical name of the web application from the ServletContext object first of all we have to declare it in the web.xml file. To declare a logical name in the web.xml file we have to use the following XML tag:
<web-app>
……………….
<display-name>logical_name</display-name>
……………….
</web-app>

To get the logical name of web application from ServletContext object we will use the following method:
public String getServletContextName()
Example: String Iname=context.getServletContextName();

If we want to provide context parameters on ServletContext object first we have to declare them in web.xml file. To declare a context parameter in web.xml file we have to use the following XML files.
<web-app>
……………….
<context-param>
<param-name>name</param-name>
<param-value>value</param-value>
</context-param>
……………….
</web-app>

When we start the server or at the time of application deployment the main job of the container is to recognize the web application and to prepare the ServletContext object.

At the time of recognizing the application container will recognize the web.xml file then perform web.xml file loading, parsing, and reading the content. While reading the content container will read all its context parameters and store them in the ServletContext object at the time of creation.

Methods of ServletContext
  1. public String getInitParameter(String name): It is used to get the particular context parameter value from ServletContext.
  2. public Enumeration getInitParameterNames(): It is used to get all the context parameter names from ServletContext object.
  3. public void setAttribute(String name, Object value): In the web application, the ServletContext object is able to allow attributes. To set an attribute on the ServletContext object we will use this method.
  4. public Object getAttribute(String name): It is used to get an attribute from the ServletContext object.
  5. public void removeAttribute(String name): It is used to remove an attribute from the ServletContext object.
  6. public Enumeration getAttributeNames(): It is used to get all the names of attributes from the ServletContext object.
What is ForeignContext?

ForeignContext is a ServletContext object of another web application being executed in the same server. To get the ForeignContext object we have to use the following method:

public ServletContext getContext(String path)

Example of ServletContext

In this example, we are getting all the initialization parameters from the web.xml file and printing the value of the initialization parameter. For getting all the parameters, we have used the getInitParameterNames() method in the servlet class.

web.xml
<web-app>
 <display-name>Context Application</display-name>
 <context-param>
  <param-name>a</param-name>
  <param-value>apple</param-value>
 </context-param>
 <context-param>
  <param-name>b</param-name>
  <param-value>bombay</param-value>
 </context-param>
 <servlet>
  <servlet-name>MyServlet</servlet-name>
  <servlet-class>MyServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>MyServlet</servlet-name>
  <url-pattern>/context</url-pattern>
 </servlet-mapping>
</web-app>
MyServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet
{
    protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType ("text/html");
        PrintWriter out = response.getWriter ();

        ServletContext context = getServletConfig ().getServletContext ();
        String logicalName = context.getServletContextName ();
        String a = context.getInitParameter ("a");
        String b = context.getInitParameter ("b");
        Enumeration e = context.getInitParameterNames ();
        context.setAttribute ("c", "cat");
        context.setAttribute ("d", "dog");
        out.println ("<html><body><h1><br>");
        out.println ("Logical Name : " + logicalName);
        out.println ("<br>");
        out.println ("a for ... " + a);
        out.println ("<br>");
        out.println ("b for ... " + b);
        out.println ("<br>");
        while (e.hasMoreElements ())
        {
         out.println (e.nextElement () + "<br>");
        }
        out.println ("c for ... " + context.getAttribute ("c"));
        out.println ("<br>");
        out.println ("d for ... " + context.getAttribute ("d") + "<br>");
        e = context.getAttributeNames ();
        while (e.hasMoreElements ())
        {
         out.println ("</h1></body></html>");
        }
    }
}

Output

ServletContext Interface

In the next article, I am going to discuss Attribute in Servlet. Here, in this article, I try to explain ServletContext Interface in Java. I hope you enjoy this ServletContext Interface in Java article.

Leave a Reply

Your email address will not be published.