ServletConfig Interface in Java


This article explains the ServletConfig interface in Java. The Netbeans IDE is used for sample example.

What ServletConfig is

This object is provided by the Servlet vendor, it provides configuration details from the web.xml file to servlets.

Servlets don't need to modify it when there is a change in the web.xml file. So it makes web applications easy to manage when any specific content is modified from time to time.


The main advantage is that if there is any modification in our web.xml file then we don't need to modify the servlet file.

Some public methods of the ServletConfig interface are:

  • Enumeration getInitParameterNames()

          Returns an enumeration for all paramters.

  • String getServletName()

          Returns the servlet name.

  • ServletContext getServletName()String

          Returns the value of the specified parameter.

  • String getInitParameter(String sname)

          Returns the value of a specified parameter.

  • ServletContext getServletContext();

          Returns the ServletContext object.

How to create the object of ServletConfig

javax.servlet.Servlet provides a method named getServletConfig(). This method returns the object of the ServletConfig interface.


public ServletConfig getServletConfig();


ServletConfig object=getServletConfig();

What the Initialization parameter (IP) is

IP refers to information provided by the application programmer in the web.xml file. This information is made available to the servlet by the web server.

They are of the following two types:

  1. Servlet Specific.
  2. Application Specific.

1. Servlet Specific

These IPs provide information to a specific servlet at the application.

2. Application Specific

These IPs provide uniforms to all the servlets of the app.

A Servlet specific and application specific IP are defined in the web.xml as follows.




Example of ServletConfig

In this example, we are creating a servlet that gets the parameter from the web.xml file then deploys it on the server.

We need to use the following procedure.

Step 1

Open the NetBeans IDE.


Step 2

Choose "Java web" -> "web application" as shown below.


Step 3

Now type your project name as "ServletConfigDemo" and click on "Next" as shown below:


Step 4

Now choose your server and Java version installed on your system and then click on "Finish" as shown below.


Step 5

Now delete your default index.jsp file and create a new index.html file as shown below.


Step 6

Now type your file name as "index" as shown below.


Step 7

Now provide the following code over there.

<!DOCTYPE html>




        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <meta name="viewport" content="width=device-width">



    <center><h1>Click on below link to get oracle-driver name</h1></center><br>

    <a href="ServletConfigDemo">Click Here</a>




Step 8

Now create a servlet named "ServletConfigDemo" and enter the following code into it.


import javax.servlet.*;

import javax.servlet.http.*;

public class ServletConfigDemo extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {


        PrintWriter out = response.getWriter();

        ServletConfig config = getServletConfig();

        String driver = config.getInitParameter("driver");

        out.print("Oracle Driver is: " + driver);





Step 9

Now replace the code of your default web.xml file with the following code.


<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">
















Step 10

Now our application is ready.

Right-click on the project menu then select "Run". The following output is generated.


Now click on the link provided.