This tutorial shows you how use the servlet 3 initialization parameter annotation @WebInitParam to inject initialized parameters in a servlet. We can configure a servlet entirely with annotations and without using web.xml configuration. This is third post of series of <a href=”https://javabycode.com/java-frameworks/servlet-jsp/java-servlet-tutorial-java-servlet-example.html” target=”_blank”>Java Servlet Tutorial</a>. This series tutorial will provide you full knowledge about Servlet 3.0.
Project structure

Maven Dependency
1 2 3 4 5 6 7 8 |
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> |
Servlet 3 initialization parameter annotation
The @WebInitParam annotation is used to initialize parameters for a servlet or filter. The servlet 3 parameter takes a required name and value. You can add a description for a servlet or filter also. You can get initialized parameters using the servletConfig.getInitParameter() method in the init() method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.javabycode; 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; import java.io.IOException; import java.io.PrintWriter; @WebServlet(value = "/example", initParams = { @WebInitParam(name = "email", value = "servletdemo@javabycode.com", description = "Email from admin"), @WebInitParam(name = "phoneNumber", value = "(800) 283-20xx", description = "Phone admin") }, description = "Initialization parameters example using annotation: @WebInitParam") public class ServletDemo extends HttpServlet { private String email; private String phoneNumber; @Override public void init(ServletConfig config) throws ServletException { email = config.getInitParameter("email"); phoneNumber = config.getInitParameter("phoneNumber"); } protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); out.write("<h2>Initialization parameters example using annotation: @WebInitParam</h2>"); out.write("<p><strong>E-mail: </strong>" + email + "</p>"); out.write("<p><strong>Phone Number: </strong>" + phoneNumber + "</p>"); } } |
Deploy Servlet 3 Initialization Parameter Annotation on application server
This example is deployed on tomcat 8 with the below address.
URL: http://localhost:8080/servlet-param/example
Here is output

That’s all. Now you can customize this example and implement your own Servlet 3 Parameter Annotation Example. You can also find other servlet example in the series of <a href=”https://javabycode.com/java-frameworks/servlet-jsp/java-servlet-tutorial-java-servlet-example.html” target=”_blank”>Java Servlet Example</a>.
In the next tutorial, we will show you how to create Servlet 3 Initialization Parameter example using web.xml configuration.
Download complete source code, please click link below
Servlet-3-Parameter-Annotation-Example.zip (352 downloads)