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

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <context-param>
    <description>This is a context parameter example</description>
    <param-name>ContextParam</param-name>
    <param-value>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> 
        <param-name>InitParam</param-name> 
        <param-value>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));
		}

		...
	}
	...
}

Related Posts

  • No Related Post
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