@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:
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.javaguides.servlet.tutorial</groupId>
    <artifactId>java-servlet-tutorial</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>java-servlet-tutorial Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- 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>
    </dependencies>
    <build>
        <finalName>java-servlet-tutorial</finalName>
    </build>
</project>

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
 *
 */
@WebServlet(
    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;

    @Override
    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:
@WebFilter(
        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

 Reference


Comments