1

New to Java and I'm trying to find the equivalent for web.config application settings in .NET.

So far I have found examples of something like this:

Properties props = new Properties();
FileInputStream fis = new FileInputStream("c:/appSettings.properties");
props.load(fis);

However, I know that .NET caches the web.config file and inserting a call to Configuration.AppSettings[x] is not expensive. I'm looking for an equivalent solution. So that I can call for Application settings multiple times through out a page load.

For Example:

<link  href="<%= ResolveUrl("~/css/main.css?ver=" + Configuration.AppSettings["version"]) %>" rel="Stylesheet" type="text/css" media="screen" />

I can create a class that can do the equivalent, but i'd hate to reinvent the wheel if there is already something there.

capdragon
  • 14,565
  • 24
  • 107
  • 153
  • 1
    Create a properties file and load it into your application through ServletContext. – Luiggi Mendoza Dec 18 '13 at 15:08
  • Word of advice: it doesn't help you to try and make Java behave like .NET; the two platforms have completely different design philosophies. Rather try and find a way to do what you functionally want to achieve within the context of the Java (Enterprise?) platform and forget that .NET exists. – Gimby Dec 18 '13 at 15:12
  • 1
    The Java equivalent for .NET's `Web.config` is `web.xml`, the deployment descriptor for webapps, which is in the WEB-INF directory of your webapp. You can set parameters there which are accessible via the `ServletContext` as Luiggi mentioned. – Jesper Dec 18 '13 at 15:13
  • @Jesper: Thanks, could you point me to any examples or tutorials? And does it get cached? – capdragon Dec 18 '13 at 15:14
  • See for example: http://docs.oracle.com/javaee/6/tutorial/doc/bnafo.html – Jesper Dec 18 '13 at 15:15
  • Hava a look into **tag libraries** and **EL, expression language**. For something like `` _and better_. Look at message bundle tags (properties) and java beans. – Joop Eggen Dec 18 '13 at 15:15
  • @Jesper is [this](http://www.mkyong.com/servlet/how-to-pass-parameters-to-whole-servlet-application-servletcontext/) what you're referring to? – capdragon Dec 18 '13 at 15:18
  • Yes, that's how you can set parameters in `web.xml` that you can access from your servlets or JSPs. – Jesper Dec 18 '13 at 15:27
  • @Jesper Create an answer so I can give you credit. – capdragon Dec 18 '13 at 15:28

3 Answers3

2

The Java equivalent for .NET's Web.config is web.xml, the deployment descriptor for Java EE webapps.

In web.xml you can set context parameters, that you can access via the class ServletContext in your servlets or JSPs. For example:

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.mycompany.myapp.SomeServlet</servlet-class>
</servlet>
<context-param>
    <param-name>myparam</param-name>
    <param-value>hello</param-value>
</context-param>

Inside your JSP:

// 'application' is an implicit variable available inside JSPs
// that refers to the ServletContext
String value = application.getInitParameter("myparam");
Jesper
  • 202,709
  • 46
  • 318
  • 350
0

You can also use init-param in web.xml. I got this from Google Cloud Endpoint's example at https://cloud.google.com/appengine/docs/java/endpoints/required_files

<servlet>
    <servlet-name>SystemServiceServlet</servlet-name>
    <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
    <init-param>
        <param-name>services</param-name>
        <param-value>com.example.helloendpoints.Greetings</param-value>
    </init-param>
</servlet>

Another example from a post at Getting the init parameters in a servlet shows: Accessing servlet init parameter in a servlet for which it was defined in DD:

getServletConfig().getInitParameter("name");
Community
  • 1
  • 1
Hil Liao
  • 1
  • 1
0

https://commons.apache.org/proper/commons-configuration/ apache commons configuration is the best choice.

Automatic Reloading A common issue with properties file is to handle the reloading of the file when it changes. The sample code is found beneath:

PropertiesConfiguration config = new 
PropertiesConfiguration("usergui.properties");
config.setReloadingStrategy(new FileChangedReloadingStrategy());
amonk
  • 1,769
  • 2
  • 18
  • 27