• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tomas Andrasko
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Tomas Andrasko
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikiran,

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

Tomas
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic