ServletConfig Interface:-


During initialization of Servlets, web container creates one ServletConfig for each Servlet. ServletConfig can be used to get servlet information from web.xml file.
We can pass any information of servlet in our web.xml file and can use this information in servlet using ServletConfig.

Creating object of ServletConfig -

We can get a reference (object) to ServletConfig by using getServletConfig() method. As we know our servlets already inherit getServletConfig(), So we can directly use it to get a reference to ServletConfig as follows -
ServletConfig sc = getServletConfig();

ServletConfig methods -

ServletConfig provides the following four methods -
Method name with return type Description
1- public String getInitParameter(String paramName) - Using this method Servlet can access the value of a parameter specified in web.xml. It returns parameter value as String, whose name is specified by paramName in web.xml.
2- public java.util.Enumeration<String> getInitParameterNames() Using this method Servlet can access the names of all parameters specified in web.xml for that particular Servlet. It returns parameter names as Enumeration of String type.
3- public String getServletName()) This method is used to get name of Servlet. By default it returns the class name of Servlet.
4- public ServletContext getServletContext() This method is used to get a reference to ServletContext. ServletContext will be discussed in detail further.

Example -

This example will show you how to use ServletConfig to get all the information specified by you in web.xml for any particular Servlet.

1- index.html or index.jsp This will be your default page.
Add the following code to it -
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h1>ServletConfig Demo</h1>
        <a href="FirstServ">Get Information of First Servlet</a>
        <br>
        <a href="SecondServ">Get Information of Second Servlet</a>
    </body>
</html>     

2- Create a package named servs and within servs create two servlets named FirstServ and SecondServ.
Add the following code in servlets -
FirstServ Servlet -
 
package servs;

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

public class FirstServ extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       try
       {
           PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet FirstServ</title>");            
            out.println("</head>");
            out.println("<body>");
            ServletConfig sc = getServletConfig();
            String name = sc.getInitParameter("UserName");
            String email = sc.getInitParameter("Email");
            
            out.println("User Name is - "+name+"<br>");
            out.println("Email id is - "+email+"<br>");
            
            java.util.Enumeration<String> paramNames = sc.getInitParameterNames();
            
            out.println("Parameter Names are - "+"<br>");
            while(paramNames.hasMoreElements())
            {
                out.println(paramNames.nextElement());
            }
            
            String servlet_name = sc.getServletName();
            
            out.println("<br>");
            out.println("Servlet Name is - "+servlet_name);
            out.println("</body>");
            out.println("</html>");
       }
       catch(Exception ex)
       {
           System.out.println(ex);
       }
    }
}

SecondServ Servlet -

package servs;

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

public class SecondServ extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       try
       {
         PrintWriter out = response.getWriter();
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet SecondServ</title>");            
            out.println("</head>");
            out.println("<body>");
            ServletConfig sc = getServletConfig();
            String name = sc.getInitParameter("UserName");
            String email = sc.getInitParameter("Email");
            
            out.println("User Name is "+name+"<br>");
            out.println("Email id is "+email+"<br>");
            
            java.util.Enumeration<String> paramNames = sc.getInitParameterNames();
            
            out.println("Parameter Names are - "+"<br>");
            while(paramNames.hasMoreElements())
            {
                out.println(paramNames.nextElement());
            }
            
            String servlet_name = sc.getServletName();
            
            out.println("Servlet Name is "+servlet_name+"<br>");
            out.println("</body>");
            out.println("</html>");   
       }
       catch(Exception ex)
       {
           System.out.println(ex);
       }
    }
}

3- web.xml (Deployment Descriptor) - Add the following code to web.xml file -
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
    <servlet>
        <servlet-name>FirstServ</servlet-name>
        <servlet-class>servs.FirstServ</servlet-class>
        <init-param>
            <param-name>UserName</param-name>
            <param-value>Tom</param-value>
        </init-param>
        <init-param>
            <param-name>Email</param-name>
            <param-value>Tom@gettechnotes.com</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>FirstServ</servlet-name>
        <url-pattern>/FirstServ</url-pattern>
    </servlet-mapping>
    
    
    <servlet>
        <servlet-name>SecondServ</servlet-name>
        <servlet-class>servs.SecondServ</servlet-class>
         <init-param>
            <param-name>UserName</param-name>
            <param-value>Jerry</param-value>
        </init-param>
        <init-param>
            <param-name>Email</param-name>
            <param-value>Jerry@gettechnotes.com</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>SecondServ</servlet-name>
        <url-pattern>/SecondServ</url-pattern>
    </servlet-mapping>
   
</web-app>
    
Note - <init-param> shoud be within <servlet> tag.
4- Now run your project and it will show you the following output -



When you will click on the first link, it will show you the configuration information of first servlet -



And when you will click on the second link, it will show you the configuration information of second servlet -