ServletContext

ServletContext interface is used to provide the configuration information per web application explicitly. Thus unlike ServletConfig, ServletContext interface can be used to provide information to more than one servlet of web application from web.xml file.

So if we want to pass the same information to all servlets of a web application then passing it from web.xml file at once is much efficient way than providing the same information in each servlet separately.

For sharing information from web.xml we need to pass the name in <param-name> and value in <param-value> tags. <context-param> contains these tags.

Methods  of ServletContext interface

              Methods

           Description

Object getAttribute(String name)

This method returns the attribute of specific name.

Enumeration getAttribute()

This method returns the enumeration of attributes names.

String getInitParameter(String name)

This method returns the parameter of name in the form of string.

String getServletInfo()

This method returns the information about servlet container such as its name and version.

void removeAttribute(String name)

We can remove the attributes of name using this method.

Example of ServletContext interface

In this example, two java servlet classes are taken. Both classes contains different information of students of same college. So instead of providing college name every time we are sharing this information from web.xml file.

DemoServletContext.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DemoServletContext extends HttpServlet
{
 public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
 {
         res.setContentType("text/html");
         PrintWriter pw=res.getWriter();
         ServletContext con=getServletContext();
         String st=con.getInitParameter("College");
         pw.println("<html><body>");
         pw.println("<h1>Name:Anuj</h1>");
         pw.println("<h1>Roll number:101</h1>");
         pw.println("<h1>College:"+st+"</h1>");
         pw.println("</body></html>");
   }
}

DemoServletContext1.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DemoServletContext1 extends HttpServlet
{
 public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
 {
         res.setContentType("text/html");
         PrintWriter pw=res.getWriter();    
         ServletContext con=getServletContext();
         String st=con.getInitParameter("College");     
         pw.println("<html><body>");
         pw.println("<h1>Name:Abhishek</h1>");
         pw.println("<h1>Roll number:102</h1>");
         pw.println("<h1>College:"+st+"</h1>");
         pw.println("</body></html>");   
 }
}

Web.xml

 <servlet>
    <servlet-name>DemoServletContext</servlet-name>
    <servlet-class>DemoServletContext</servlet-class>
  </servlet> 
   <servlet>
    <servlet-name>DemoServletContext1</servlet-name>
    <servlet-class>DemoServletContext1</servlet-class>
  </servlet> 
  <context-param>
    <param-name>College</param-name>
    <param-value>Bharat Institute of Technology</param-value>
  </context-param> 
  <servlet-mapping>
    <servlet-name>DemoServletContext1</servlet-name>
    <url-pattern>/Demo1</url-pattern>
  </servlet-mapping> 
   <servlet-mapping>
    <servlet-name>DemoServletContext</servlet-name>
    <url-pattern>/Demo</url-pattern>
  </servlet-mapping>
Output: