Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set a system property in a web app?

 
JD Glanville
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I set a system property for a web application?
I want to use the java.util.logging classes within my web application as my logging framework. In this framework, I set all my logging properties in a properties file. I then want to set the system property �java.util.logging.config.file� to point to this properties file. This system property is used by the logger for initialization.
How do I state the system property in web.xml? I don�t want to have to pass in a property through the command line (i.e.: �java �Djava.util�.�).
I originally tried the <env-entry> tag, but that didn�t seem to work (I dumped the System.getProperties() values to standard out in a servlet to test). Besides, all the examples I�ve been able to find that use <env-entry> show using a JNDI context to access the variable. I�m assuming that the logging framework is only using System.getProperty().
I then tried the <context-param> tag, with similar results: no success.
How do I set a system property for a web application?
 
Sri Basavanahally
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried <init-param> ? in web.xml ? You can use it in your program with ServletConfig object.
 
JD Glanville
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunatly, <init-param> is used on the initialization of a servlet. I want this to be set on an application.
You see, I'm creating a tag library and I'm wanting to log some of the actions within the library. That's why I thought <context-param> would have worked.
Thanks for the suggestion though.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<context-param> should suffice quite nicely for what you are doing. How is it not working for you?
bear
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you say: This system property is used by the logger for initialization.

So you mean that you do this, in your logging initialization...

String the_file = System.getProperty("java.util.logging.config.file");


And instead of sending it into the program with a -D switch, you want to prepare j.u.l.c.file with web.xml...

So at some point you'll need to retrieve that property (from the context-param), probably in a lifecycle listener (there's one for application startup) and use the corresponding System.setProperty() method to place it into the System's property space. That way your code that expects it in System.getProperty() will find it.
 
Kiran Baratam
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am also having the same problem.I am trying to set a system property to accesss RMI server.I could not able to connect to RMI server.Do i need to plase the java.policy in any specific place???
Kiran
 
JD Glanville
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Curwen:
you say: This system property is used by the logger for initialization.

So you mean that you do this, in your logging initialization...

String the_file = System.getProperty("java.util.logging.config.file");

Actually, I don't do that myself, the logging framework of the JDK expects it. If you look at the javadoc for java.util.logging.LogManager, it describes how LogManager uses this property.

However, I'm now learning that there is no way to directly set a system property from within the web.xml (disapointing). Therefore, I'm going to need to do what Mike suggested: add an application life-cycle servlet, translate the <context-param> into a system property, and then reset the logging configuration using LogManager.readConfiguration().
Thanks for the help, yall (that's supposed to be you-all, rolled together in a Texan-style, which is interesting to hear, coming from a Scottish-Canadian) ;-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic