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

Java Conceptuel Diagram

About

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

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.

Definition of a context parameter

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

<context-param>
  <param-name>filedir</param-name>
  <param-value>/temp</param-value>
</context-param>

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"); 





Discover More
Java Conceptuel Diagram
J2EE - Web.xml (Deployment descriptor of a Web Application)

The web.xml file is the deployment descriptor file for a web application. It's a J2EE standard XML document that configures the contents of a WAR file. You can override any of the default servlet parameters...
Card Puncher Data Processing
Oracle Containers for J2EE (OC4J)

OC4J is a Java application server. It's composed with , so a lot of configuration for are the same for . This Java application server is a slimmed-down version of the better-known Oracle Application...



Share this page:
Follow us:
Task Runner