ServletConfig in Servlet

19 Nov, 2021
javax.servlet.ServletConfig is an interface as a part of servlet API. For every Servlet class in our application, the web container will create one ServletConfig object and the web container will pass this object as an argument to the public void init(ServletConfig config) method of our Servlet class object. Some of the important points on ServletConfig are:
ServletConfig is an object containing some initial parameters or configuration information created by Servlet Container and passed to the servlet during initialization.
ServletConfig is for a particular servlet, which means one should store servlet-specific information in web.xml and retrieve them using this object.
Suppose, one is building a job portal and desires to share different email ids (which may get changed over time) with to recruiter and job applicant. So, he decides to write two servlets one for handling the recruiter’s request and another one for the job applicant.
Where to store email-ids?
Put email-id as a name-value pair for different servlet inside web.xml which can further be retrieved using getServletConfig().getInitParameter(“name”) in the servlet.
There are 4 Methods in the ServletConfig interface
public abstract java.lang.String getServletName()public abstract javax.servlet.ServletContext getServletContext()public abstract java.lang.String getInitParameter(java.lang.String)public abstract java.util.Enumeration<java.lang.String> getInitParameterNames()
public abstract java.lang.String getServletName()
public abstract javax.servlet.ServletContext getServletContext()
public abstract java.lang.String getInitParameter(java.lang.String)
public abstract java.util.Enumeration<java.lang.String> getInitParameterNames()
1. public abstract java.lang.String getServletName()
While configuring Servlet in web.xml we have to give a logical name to our servlet. Let’s say we have a Servlet class TestServlet and we want to configure it in web.xml.
XML
<web-app>    <servlet>        <servlet-name>Test</servlet-name>        <servlet-class>TestServlet</servlet-class>    </servlet>      <servlet-mapping>        <servlet-name>Test</servlet-name>        <url-pattern>/test</url-pattern>    </servlet-mapping></web-app>
In <servlet-name> we have given a logical name for our TestServlet class. So if we use the method getServletName() it will return the logical name of Servlet and in this case, it will return “Test”.
2. public abstract javax.servlet.ServletContext getServletContext()
This method will simply return ServletContext Object. Web container creates one ServletContext object for every web application.
3. public abstract java.lang.String getInitParameter(java.lang.String)
We can store init parameters as a part of web.xml
XML
<web-app>   <servlet>       <servlet-name>Test</servlet-name>       <servlet-class>TestServlet</servlet-class>       <init-param>           <param-name>username</param-name>           <param-value>xyz</param-value>       </init-param> <init-param>           <param-name>password</param-name>           <param-value>welcome@123</param-value>       </init-param>   </servlet>     <servlet-mapping>       <servlet-name>Test</servlet-name>       <url-apptern>/test</url-apptern>   </servlet-mapping></web-app>
The advantage of using init parameters is we do not need to hard code values in source code and if we are not hard coding values in source code, to change values we don’t need to touch source code. We just need to change values in web.xml and re-deploy the application. These init parameters are specific to a Servlet for which you have configured them. By using getInitParameter() we can access the value of init parameters in our Servlet.
Java
import javax.servlet.Servlet;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.ServletConfig;  public class TestServlet implements Servlet{    private ServletConfig config;     // web container will call this        // method by passing ServletConfig     public void init(ServletConfig config){        this.config=config;    }      public void service(ServletRequest request, ServletResponse response){        // pass <param-name> to get <param-value>        String username=config.getInitParameter("username"); // xyz        String password=config.getInitParameter("password"); // welcome@123        System.out.println(username);        System.out.println(password);      }      public void destroy(){      }      public ServletConfig getServletConfig(){        return config;    }      public String getServletInfo(){        return this.getClass().getName();    }  }
4. public abstract java.util.Enumeration<java.lang.String> getInitParameterNames()
This method will return Enumeration having names of all init parameter names.
java-servlet
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
List Interface in Java with Examples
Strings in Java
Different ways of Reading a text file in Java
Reverse an array in Java
How to remove an element from ArrayList in Java?
Checked vs Unchecked Exceptions in Java
ArrayList get(index) Method in Java with Examples
ArrayList to Array Conversion in Java : toArray() Methods
Comparator Interface in Java with Examples
Exceptions in Java