Open In App

ServletConfig in Servlet

Last Updated : 09 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 the 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 it using this object.

Example

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 decided 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 servlets inside web.xml which can further be retrieved using getServletConfig().getInitParameter(“name”) in the servlet.

Methods in the ServletConfig Interface

There are 4 Methods in the ServletConfig interface

  1. public abstract java.lang.String getServletName()
  2. public abstract javax.servlet.ServletContext getServletContext()
  3. public abstract java.lang.String getInitParameter(java.lang.String)
  4. 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-pattern>/test</url-pattern>
   </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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads