ZetCode

Java ServletConfig

last modified November 30, 2026

Java ServletConfig tutorial shows how to pass initialization data to a servlet with ServletConfig.

ServletConfig is a servlet configuration object used by a servlet container to pass information to a servlet during initialization. Servlet container creates a ServletConfig for each servlet in a web application.

Java Servlet

Servlet is a Java class which responds to a particular type of network request - most commonly an HTTP request. Java servlets are used to create web applications. They run in servlet containers such as Tomcat or Jetty. Modern-day Java web development uses frameworks that are built on top of servlets.

Pure.css

Pure.css is a set of small, responsive CSS modules that can be used in every web project. The library is created by Yahoo.

Java ServletConfig example

In the following web application, we have a simple web form. We sent a name parameter to the servlet. If the parameter is empty, we read an initialization parameter through ServletConfig. In the example, we also use Yahoo's Pure.css library.

$ tree
.
├── nb-configuration.xml
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── zetcode
    │   │           └── web
    │   │               └── MyServlet.java
    │   └── webapp
    │       └── index.html
    └── test
        └── java

This is the project structure.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zetcode</groupId>
    <artifactId>JavaServletConfigEx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>JavaServletConfigEx</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>25</maven.compiler.release>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>6.1.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.eclipse.jetty.ee11</groupId>
                <artifactId>jetty-ee11-maven-plugin</artifactId>
                <version>12.1.15</version>
                <configuration>
                    <webApp>
                        <contextPath>/app</contextPath>
                    </webApp>
                </configuration>
            </plugin>

        </plugins>
    </build>

</project>

This is the Maven pom.xml file. The jakarta.servlet-api artifact provides the Servlet API. The maven-war-plugin packages the application as a WAR, and the Jetty EE11 plugin runs it at /app with mvn jetty:run.

index.html
<!DOCTYPE html>
<html>
    <head>
        <title>Home Page</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://unpkg.com/purecss@1.0.0/build/pure-min.css">
    </head>
    <body>
        <form class="pure-form" action="MyServlet">
            <fieldset>
                <legend>Enter your name</legend>

                <input type="text" name="name">
                <button type="submit" class="pure-button pure-button-primary">Submit</button>
            </fieldset>
        </form>
    </body>
</html>

This is the home page. It contains an HTML form. Upon submitting the form, the processing is sent to the MyServlet.

<link rel="stylesheet" href="https://unpkg.com/purecss@1.0.0/build/pure-min.css">

We include the Pure.css library.

<form class="pure-form" action="MyServlet">

The form tag uses the pure-form class from the Pure.css library. The action attribute points to the MyServlet.

<input type="text" name="name">

The name value entered by the user will be sent to the servlet as a name parameter.

<button type="submit" class="pure-button pure-button-primary">Submit</button>

The Submit button uses pure-button and pure-button-primary classes.

com/zetcode/MyServlet.java
package com.zetcode.web;

import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.annotation.WebInitParam;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

@WebServlet(name = "MyServlet", urlPatterns = {"/MyServlet"}, initParams = {
    @WebInitParam(name = "name", value = "Guest")})
public class MyServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/plain;charset=UTF-8");

        String name = request.getParameter("name");

        if (name == null || name.isBlank()) {

            name = getServletConfig().getInitParameter("name");
        }

        PrintWriter out = response.getWriter();
        out.println("Hello " + name);
    }
}

The MyServlet reads the name attribute from the request and generates output. The output is plain text.

@WebServlet(name = "MyServlet", urlPatterns = {"/MyServlet"}, initParams = {
    @WebInitParam(name = "name", value = "Guest")})

With @WebInitParam, we initialize a name parameter to the "Guest" value.

String name = request.getParameter("name");

if (name == null || name.isBlank()) {
    name = getServletConfig().getInitParameter("name");
}

We read the name parameter from the request. If it is missing or blank, we read the name initialization parameter using ServletConfig. The parameter is retrieved with getInitParameter.

PrintWriter out = response.getWriter();
out.println("Hello " + name);

We write the text message with a character writer.

In this tutorial, we have used ServletConfig to read an initialization parameter in a Java servlet.

Running the application

Start the application with the Jetty Maven plugin and open http://localhost:8080/app/.

$ mvn jetty:run

Verified output

A request parameter overrides the initialization parameter, while a request without the parameter falls back to the value from @WebInitParam.

$ curl 'localhost:8080/app/MyServlet?name=Jane'
Hello Jane

$ curl localhost:8080/app/MyServlet
Hello Guest

Source

Jakarta Servlet specification

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Java Servlet tutorials.