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:
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
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.

ServletConfigExample.java

package net.javaguides.servlet.tutorial.examples;

import java.io.IOException;

import java.io.PrintWriter;
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;

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

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter printWriter = resp.getWriter();
        printWriter.print("<html>");
        printWriter.print("<body>");
        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=\"http://www.javaguides.net\">Java Guides</a>");
        printWriter.print("</body>");
        printWriter.print("</html>");
        printWriter.close();
    }
}

Output

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:

Comments

  1. Hello Ramesh,

    Nice blog! I am editor at Java Code Geeks (www.javacodegeeks.com). We have the JCG program (see www.javacodegeeks.com/join-us/jcg/), that I think you’d be perfect for.

    If you’re interested, send me an email to eleftheria.drosopoulou@javacodegeeks.com and we can discuss further.

    Best regards,
    Eleftheria Drosopoulou

    ReplyDelete

Post a Comment