Share on Google+Share on Google+

@WebInitParam Annotation Servlet Example

In this tutorial you will learn how to use the init parameters in servlet 3.0 specificattion.

@WebInitParam Annotation Servlet Example

In this tutorial you will learn how to use the init parameters in servlet 3.0 specificattion.

In the older version of servlet init parameters specifications were given into the web.xml file. But now from the servlet's 3.0 version it can be given at the time of defining a servlet class which is created using the annotation @WebServlet.  Here you can give the name and value of init param using the initParams attribute. To specifies these initParams the annotation @WebInitParam is used, although you can give these information through the web.xml but, for this you will be required to give these information additionally to the servlet. @WebInitParam has the two attributes (i) "name" that specifies the name of parameter and (ii) "value" which specifies the value of the parameter. These init params can be accessed using the getInitParameter(String name) contained by the ServletConfig

Example :

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

name = "WebInitParamExample", urlPatterns = {"/hello"}
,initParams = {
@WebInitParam(name= "Site :", value=""),
@WebInitParam(name= "Rose", value= "India"),
public class WebInitParamExample extends HttpServlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
PrintWriter out = response.getWriter();
out.println("<h2>Init Param Servlet Example</h2>");
ServletConfig config= getServletConfig();
String pValue= config.getInitParameter("Site :");
out.println("Param Value : "+pValue);
String pValue1= config.getInitParameter("Rose");
out.println("<br>Param Value : "+pValue1);

Before the servlet 3.0 information of init-params were given as :

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="">

<param-name>Site :</param-name>




Output :

Download Source Code



Posted on: December 14, 2011 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 Annotation Servlet Example  

Post your Comment

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