Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Java Javaee6 setInitParameter() Method Example

Related Tutorials


Share on Google+Share on Google+

setInitParameter() Method Example

In this tutorial you will learn how can you set a context initialization parameter in Servlet 3

setInitParameter() Method Example

In this tutorial you will learn how can you set a context initialization parameter in Servlet 3

setInitParameter() method of ServletContext is used to set the context initialization parameter by the specified name and value. Return type of this method is a boolean type which returns 'true' on successfully set the value otherwise returns false, if the value you are trying to set is already exists.

Syntax :

boolean setInitParameter(String name, String value);

Here parameter 'name' is a specified name by which you want to set the context initialization parameter and the parameter 'value' is a specified value that you want to bind with the corresponding specified name.

Example :

Here I am giving a simple example which will demonstrate you how to set the context initialization parameter using setInitParameter() method.

package roseindia.webContext;


import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
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;

@WebInitParam(name="middleName", value="Kumar")
public class SetInitParameterExample extends HttpServlet
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
ServletConfig config= getServletConfig();

ServletContext context= getServletContext();
context.setInitParameter("firstName", "Bipul");

PrintWriter out= response.getWriter();

String contextParam= context.getInitParameter("firstName");
out.println("Context Parameter Value = "+contextParam);

String configParam= config.getInitParameter("middleName");
out.println("<br>Config Parameter Value = "+configParam);

context.setInitParameter("lastName", "Choudhary");
String contextParam1= context.getInitParameter("lastName");

out.println("<br>Context Parameter Value = "+contextParam1);
boolean bol= context.setInitParameter("lastName", "Kumar");

out.println("<br>you can not replace the value with same key");
out.println("Value is replaced.");
out.println("And Replaced value is : "+context.getInitParameter("lastName"));
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
doGet(request, response);

Output :

When you will execute the above example you will get the output as :

Download Source Code


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  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.

Posted on: December 26, 2011

Related Tutorials

Discuss: setInitParameter() Method Example  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
March 24, 2012
Servlets Init parameters

Sir, in the same example how to add more than one init parameters??
sheetal bagi
December 27, 2012

i am not able to import these files. i am getiing error saying these files cannot be resolved. import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebInitParam; context.setInitParameter("firstName", "Bipul"); for this it says that setInitParameter is undefined for type ServletContext