ServletConfig and ServletContext

Tomcat web container uses two important concepts in the deployment descriptor (i.e. web.xml) related to the web application development. They are namely: ServletConfig and ServletContext.

ServletConfig is one for each Servlet or JSP.  While, ServletContext is one for each web application.

The Servlet init parameters (i.e. elements) are placed within the element for each Servlet or JSP.

The Servlet context parameters (i.e. elements) are placed within the element but not within a specific element.

We begin with ServletConfig first, followed by ServletContext.

 

ServletConfig

Whenever the web container initializes a Servlet it always reads it deployment descriptor file i.e. web.xml. Container creates name/value pairs for the ServletConfig object.  Once the parameters are in ServletConfig they will never be read again by the container.

The deployment descriptor (web.xml) is given below:

<web-app>
  <servlet>
   <servlet-name>TestInit</servlet-name>
   <servlet-class>InitServlet</servlet-class>
   <init-param>
      <param-name>Email</param-name>
      <param-value>[email protected]</param-value>
   </init-param>
   <init-param>
      <param-name>Address</param-name>
      <param-value>Mumbai</param-value>
   </init-param>
   <init-param>
      <param-name>PhoneNo</param-name>
      <param-value>9830098300</param-value>
   </init-param>
 </servlet>
 <servlet-mapping>
   <servlet-name>TestInit</servlet-name>
   <url-pattern>/init.do</url-pattern>
 </servlet-mapping>
</web-app>

 

The main job of the ServletConfig object is to give the init parameters. To retrieve the init parameters in the program firstly we have made one class named InitServlet. The Servlet code (InitServlet.java) is placed under the TestInitParam\src directory.

The container calls the Servlet’s service() method then depending on the type of request, the service method calls either the doGet() or the doPost(). By default it will be doGet() method. Now inside the processRequest() method we use getWriter() method of the response object which will return an object of the PrintWriter class which helps us to print the content on the browser.

To retrieve all the values of the init parameter we use the method getInitParameterNames() which will return the java.util.Enumeration of the init parameters.

 

The Servlet code is given below:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class InitServlet extends HttpServlet {

public void processRequest(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
  
PrintWriter pw = response.getWriter();
   pw.print("Init Parameters are : ");
   Enumeration enumeration = getServletConfig().getInitParameterNames();
   while(enumeration.hasMoreElements()){
     pw.print(enumeration.nextElement() + " ");
   }
  
pw.println("\nThe email address is = " + getServletConfig().getInitParameter("Email"));
   pw.println("The address is = " + getServletConfig().getInitParameter("Address"));
   pw.println("The phone no is = " + getServletConfig().getInitParameter("PhoneNo"));
}

//for "HTTP GET" method
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

//for "HTTP POST" method
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}

 

The output of the program is given below: 

 

 

ServletContext

ServletContext is an interface which helps us to communicate with the Servlet container. There is only one ServletContext for the entire web application and the components of the web application can share it.

The information in the ServletContext will be common to all the components. Remember that each Servlet will have its own ServletConfig. The ServetContext is created by the container when the web application is deployed and after that only the context is available to each servlet in the web application.

The steps for web application initialization:

  1. First of all the web container reads the deployment descriptor file and then creates a name/value pair for each <context-param> tag.
  2. After creating the name/value pair it creates a new instance of ServletContext.
  3. It’s the responsibility of the Container to give the reference of the ServletContext to the context init parameters.
  4. The Servlet and JSP which are part of the same web application can have the access of the ServletContext.

The Context init parameters are available to the entire web application not just to the single servlet like servlet init parameters.

To retrieve the context parameters in the program firstly we have made one class named ContextServlet. The Servlet code (ContextServlet.java) is placed under the TestContextParam\src directory.

The deployment descriptor (web.xml) is given below:

<web-app>
    <servlet>
     <servlet-name>TestContext</servlet-name>
      <servlet-class>ContextServlet</servlet-class>
   </servlet>
<servlet-mapping>
  <servlet-name>TestContext</servlet-name>
  <url-pattern>/context.do</url-pattern>
</servlet-mapping>
<context-param>
     <param-name>Admin</param-name>
    <param-value>[email protected]</param-value>
</context-param>
</web-app>

 

The code of the program is given below: 

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class ContextServlet extends HttpServlet {

public void processRequest(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException   {

   PrintWriter pw = response.getWriter();
    pw.print("The admin email address is = ");
    pw.println(getServletContext().getInitParameter("Admin"));
  }

//for "HTTP GET" method
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

//for "HTTP POST" method
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

}

 

The output of the program is given below:

 

 

 

 

♦ ServletConfig vs. ServletContext

 ServletConfig
 ServletContext

(1) ServletConfig is one for each Servlet or JSP.

(1) ServletContext is one for each web application.

(2) The Servlet init parameters (i.e. <init-param> elements) are placed within the <servlet> element for each Servlet or JSP.

(2) The Servlet context parameters (i.e. <context-param> elements) are placed within the <web-app> element but not within a specific <servlet> element.

(3) The code for accessing Servlet init parameter is:

getServletConfig().getInitParameter(“Email”)

(3) The code for accessing Servlet context parameter is:

getServletContext().getInitParameter(“Email”)