Tuesday, March 24, 2020

ServletConfig interface

ServletConfig interface


ServletConfig is an interface from javax.servlet package. An object of ServletConfig interface is manufactured by the Servlet container for each servlet available in a web application. We can use this object to fetch configuration information from the web.xml file.

The difference between ServletContext and the ServletContfig is that there is only one object of ServletContext created once per web application. It means a ServletConfig object is unique to all the servlets in web applications.

web.xml


An object of ServletConfig interface has <init-param> information stored web.xml file, of a particular servlet. This ServletConfig object has the <init-param> data as key-value pairs. The keys denote init parameter names and values are their respective values, stored in the web.xml file. The <init-param> tag information also known as the initialization parameter. This is used to initialize the Servlet with some other properties. The web.xml is known as a deployment descriptor. The deployment information of a web application is stored in XML format. The web.xml also include initialization (<init-param>) parameters.

The ServletConfig object persists during the servlet execution, However, once the servlet execution is completed, the ServletConfig object will be destroyed by the container.

If the parameter values are updated in the web.xml file, The Servlet does not require to be re-compiled. At the time reading, whatever values persist in the deployment descriptor, only those values or parameters of the Servlet is would be used to create ServletConfig Object by the web container.

Advantage of ServletConfig


The biggest advantage of ServletConfig is that there is no need to edit the servlet itself if the information is updated in the web.xml file.

A ServletConfig object belongs to a particular Servlet. If a web application is having n Servlets than n ServletConfig objects would be created during the time of execution by the web container.

ServletConfig interface methods


Method Description Return type
getInitParameter(String name) Returns the parameter value for the specified parameter name. String
getInitParameterNames() Returns an enumeration of all the initialization parameter names Enumeration
getServletName() Returns the name of the servlet String
getServletContext() Returns an object of ServletContext ServletContext


ServletConfig Interface Example


Add  <init-param> entries to the web.xml,



<?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">
  <display-name>LoginServlet</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!--ADD THIS CODE  -->
  <servlet> 
<servlet-name>DemoServletConfig</servlet-name> 
<servlet-class>DemoServletConfig</servlet-class> 
 
<init-param> 
<param-name>MESSAGE</param-name> 
<param-value>Welcome to Servlets</param-value> 
</init-param> 
 
</servlet>
<!-- -->
</web-app>

Create DemoServletConfig servlet class




import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
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("/DemoServletConfig")
public class DemoServletConfig extends HttpServlet {
     private static final long serialVersionUID = 1L;
      
    public DemoServletConfig() {
        super();
    }

     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

           response.setContentType("text/html"); 
         PrintWriter pout = response.getWriter(); 
          
         ServletConfig config=getServletConfig(); 
         String driver=config.getInitParameter("MESSAGE"); 
         pout.print("<h2>from ProgrammingHunk</h2> "+driver); 
              
         pout.close();   
     }

}


Result


output