Share on Google+Share on Google+


This section contains detailed description of @WebInitParam annotation and its implementation with sample code.


This section contains detailed description of @WebInitParam annotation and its implementation with sample code.

The @WebInitParam is used to set parameters which is available trough out in the context of a Servlet or filter in a web application. Only Servlet or Filter for which these init parameters are set ,can access the init parameters.

Prior to Servlet 3.0, the <init-param> can be set in web.xml as follows :



Now (in Servlet 3.0), you can set the init parameter using annotation @WebInitParam as follows :

	initParams = {
	@WebInitParam(name="rose", value="Hello "),
	@WebInitParam(name="india", value=" World!")

The @WebInitParam has the following attributes :

  • name : Contains the name of the init parameter as string.

  • value : Contains the value of the init parameter as string.

  • description :Contains the description of the init parameter.

The tools and technology used in the example is given below :

  • Eclipse Helios 3.6.1

  • Tomcat 7

  • jdk1.6.0_18


In the below tutorial, you will learn to set the init parameters using @WebInitParam  , which we also read and displays as output :

package roseindia;


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;

@WebServlet(asyncSupported = false, name = "WebInitParamAnnotation", urlPatterns = { "/initparamServlet" }, initParams = {
@WebInitParam(name = "param1", value = "Rose"),
@WebInitParam(name = "param2", value = "India") })
public class WebInitParamAnnotation extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

PrintWriter out = res.getWriter();

// Writing response to display on page
out.write("<html><head><title>@WebInitParam Annotation</title></head>");
out.write("<h2>@WebInitParam Annotation Example</h2>");
out.write("<h3>WELCOME TO <font color='RED'>ROSEINDIA </font>!!!!</h3>");
out.write("<p>The name & value of two init parameter are given below --></p>");
out.write("<p><b>param1</b> --> " + getServletConfig().getInitParameter("param1")+"</p>");
out.write("<p><b>param1</b> --> " + getServletConfig().getInitParameter("param2")+"</p>");



When you execute the above code, you will get the following output :

Download Source Code


Posted on: June 27, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: @WebInitParam  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image