How do I read servlet init parameter?

When configuring a servlet in the web.xml file we can define some init parameters in the servlet configuration section. This init parameter can be used to define where a configuration file of our application is stored. This simple servlet below shows how to obtain these init parameters value.

package org.kodejava.example.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InitParameterServlet extends javax.servlet.http.HttpServlet
        implements javax.servlet.Servlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        doPost(request, response);

    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        // Get application configuration path
        String configPath = getInitParameter("configPath");

        System.out.println("Configuration Path: " + configPath);

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.