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

admin  

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


<web-app id="WebApp_ID" version="2.4" 
         xmlns="https://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://java.sun.com/xml/ns/j2ee https://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <context-param>
    <description>This is a context parameter example</description>
    


ContextParam</param-name>
    


ContextParam value</param-value>
  </context-param>
...
<web-app>

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

...
<servlet>
    <servlet-name>A Servlet</servlet-name>
    <servlet-class>com.controller.TestServlet</servlet-class>
    <init-param> 
        <description>This is an init parameter example</description> 
        


InitParam</param-name> 
        


init param value</param-value> 
    </init-param> 
</servlet>
...

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.