Tuesday, March 17, 2020

ServletContext interface Java Servlets

The ServletContext


The ServletContext interface provides a set of methods that a servlet can use to communicate with a web container. A Servlet can ask to dispatch requests, write a log file or get the MIME type of any file ServletContext interface.

The ServletConfig and the ServletContext are very much similar except, that there is only one object of ServletContext created once per web application. It means a ServletContext instance is common among all the servlets in web applications.

An instance of ServletContext is created by the servlet container at the time of deploying the project. This instance is used to get configuration data from the deployment descriptor (web.xml). A web application can have only one ServletContext instance.

The advantage of the ServletContext interface

To share data among many servlets, we can provide this data in the web.xml file using the <context-param> element, so is the data is changed we would not require to update the servlets. This will remove the maintenance problem.

The ServletContext instance is kept within the ServletConfig object. The web server creates the ServletConfig object when the servlet is initialized.

ServletContext interface methods

Few most common methods provided by the ServletContext interface are,

Method

Description

public String getInitParameter(String parameter) This method returns the value of the given parameter or null if the parameter is not available
public Enumeration getInitParameterNames() This method returns an enumeration of context parameter names
public void setAttribute(String name,Object obj) This method sets the attribute value to attribute name
public String getContextPath() This method returns the context path of this web application
public Object getAttribute(String name) This method returns the attribute value for the given name or null if the attribute is not available
public String getServletInfo() The name and version of the servlet container on which the servlet is running is returned by this method

ServletContext Example


Add parameters(<context-param> to web.xml



t
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  *
  *
  <context-param> 
    <param-name>name</param-name> 
    <param-value>pushpendra</param-value> 
  </context-param>
<context-param>
   <param-name>org</param-name> 
   <param-value>ProgrammingHunk</param-value> 
  </context-param>
  *
  *
 
</web-app>

Write your servlet


import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/TestServlet" )
public class TestServlet extends HttpServlet {
     private static final long serialVersionUID = 1L;
ServletContext servletContext=null;
     public TestServlet() {
          
        super();

    }
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     response.setContentType("text/html");
    servletContext=getServletConfig().getServletContext();
     String name=servletContext.getInitParameter("name");
     String org=servletContext.getInitParameter("org");
    String path=servletContext.getContextPath();
     response.getWriter().append("<html><body> This is"+name+" from "+org+"<br>ContextPath:"+path+"</body></html>");
    
     }

}



Output


ServletContext