J2EE - Web.xml - How do I define an environment variable that can be accessed in all servlets in a Web application ?

> Procedural Languages > Java

1 - About

A J2EE application may require one or more initialization parameters to be accessed by one or more servlets in a web application.


3 - Steps

An initialization parameter be for a context or for a specific servlet. The context level parameters can be accessed by any servlets in the web application.

3.1 - Definition of a context parameter

To define a context parameter you have to add in the following in the web.xml as follows:


3.2 - Using the context.getInitParameter method

You have to use context.getInitParameter method to get the Context level parameter as in the following code:

javax.servlet.ServletContext context = getServletContext();
String fileDir = context.getInitParameter("filedir"); 
lang/java/web_application_parameter.txt · Last modified: 2017/02/12 21:05 by gerardnico