ServletConfig interface

When the Web Container initializes a servlet, it creates a ServletConfig object for the servlet. ServletConfig object is used to pass information to a servlet during initialization by getting configuration information from web.xml(Deployment Descriptor).

Main Points:
  • ServletConfig is one of the pre-defined interface.
  • ServletConfig object is used for developing flexible servlets.
  • ServletConfig objct exist one per servlet program.
  • An object of ServletConfig created by the container during its initialization phase.
  • An object of ServletConfig is available to the servlet during its execution, once the servlet execution is completed, automatically ServletConfig interface object will be removed by the container.
  • An object of ServletConfig interface contains <init-param> details at web.xml, of a particular servlet.
  • The moment when we are using an object of ServletConfig, we need to configure the web.xml by writing <init-param> tag under <servlet> tag of web.xml.
  • When ever compiler executes init() mehod then the ServletConfig will be created in general.
  • An object of ServletConfig contain the <init-param> data in the form of key,value pairs, here the keys represents init param names and values are its values, which are represented in the web.xml file

Advantage of ServletConfig


The core advantage of ServletConfig is that you don't need to edit the servlet file if information is modified from the web.xml file.

Methods of ServletConfig interface


  1. public String getInitParameter(String name):Returns the parameter value for the specified parameter name.
  2. public Enumeration getInitParameterNames():Returns an enumeration of all the initialization parameter names.
  3. public String getServletName():Returns the name of the servlet.
  4. public ServletContext getServletContext():Returns an object of ServletContext.

Syntax:
public ServletConfig getServletConfig();
Example of getServletConfig() method
ServletConfig config=getServletConfig();

How to Initialize a Servlet inside web.xml

In the Deployment Descriptor(web.xml) file:
servletconfig
Inside the Servlet class, using following code

 ServletConfig sc = getServletConfig();

 out.println(sc.getInitParameter("email"));

			     

Example ServletConfig

Web.xml

			     <web-app...>

   <servlet>

      <servlet-name>check</servlet-name>

      <servlet-class>MyServlet</servlet-class>

      <init-param>

            <param-name>email</param-name>

            <param-value>admin@javatportal.com</param-value>

      </init-param>

    </servlet>

    <servlet-mapping>

        <servlet-name>check</servlet-name>

        <url-pattern>/check</url-pattern>

    </servlet-mapping>

    <welcome-file-list>

        <welcome-file>index.html</welcome-file>

     </welcome-file-list>   

</web-app>

			     

			     

MyServlet.java

  import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class MyServlet extends HttpServlet {



  protected void doGet(HttpServletRequest request, HttpServletResponse response)

         throws ServletException, IOException {

       

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        ServletConfig sc=getServletConfig();

        out.println(sc.getInitParameter("email")); 

    }

}