Get Configuration information – ServletConfig

Each servlets application allows you to configure and retrieve parameters via web.xml . You can specify initial parameter name/value pairs using <init-param> tag. In this example we will learn how to retrieve configuration information from the web.xml file.

To retrieve initial parameters, you need the ServletConfig object passed by the servlet container to the servlet. After you get the ServletConfig object, you can use either  getInitParameterNames and strong>getInitParameter methods.

  • The getInitParameterNames does not take an argument and returns an Enumeration containing all the parameter names in the ServletConfig object.
  • The getInitParameter takes a String containing the parameter name and returns a String containing the value of the parameter.

Since the servlet container passes a ServletConfig object to the init method, we will be writing our logic in the init() method itself. Here we will be configuring two initial parameters, the first parameter is  “admin” whose value will be “JavaInterviewPoint” and the second parameter is “admin_contact” and the value for the parameter is “123456789”. We will be implementing Servlet Interface to achieve this.

Lets see the below Servlet Example which implements the Servlet Interface

package com.javainterviewpoint;

import java.util.Enumeration;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class ServletConfigExample implements Servlet 
    public void init(ServletConfig config) throws ServletException 
        Enumeration parameters = config.getInitParameterNames();
        while (parameters.hasMoreElements()) 
            String parameter = (String) parameters.nextElement();
            System.out.println("Parameter name : " + parameter);
            System.out.println("Parameter value : " +
    public void destroy() 
    { }
    public void service(ServletRequest request, ServletResponse response)
        throws ServletException, IOException 
    { }
    public String getServletInfo() 
        return null;
    public ServletConfig getServletConfig() 
        return null;


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">


To run our ServletConfigExample application hit on the below url


We will get the below output displayed in the browser

Parameter name : admin
Parameter value : JavaInterviewPoint
Parameter name : admin_contact
Parameter value : 123456789


Leave a Reply

Your email address will not be published. Required fields are marked *