|
Java™ by example!
|
|
|
How do I read environment variables from a Servlet?
To get environmental values, servlets should use
ServletConfig.getParameter(String param_name) |
method. The ServletConfig object can be obtained either during any service method or at
time by calling
. The tricky and vendor-dependant part is defining the parameters. With Apache/Jserv, you need to alter relevant zone.properties file and set
servlet.<servlet-name>.initArgs=my_param=my_value |
With Tomcat, go to $TOMCAT_HOME/webapps//WEB-INF/web.xml and have it include something like this:
<servlet> <servlet-name> my_servlet </servlet-name> <init-param> <param-name>param_name</param-name> <param-value>param_value</param-value> </init-param> </servlet>
|
You can have as many sections as you need parameters to be passed. A good way is to pass to servlets only one parameter, which is the path to some configuration file, so that only this file needs to be changed to alter servlets behaviour, and access permissions for this file do not depend on access rights to other config files. This is important when people with different access rights have to manage several web apps running on the same servlet engine.
Further Information
Author of answer: hal9000
Comments
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|