ServletConfig Interface Example

In this post, we will discuss the ServletConfig interface with an example. The ServletConfig interface allows a servlet to obtain configuration data when it is loaded.

ServletConfig Interface Methods

Below class diagram shows a list of ServletConfig interface methods:
Let me describe each ServletConfig interface method here:
  • String getInitParameter(String name) - This method gets the value of the initialization parameter with the given name.
  • Enumeration getInitParameterNames() - This method returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.
  • ServletContext getServletContext() - this method returns a reference to the ServletContext in which the caller is executing.
  • String getServletName() - This method returns the name of this servlet instance.

Add servlet dependency to pom.xml or classpath

Let's add servlet 4.0.1 dependency to pom.xml:
        <!-- -->
If your project is not a maven project then you can download the jar file and keep in the classpath.

ServletConfig Interface Example

In this example, we will use two methods to getInitParameter() and getInitParameterNames() to get all the init parameters from @WebInitParam annotation along with their values. The getInitParameterNames() method returns an enumeration of all parameters names and by passing those names during the call of getInitParameter() method, we can get the corresponding parameter value from the @WebInitParam annotation.

package net.javaguides.servlet.tutorial.examples;


import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "ServletConfig", initParams = {
    @WebInitParam(name = "username", value = "Ramesh"),
    @WebInitParam(name = "password", value = "Pass@123")
}, urlPatterns = "/servlet_config")
public class ServletConfigExample extends HttpServlet {

    private static final long serialVersionUID = 1 L;

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        PrintWriter printWriter = resp.getWriter();
        printWriter.print("<h1>ServletConfig Data</h1>");
        ServletConfig sc = getServletConfig();

        Enumeration < String > e = sc.getInitParameterNames();
        String str;
        while (e.hasMoreElements()) {
            str = e.nextElement();
            printWriter.println("<br>Param Name: " + str);
            printWriter.println(" value: " + sc.getInitParameter(str));
        printWriter.print("<a href=\"\">Java Guides</a>");


Let's deploy above ServletConfigExample servlet into tomcat server. Hit http://localhost:8080/java-servlet-tutorial/servlet_config URL in a browser will result below web page which displays ServletConfig data:


  1. Hello Ramesh,

    Nice blog! I am editor at Java Code Geeks ( We have the JCG program (see, that I think you’d be perfect for.

    If you’re interested, send me an email to [email protected] and we can discuss further.

    Best regards,
    Eleftheria Drosopoulou


Post a Comment

Leave Comment