Storing parameters in web.xml: context-param & init-param

There are two options to store parameters in web.xml:
context parameters – available to the entire scope of the web application
init parameters – available in the context of a servlet or filter in the web application

Context Parameters



  
    This is a context parameter example
    ContextParam
    ContextParam value
  
...

The following code can be be invoked from a servlet or a filter to retrieve the ContextParam value. The parameter can be read successfully from any servlet or filter class.

	@Override
	public void init(ServletConfig config) throws ServletException {
		String contextParam = config.getServletContext().getInitParameter("ContextParam");
	}

//or
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String contextParam = this.getServletContext().getInitParameter("ContextParam");
		...
	}
	...
}

String value = this.getServletContext().getInitParameter("ContextParam");

Init Parameters

...

    A Servlet
    com.controller.TestServlet
     
        This is an init parameter example 
        InitParam 
        init param value 
     

...

The following code can be be invoked to retrieve the value of InitParam. The parameter can be accessed only from com.controller.TestServlet.

public class DefaultController extends HttpServlet
{
	@Override
	public void init(ServletConfig config) throws ServletException {
		String initParam  = config.getInitParameter("InitParam");
	}

//or
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String initParam = getServletConfig().getInitParameter("InitParam");
		...
	}
	...
}

Iterating through context-params and init-params

It’s possible to iterate through the paramters if required:

public class DefaultController extends HttpServlet
{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Enumeration contextParams = getServletContext().getInitParameterNames();

		Sytem.out.println("context-params: ");
		while (params.hasMoreElements()) {
			String name = (String) params.nextElement();
			Sytem.out.println(name + " = " + config.getInitParameter(name));
		}

		Enumeration initParams = getServletConfig().getInitParameterNames();

		Sytem.out.println("init-params: ");
		while (params.hasMoreElements()) {
			String name = (String) params.nextElement();
			Sytem.out.println(name + " = " + config.getInitParameter(name));
		}

		...
	}
	...
}

Obviously, context parameters are introduced to store parameters that are used in several places while init parameters are associated to a specific web application servlet or filter. They are a great way to avoid the input of hard-coded values inside the code, but in the same time they are limited to simple key-value data representation.

Did you enjoy this tutorial? Be sure to subscribe to the my RSS feed not to miss my new posts!
... or make it popular on

8 Comments

  1. hey good useful stuff….have u used jrun as your server to run scriptlets ? do u need 2 change da path? where is Jintegra.jar nd exel.jar files.
    can u plz tell me how 2 run scriptlets in jrun from scratch?

  2. Hey!

    Can the context-param or init-param tags contain multiple values for the same paramater?

    param1
    val1
    val2

    That is, is the above possible?

Leave a Comment.