Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Tomcat and the fly likes Tomcat 7 - Define variable in context config file and read it in web.xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Tomcat 7 - Define variable in context config file and read it in web.xml" Watch "Tomcat 7 - Define variable in context config file and read it in web.xml" New topic
Author

Tomcat 7 - Define variable in context config file and read it in web.xml

Tomas Andrasko
Greenhorn

Joined: May 25, 2014
Posts: 3
Hi,
I need deploy several instances of the same web application (webapp uses Spring framework 4.0.x) as WARs with different names inside one Tomcat 7 instance. Configuration files are located outside of WAR in $Tomcat/conf/<appname>, path to this directory is located in context config file this way:

In Spring config files I am reading variable "appConfigPath" in a property configurer bean using "java:comp/env" and this works fine.



Now I need use the same variable in web.xml file to define path to log4j.xml.



but Tomcat during deploying / starting the application does not substitute the "variable" to its value.

How can I read the variable inside the web.xml ?

Thanks a lot for some help.

Tomas
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9956
    
163

Tomas, welcome to CodeRanch!


You don't need to do any param configurations in web.xml since you have setup the environment variable already in the Tomcat context xml. To get to the value of that configured reference, in your servlet code, all you have to do is:



[My Blog] [JavaRanch Journal]
Tomas Andrasko
Greenhorn

Joined: May 25, 2014
Posts: 3
Jaikiran Pai wrote:Tomas, welcome to CodeRanch!
You don't need to do any param configurations in web.xml since you have setup the environment variable already in the Tomcat context xml.


Hi Jaikiran,

you're right, I could read the param in java code, but I can't because Log4j have to be parameterized earlier than Spring loads the application context. Scenario with configuring Log4j inside the servlet code have been the first solution I have used, but it produces warning.



Using of listener inside the web.xml is the solution, but now I need solve parameterizing the place of Log4j configuration file.

Tomas
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9956
    
163

Tomas, you can do the lookup in the ServletContextListener too. In its contextInitialized method you can do something like:



It doesn't have to be a Servlet.
Tomas Andrasko
Greenhorn

Joined: May 25, 2014
Posts: 3
Jaikiran,

using ServletContextListener seems to be working fine, thanks a lot.

Tomas
 
Consider Paul's rocket mass heater.
 
subject: Tomcat 7 - Define variable in context config file and read it in web.xml
 
Similar Threads
problems with faces (myfaces and mojarr 2.1.7) and grails 2.03 incompatibility - need some assistanc
ManagedBean is not accessible, using JSF2.2, Spring 4.0 and SpringSecurity 3.2
WSDP2.0 pack example
[Architecture] Frameworks, tools, plugins and etc - What next
Create subclass of struts ActionServlet that supports "classpath:" config file paths.