@WebInitParam Annotation Example

The @WebInitParam annotation is used to specify any initialization parameters that must be passed to the Servlet or the Filter. It is an attribute of the @WebServlet and @WebFilter annotation.
Below diagram shows a @WebInitParam annotation is an attribute of the @WebServlet annotation to define the initialization parameters.

@WebInitParam Annotation Attributes

Below diagram shows a class diagram of @WebInitParam annotation:
Let me list out all the attributes used for @WebInitParam annotation:
1. String name - Name of the initialization parameter
2. String value - Value of the initialization parameter
3. String description - Description of the initialization parameter

@WebInitParam Annotation Example

Let's develop a complete example to demonstrate the usage of @WebInitParam annotation.

Add servlet dependency to pom.xml

Let's first add servlet 4.0.1 dependency to our pom.xml:
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <name>java-servlet-tutorial Maven Webapp</name>
        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->

Example 1: Define Servlet and Initialize Parameters using @WebInitParam Annotation

The following example describes how to use @WeInitParam annotation along with @WebServlet annotation. It is a simple servlet that displays the user firstName and lastName which are taken from the init parameters.
package net.javaguides.servlet.tutorial.httpservlet;

import java.io.IOException;
import java.io.PrintWriter;

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;

 * Create Servlet using @WebServlet annotation
 * @author Ramesh Fadatare
    urlPatterns = "/userinfo",
    initParams = {
        @WebInitParam(name = "firstName", value = "Ramesh"),
        @WebInitParam(name = "lastName", value = "Ramesh")
public class WebInitParamExample extends HttpServlet {

    private static final long serialVersionUID = 1 L;

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String firstName = getInitParameter("firstName");
        String lastName = getInitParameter("lastName");

        PrintWriter writer = resp.getWriter();

        writer.println("firstName = " + firstName);
        writer.println("lastName = " + lastName);
Let's run above servlet in web container will result following web page in a browser:

Example 2: Define Filter and Initialize Parameters using @WebInitParam Annotation

The following example describes how to use @WeInitParam annotation along with @WebFilter annotation:
        urlPatterns = "/auto",
        initParams = @WebInitParam(name = "username", value = "Ramesh")
public class AutoFilter implements Filter {
    // overrides filter methods ..

Related Servlet Examples

Check out complete Servlet 4 tutorial at Servlet Tutorial